以前发过一篇文章,最开始采用的是css控制width的方式控制标题的长度。这种方式的弱点就是:会出现截取半个字,而且FIERFOX下并不兼容。
静下心来写了一个自定义标签,用代码的方式控制长度,这个方式最精准不过了!
首先是tag源文件:
package cn.ryan.commons.web.taglib;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
import org.apache.log4j.Logger;
/**
* 截断字符串并以制定符号替代的tag
*
* @author ryankay.xiang@gmail.com
*
*/
public class CutStringTag extends TagSupport {
/**
* Logger for this class
*/
private static final Logger logger = Logger.getLogger(CutStringTag.class);
String value;
String mark="";
Integer size;
@Override
public int doStartTag() throws JspException {
return SKIP_BODY;
}
@Override
public int doEndTag() throws JspException {
String html = cutString(value, size,mark);
try {
this.pageContext.getOut().write(html.toString());
} catch (IOException e) {
logger.error("tag CutStringTag error", e);
}
return EVAL_PAGE;
}
public String cutString(String str, int len,String mark) {
len = len * 2;
StringBuffer sb = new StringBuffer();
int counter = 0;
for (int i = 0; i < str.length(); i++) {
char c = str.charAt(i);
if (c < 255) {
counter++;
} else {
counter = counter + 2;
}
if (counter > len) {
String result=sb.toString().trim();
result+=mark;
return result;
}
sb.append(c);
}
return sb.toString();
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getMark() {
return mark;
}
public void setMark(String mark) {
this.mark = mark;
}
public Integer getSize() {
return size;
}
public void setSize(Integer size) {
this.size = size;
}
}
标签描述文件:site.tld
<?xml version="1.0" encoding="iso-8859-1"?> <!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd"> <taglib> <tlib-version>1.0</tlib-version> <jsp-version>1.2</jsp-version> <short-name>site</short-name> <uri>http://www.collin.cn/taglib/site</uri> <display-name>Collin Site</display-name> <description></description> <tag> <name>cutString</name> <tag-class>cn.ryan.commons.web.taglib.CutStringTag</tag-class> <body-content>empty</body-content> <description>Used to cut a string with your parameter.</description> <attribute> <name>value</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <description>Required to set the string what you want to cut.</description> </attribute> <attribute> <name>size</name> <required>true</required> <rtexprvalue>true</rtexprvalue> <description>Required to set the size or length and it compute as chinese.</description> </attribute> <attribute> <name>mark</name> <required>false</required> <rtexprvalue>true</rtexprvalue> <description>Write something append to the string or you can use the default ''.</description> </attribute> </tag> </taglib>
web.xml
<?xml version="1.0" encoding="UTF-8"?> <web-app id="WebApp_ID" version="2.4" 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-app_2_4.xsd"> <display-name>example</display-name> <description>example</description> <jsp-config> <taglib> <taglib-uri>http://www.collin.cn/taglib/site</taglib-uri> <taglib-location>/WEB-INF/site.tld</taglib-location> </taglib> </jsp-config> </web-app>
使用方法:
<site:cutString size="10" value="我们是共产主义的接班人" mark="..."/>
<site:cutString size="10" value="123456789,,我们是共产主义的接班人" mark="..."/>
<site:cutString size="10" value="123456789,,我们是共产主义的接班人"/>
注释:
1.mark标签制定以什么符号后缀,也可以不填,默认为""。
2.size指定限制的字符数,注意这里是指中文的字数,以中文为单位的好处是防止截取半个字。
预览效果:
http://www.wibowibo.com
当然也有其他方法控制,不过这种方式个人觉得用起来比较灵活的,而且兼容各个浏览器【这是肯定的了,呵呵】。
欢迎拍砖,呵呵。