六狼论坛

 找回密码
 立即注册

QQ登录

只需一步,快速开始

新浪微博账号登陆

只需一步,快速开始

搜索
查看: 47|回复: 0

页面字符串截取自定义标签

[复制链接]

升级  98%

13

主题

13

主题

13

主题

童生

Rank: 1

积分
49
 楼主| 发表于 2013-2-6 08:42:30 | 显示全部楼层 |阅读模式
描述:需求新闻,公告,提示。。。。对于页面过长的标题,进行指定长度的显示
tld描述:
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/j2ee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
    version="2.0">
    <description>JSTL 1.1 XML library</description>
    <display-name>JSTL XML</display-name>
    <tlib-version>1.1</tlib-version>
    <short-name>time</short-name>
    <uri>http://java.sun.com/jsp/jstl/xml</uri>
    <tag>
        <description>subString</description>
        <name>subString</name>
        <tag-class>
            指定类
        </tag-class>
        <body-content>JSP</body-content>
        <attribute>
        <name>subBegin</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
        <name>index</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
        <name>classStyle</name>
        <required>true</required>
        <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
</taglib>
 
指定类描述:
 
/*
 * 字符串截取
 */
public class SubString extends TagSupport {

    private static final long serialVersionUID = 6833509617093650476L;

    private String subBegin;//需要截取的字符串

    private int index; //截取长度

    private String classStyle; //css样式

    private String subEnd; // 显示的字符串

    public String getClassStyle() {
        return classStyle;
    }

    public void setClassStyle(String classStyle) {
        this.classStyle = classStyle;
    }

    public int getIndex() {
        return index;
    }

    public void setIndex(int index) {
        this.index = index;
    }

    public String getSubBegin() {
        return subBegin;
    }

    public void setSubBegin(String subBegin) {
        this.subBegin = subBegin;
    }

    public String getSubEnd() {
        return subEnd;
    }

    public void setSubEnd(String subEnd) {
        this.subEnd = subEnd;
    }

    @Override
    public int doEndTag() throws JspException {
        // TODO Auto-generated method stub
        return EVAL_BODY_INCLUDE;
    }

    @Override
    public int doStartTag() throws JspException {
        // TODO Auto-generated method stub
        JspWriter out = pageContext.getOut();
        if (subBegin == null || subBegin.equals("")) {
            subBegin = "";
            try {
                out.write("<span class='" + classStyle + "'>" + subBegin
                        + "</span>");
            } catch (IOException e) {
                Log4J.logWarn(e.getMessage());
            }
        } else {
            if (subBegin.length() > index) {
                subEnd = subBegin.substring(0, index)+"...";
            } else {
                subEnd = subBegin;
            }
            try {
                out.write("<span class='" + classStyle + "' title='" + subBegin
                        + "'>" + subEnd + "</span>");
            } catch (IOException e) {
                Log4J.logWarn(e.getMessage());
            }
        }
        return EVAL_PAGE;
    }

}
您需要登录后才可以回帖 登录 | 立即注册 新浪微博账号登陆

本版积分规则

快速回复 返回顶部 返回列表