`
xyqck163
  • 浏览: 106288 次
  • 性别: Icon_minigender_1
  • 来自: 北京
文章分类
社区版块
存档分类
最新评论

按字节截取字符串的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);
	}

}
 
分享到:
评论

相关推荐

    JS简单限制textarea内输入字符数量的方法

    文章中展示了两个关键的自定义函数:getStringUTFLength和leftUTFString,分别用于获取字符串的UTF长度和截取字符串到指定长度。 - getStringUTFLength函数:通过正则表达式匹配并替换掉字符串中的中文字符(汉字)...

    jsp+md5 加密原文件

    `getMD5ofStr()`看起来是一个自定义的方法,它应该是对输入字符串进行MD5加密处理。 在给定的描述中,`MD5.getMD5ofStr("asssss").substring(8,24)`这段代码显示了如何从一个MD5哈希值中提取一部分。MD5哈希值通常...

    2021-2022计算机二级等级考试试题及答案No.16383.docx

    6. VisualBasic6.0截取字符串:在VB6中,使用`LEFT`函数可以截取字符串的前几个字符。例如,`LEFT("Visual Basic 6.0", 6)`将返回"Visual"。 7. 计算机基本存储单位:计算机存储和处理数据的基本单位是字节(Byte)...

    php面试题库

    处理中文或其他多字节字符时,使用`mb_*`系列函数更为可靠,如`mb_substr()`用于截取字符串而不破坏多字节字符。 ### 11. 变量引用与解引用 PHP中的变量引用允许一个变量成为另一个变量的别名,示例中 `$b` 被设为...

    2021-2022计算机二级等级考试试题及答案No.16289.docx

    使用 `LEFT` 函数可以截取字符串的前几个字符。 ### 25. 索引管理 一个数据库表可以创建多个索引,包括主索引和唯一索引,但每个字段只能有一个主索引。 ### 26. Session 属性获取 在 Java Web 开发中,通过 `...

    java初学者必看

    5.1.3 StringBuffer创建字符串 5.2 连接字符串 5.2.1 与字符串的连接 5.2.2 与其他数据类型的连接 5.3 String字符串操作 5.3.1 基本操作 5.3.2 比较 5.3.3 转化 5.3.4 查找 5.3.5 截取拆分 5.3.6 替换或...

    java学习笔记.pdf

    - **字符串的截取**:使用substring()方法。 - **替换**:使用replace()方法。 - **字符串转化为相应的数值**:使用parseInt()等方法。 - **数值转化为字符串**:使用String.valueOf()方法。 - **对象的字符串...

    java面试题(面试 宝典)

    16. **截取字符串的函数**: - 需要考虑到中文字符占用两个字节,避免截断。 这些面试题涉及了Java的基础知识、Web开发中的JSP和Servlet、J2EE架构以及软件工程等方面,是全面评估Java开发者技能的重要参考。在...

    2021-2022计算机二级等级考试试题及答案No.4484.docx

    21. 字符串截取:`substring(1,3)`从字符串a的第二个字符开始截取,到第三个字符结束,结果为"el"。 22. 广域网缩写:广域网的英文缩写是WAN,LAN是局域网的缩写。 23. C语言输入:在给定的C语言程序中,正确输入1...

    PHP新手面试题总100分

    `mb_substr()`函数用于多字节字符串的截取,适用于处理非ASCII字符集。 #### 10. 版本控制软件的选择和区别 例如,TortoiseSVN 1.2.6和svn 1.2.3,主要区别在于前者是图形界面工具,后者是命令行工具。 #### 11. ...

    Java范例开发大全 (源程序)

     实例42 字符串索引越界异常(StringIndexOutBounds) 60  实例43 操作错误(UnsupportedOperationException) 60  4.2 运行时异常 61  实例44 找不到指定类时发生的异常(ClassNotFoundException) 62  ...

    java范例开发大全(pdf&源码)

    实例42 字符串索引越界异常(StringIndexOutBounds) 60 实例43 操作错误(UnsupportedOperationException) 60 4.2 运行时异常 61 实例44 找不到指定类时发生的异常(ClassNotFoundException) 62 实例45 请求的...

    java范例开发大全源代码

     实例42 字符串索引越界异常(StringIndexOutBounds) 60  实例43 操作错误(UnsupportedOperationException) 60  4.2 运行时异常 61  实例44 找不到指定类时发生的异常(ClassNotFoundException) 62 ...

    java范例开发大全

    实例42 字符串索引越界异常(StringIndexOutBounds) 60 实例43 操作错误(UnsupportedOperationException) 60 4.2 运行时异常 61 实例44 找不到指定类时发生的异常(ClassNotFoundException) 62 实例45 请求的...

    springMVC文件上传与下载的相关jar包

    例如,`fmt`库可以用于日期格式化,`c`库可以进行条件判断和循环,而`fn`库则提供了字符串操作的方法,如截取、替换等,这些在处理文件名或路径时很有用。 实现文件上传的基本步骤: 1. 在Spring MVC配置文件中,...

    JavaScript完全自学宝典 源代码

    3.3.html 截取字符串的子串。 3.4.html 用户自定义的实现slice函数功能的函数。 3.5.html 删除数组最后项的方法。 3.6.html 向数组头添加一个项。 3.7.html 多维数组的实现方法。 第4章(\c04...

    Java范例开发大全(全书源程序)

    实例42 字符串索引越界异常(StringIndexOutBounds) 60 实例43 操作错误(UnsupportedOperationException) 60 4.2 运行时异常 61 实例44 找不到指定类时发生的异常(ClassNotFoundException) 62 实例45 请求...

    java面试综合java面试综合.doc

    15. **字符串截取**:在处理中文字符时,要确保截取时不破坏字符完整性,避免半角字符问题。 16. **Oracle大数据量分页**:通常使用ROWNUM、ROWNUMBER()或分页SQL实现。 17. **Web服务**:Web服务是通过网络提供...

Global site tag (gtag.js) - Google Analytics