jstl三目运算:前台页面截取字符串
摘要: 通常像标题之类的,如果后台输入过长,那么前台完整输出,必然在格式上造成多行,会显得很难看,当然如果事先对该行进行了
长度和高度的控制,加上了一句“overflow:hidden”,那又另当别论了。 截取的方式自然是通过 ...
通常像标题之类的,如果后台输入过长,那么前台完整输出,必然在格式上造成多行,会显得很难看,当然如果事先对该行进行了
长度和高度的控制,加上了一句“overflow:hidden”,那又另当别论了。
截取的方式自然是通过${fn:substring()}函数,那么做法如下:
代码相比之下,简洁了很多。。
${fn:length()}这个标签很强大,因为它不止可以计算字符串的长度,还可以计算从后台传过来的list对象的长度,
一开始还真不知道,下午做项目时就碰到这个问题了,为此卡了一下。
${fn:substring()}这个标签,我觉得他对于中英文字符串的处理不是太好,它将汉字和英文字符都当成是1个字节, 在截取的时候,有时候得到的效果往往不是我们想的,为此最好的解决方法是自己写个标签,当然如果后台输出的都是汉字, 那么干脆用这个标签来截取,也很方便的。 中英文截取字符串标签的java代码如下,这是我从javaeye上拷贝过来的,在实际的项目中通过。
标签的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>com.test.mytag.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> 原文出处:http://www.189works.com/article-7120-1.html |