按字节截取字符串的jsp自定义标签

按字节截取字符串的jsp自定义标签

 

tld 定义如下:

	<!-- 按字节数截取字符串 -->
    <tag>
      <name>sliceByte</name>
        <tag-class>...</tag-class>
        <body-content>empty</body-content>
        <attribute>
           <name>baseStr</name>
           <required>true</required>
           <rtexprvalue>true</rtexprvalue>
        </attribute>
	    <attribute>
           <name>byteCount</name>
           <required>true</required>
           <rtexprvalue>true</rtexprvalue>
        </attribute>
        <attribute>
           <name>escapeXml</name>
           <required>false</required>
           <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
           <name>addEllipsis</name>
           <required>false</required>
           <rtexprvalue>false</rtexprvalue>
        </attribute>
        <attribute>
           <name>variable</name>
           <required>false</required>
           <rtexprvalue>false</rtexprvalue>
        </attribute>
    </tag>

 

 

java代码实现如下:

import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.SimpleTagSupport;

import org.apache.commons.lang.StringEscapeUtils;

/**
 * 按字节截取字符串标签 (非按字符截取)
 * 
 * @author chengkai
 * 
 */
public class SliceByte extends SimpleTagSupport {

	/**
	 * 原始字符串
	 */
	private String baseStr;
	/**
	 * 截取数量
	 */
	private long byteCount;

	/**
	 * 是否转义
	 */
	private boolean escapeXml;

	/**
	 * 是否在进行截取后添加...
	 */
	private boolean addEllipsis;

	/**
	 * 将截取后的结果保存进指定变量
	 */
	private String variable;

	public String getBaseStr() {
		return baseStr;
	}

	public void setBaseStr(String baseStr) {
		this.baseStr = baseStr;
	}

	public long getByteCount() {
		return byteCount;
	}

	public void setByteCount(long byteCount) {
		this.byteCount = byteCount;
	}

	public boolean isEscapeXml() {
		return escapeXml;
	}

	public void setEscapeXml(boolean escapeXml) {
		this.escapeXml = escapeXml;
	}

	public boolean isAddEllipsis() {
		return addEllipsis;
	}

	public void setAddEllipsis(boolean addEllipsis) {
		this.addEllipsis = addEllipsis;
	}

	public String getVariable() {
		return variable;
	}

	public void setVariable(String variable) {
		this.variable = variable;
	}

	@Override
	public void doTag() throws JspException, IOException {
		JspWriter out = this.getJspContext().getOut();
		String resultStr = this.substringByBytes(this.baseStr, 0, byteCount);
		resultStr = resultStr == null ? "" : resultStr;

		if (addEllipsis && baseStr != null && baseStr.length() != resultStr.length()) {
			resultStr += "...";
		}

		resultStr = escapeXml ? this.html(resultStr) : resultStr;
		if (this.variable != null) {
			this.getJspContext().setAttribute(variable, resultStr);
		} else {
			out.print(resultStr);
		}
	}

	/**
	 * 根据字节截取字符串
	 * 
	 * @param baseString
	 *            原始字符串
	 * @param offset
	 *            字节偏移索引 (包含)
	 * @param count
	 *            要截取的字节数
	 * @return 返回截取后的字符串
	 */
	public String substringByBytes(String baseString, long offset, long count) {
		if (baseString == null) {
			return null;
		}
		offset = offset < 0 ? 0 : offset;
		count = count < 0 ? 0 : count;
		char[] chr = baseString.toCharArray();
		StringBuffer resultStr = new StringBuffer();
		for (int index = 0, bytecnt = 0, bytenum = 0; index < chr.length; index++) {
			int currentBC = String.valueOf(chr[index]).getBytes().length;
			bytecnt += currentBC;
			if (bytecnt >= offset) {
				bytenum += currentBC;
				if (bytenum <= count) {
					resultStr.append(chr[index]);
				} else {
					break;
				}
			}
		}
		return resultStr.toString();
	}

	private String html(String content) {
		return StringEscapeUtils.escapeXml(content);
	}

}
 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值