taglib--文本切割

返回 常用taglib及用法>>

 

1. tld文件配置

<tag>
		<name>stringCut</name>
		<tag-class>com.neu.edu.utils.mytaglib.StringCutTag</tag-class>
		<body-content>JSP</body-content>
		<attribute>
			<name>length</name>
			<required>true</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
		<attribute>
			<name>str</name>
			<required>false</required>
			<rtexprvalue>true</rtexprvalue>
		</attribute>
	</tag>
 

2. 实现

package com.neu.edu.utils.mytaglib;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyContent;
import javax.servlet.jsp.tagext.BodyTagSupport;
import javax.servlet.jsp.tagext.JspFragment;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import org.apache.commons.codec.EncoderException;
import org.apache.commons.codec.net.URLCodec;
import org.apache.commons.lang.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.xiaonei.wap.framework.util.WapStringUtils;

public class StringCutTag extends BodyTagSupport {
    
    private int length = 0;
    
    private String str = "";

    public void setLength(int length) {
        this.length = length;
    }

    public void setStr(String str) {
        this.str = str;
    }

    @Override
    public int doAfterBody() throws JspException {
        BodyContent bodyContent = getBodyContent();
        
        String content = bodyContent.getString();
        
        String outStr = cut(content, length);
        
        JspWriter out = bodyContent.getEnclosingWriter();
        
        try {
            out.write(outStr);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return super.doAfterBody();
    }

    @Override
    public int doStartTag() throws JspException {
        
        if(StringUtils.isNotBlank(str)){
            String outStr = cut(str, length);
            try {
                pageContext.getOut().print(outStr);
            } catch (IOException e) {
                e.printStackTrace();
            }
            return SKIP_BODY;
        } else
            return super.doStartTag();
    }
    private  String cut(String str, int length) {

        if (StringUtils.isBlank(str) || length <= 0) return "";

        if (str.length() <= length) return str;

        int tlength = 0;
        int maxLength = length * 2;
        int i;
        for (i = 0; i < str.length() && tlength <= maxLength; i++) {
            if (CharUtils.isAscii(str.charAt(i))) ++tlength;
            else tlength += 2;
        }
        if (i == str.length() && tlength <= maxLength) return str;
        maxLength -= 2;
        while (tlength > maxLength) {
            if (CharUtils.isAscii(str.charAt(i - 1))) --tlength;
            else tlength -= 2;
            --i;
        }
        return str.substring(0, i) + "...";
    }

}
 

3. 使用

 

    <renren-wap:stringCut length="28">${content}</renren-wap:stringCut>

 

返回>>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值