自己写的标题截取长度加“...”显示标签
网络上的提供的大多数截取长度的方法放到jsp页面中总是感觉比较繁琐,参考网友的代码,自己写了一个标签,大家可以参考一下
?
?
package com.abc.zj.framework.tag;
import java.io.IOException;
import java.text.MessageFormat;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.TagSupport;
import com.abc.zj.framework.action.BaseResultModel;
public class SubString extends TagSupport
{
?private String value;
?private int len;
?private String suffix;
?
??? public int getLen() {
??return len;
?}
?public void setLen(int len) {
??this.len = len;
?}
?public String getValue() {
??return value;
?}
?public String getSuffix() {
??return suffix;
?}
?public void setSuffix(String suffix) {
??this.suffix = suffix;
?}
?public void setValue(String value) {
??this.value = value;
?}
?public SubString()
??? {
??value="";
??len=0;
??suffix= "";
??? }
??? public int doStartTag()
??????? throws JspException
??? {
??????? return 0;
??? }
??? public int doEndTag()
??????? throws JspException
??? {
??????? JspWriter out = super.pageContext.getOut();
??????? try
??????? {
??????? ?String temp = getLimitStr(value, len);
??????? ?if(value.equals(temp)){
??????? ??out.print(temp);
??????? ?}else{
??????? ??out.print(temp+suffix);
??????? ?}
??????? }
??????? catch(IOException e)
??????? {
??????????? throw new JspException(e.getMessage());
??????? }
??????? catch(Exception e)
??????? {
??????????? throw new JspException(e.getMessage());
??????? }
??????? return 6;
??? }
?/**
? * 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,
? * 输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。
? *
? * @param args
? */
?public static void main(String args[]) {
??String s = "一般收文单独:手工登记状态测试111111";
??int len = 40;
//??SplitStr(s, len);
??System.out.print(getLimitStr(s,len));
?}
?public static String getLimitStr2(String str, int len) {
??String result = "";
??char temp;
??// 取得的字节数
??int counter = 0;
??int i = 0;
??// 汉字个数
??int han = 0;
?
int strLen = str.getBytes().length;
??if(strLen<len){
???len = strLen;
??}
??while (counter < len) {
???temp = str.charAt(i);
???System.out.println(i);
???if (Character.getNumericValue(temp) != -1) {
????// 遇到字母的时候
????result = result + temp;
????counter++;
????i++;
???} else {
????// 遇到汉字的时候,作为unicode字符,汉字的整数值是-1
????result = result + temp;
????counter = counter + 2;
????i++;
????han = han + 1;
???}
??}
??if (counter > len) {
???if (len == 1) {
????result = "";
???} else {
????result = result.substring(0, counter - (han + 1));
???}
??}
??return result;
??//System.out.println("result?????? " + result);
?}
?
?
?public static String getLimitStr( String str,int len){
??????? try{
??????????? if(str==null)???? return"";????????
??????????? int counterOfDoubleByte = 0;
??????????? byte[] b = str.getBytes("GBK");
???????????
??????????? if(b.length <= len)
?????????????? return str;
???????????
??????????? for(int i = 0; i < len; i++){??????????
??????????????? if(b[i] < 0)
???????????????? counterOfDoubleByte++;
?????????? }???
??????????? if(counterOfDoubleByte % 2 == 0)
?????????????? return new String(b, 0, len, "GBK");
??????????? else
?????????????? return new String(b, 0, len - 1, "GBK");
???????? }catch(Exception ex){??????
??????? return"";
??????? }
??? }
}
?
?
abc.tld
?
?
<?xml version="1.0" encoding="utf-8" ?>
<!DOCTYPE taglib PUBLIC
??? "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
??? "http://java.sun.com/dtds/web-jsptaglibrary_1_2.dtd" >
<taglib>
?<tlib-version>1.0</tlib-version>
?<jsp-version>1.2</jsp-version>
?<short-name>abc Tags</short-name>
?<description>
??Tag Library of abc Web Application Framework
?</description>
?<tag>
??<name>page</name>
??<tag-class>com.abc.zj.framework.tag.Page</tag-class>
??<description>
???Render page navigation bar in jsp file.
??</description>
??<attribute>
???<name>formName</name>
???<required>false</required>
???<rtexprvalue>false</rtexprvalue>
??</attribute>
??<attribute>
???<name>className</name>
???<required>true</required>
???<rtexprvalue>true</rtexprvalue>
??</attribute>
??<attribute>
???<name>noForm</name>
???<required>true</required>
???<rtexprvalue>true</rtexprvalue>
???<type>java.lang.Boolean</type>
??</attribute>
?</tag>
??<tag>
??<name>subString</name>
??<tag-class>com.abc.zj.framework.tag.SubString</tag-class>
??<description>
???SubString for like abcdefg...
??</description>
??<attribute>
???<name>value</name>
???<required>true</required>
???<rtexprvalue>true</rtexprvalue>
??</attribute>
??<attribute>
???<name>len</name>
???<required>false</required>
???<rtexprvalue>true</rtexprvalue>
???<type>java.lang.Integer</type>
??</attribute>
??<attribute>
???<name>suffix</name>
???<required>false</required>
???<rtexprvalue>true</rtexprvalue>
??</attribute>
?</tag>
</taglib>
代码就那么多,考回去改成自己想要的名字就可以用
?