jstl三目运算:前台页面截取字符串

通常像标题之类的,如果后台输入过长,那么前台完整输出,必然在格式上造成多行,会显得很难看,当然如果事先对该行进行了长度和高度的控制,加上了一句“overflow:hidden”,那又另当别论了。

       截取的方式自然是通过${fn:substring()}函数,那么做法如下:      

1
2
< c:if  test="${fn:length(article.title)>21"}>${fn:substring(article.title,0,21)}....</ c:if >
< c:if  test="${fn:length(article.title)<=21"}>${article.title}</ c:if >

       这样写未尝不可,其实有一个更简单的输出方式,用到的方法就是三目运算法,代码如下:

1
${fn:length(article.title)>21 ? fn:substring(article.title,0,21) : article.title}${fn:length(article.title)>21 ? '...' : ''}

       代码相比之下,简洁了很多。。

       ${fn:length()}这个标签很强大,因为它不止可以计算字符串的长度,还可以计算从后台传过来的list对象的长度,一开始还真不知道,下午做项目时就碰到这个问题了,为此卡了一下。

       ${fn:substring()}这个标签,我觉得他对于中英文字符串的处理不是太好,它将汉字和英文字符都当成是1个字节,在截取的时候,有时候得到的效果往往不是我们想的,为此最好的解决方法是自己写个标签,当然如果后台输出的都是汉字,那么干脆用这个标签来截取,也很方便的。

       中英文截取字符串标签的java代码如下,这是我从javaeye上拷贝过来的,在实际的项目中通过。

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package  com.test.mytag;
 
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 {
     /**
      *
      */
     private  static  final  long  serialVersionUID = 1L;
 
     /**
      * 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;
     }
}

 

 

        标签的tld文件代码如下: 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
<? xml  version="1.0" encoding="iso-8859-1"?>
<! DOCTYPE  taglib
PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
< 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 >

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值