Chinaunix首页 | 论坛 | 博客
  • 博客访问: 29958777
  • 博文数量: 708
  • 博客积分: 12163
  • 博客等级: 上将
  • 技术积分: 8240
  • 用 户 组: 普通用户
  • 注册时间: 2007-12-04 20:59
文章分类

全部博文(708)

分类: Java

2008-12-15 11:06:11

问题描述:
关于自动换行问题,正常字符的换行是比较合理的,而连续的数字和英文字符常常将容器撑大,挺让人头疼。
我们可以通过CSS控制自动换行,但效果并不是很好,查了一些资料也没有找的好的现成的控件,于是我编写了一个自定义标签解决这个问题,可能其中还存在一些问题,也不是很完善,我会虚心听从指教。
关于自定义标签的理论可参看:JSP2的自定义标签这片贴子(引用的)。

Java代码
  1. import javax.servlet.jsp.JspException;   
  2. import javax.servlet.jsp.JspTagException;   
  3. import javax.servlet.jsp.tagext.BodyContent;   
  4. import javax.servlet.jsp.tagext.BodyTagSupport;   
  5.   
  6. public class TagTextLimit extends BodyTagSupport{   
  7.     private int total = 0;   
  8.        
  9.     private int numOfRow = 0;   
  10.        
  11.     public int doEndTag()throws JspException{   
  12.         try{   
  13.             if(bodyContent!=null){   
  14.                 String str = bodyContent.getString();   
  15.                 String result = "";   
  16.                 if( numOfRow!= 0){   
  17.                     String[] temp = str.split(" ");   
  18.                        
  19.                     for(int i = 0;i
  20.                         if(temp[i].length() > numOfRow){   
  21.                                 String str1 = temp[i].substring(0,numOfRow-1)+"-";   
  22.                                 String str2 = temp[i].substring(numOfRow-1);   
  23.                                    
  24.                                 while(str2.length()>numOfRow-1){   
  25.                                     str1 += str2.substring(0,numOfRow-1)+"-";   
  26.                                     str2 = str2.substring(numOfRow-1);   
  27.                                 }   
  28.                                    
  29.                                 temp[i] = str1.concat(str2);                           
  30.                         }   
  31.                         result += temp[i]+" ";                         
  32.                     }   
  33.                     str = result;   
  34.                 }   
  35.                    
  36.                    
  37.                 if(total!=0){   
  38.                     result = str.substring(0, total)+"...";   
  39.                 }   
  40.                    
  41.                 if(numOfRow == 0 && total == 0){   
  42.                     bodyContent.writeOut(bodyContent.getEnclosingWriter());   
  43.                 }else{   
  44.                     pageContext.getOut().write(result);   
  45.                 }   
  46.                    
  47.             }   
  48.         }catch(java.io.IOException ex){   
  49.             throw new JspTagException("IOError:"+ex.getMessage());   
  50.         }   
  51.         return EVAL_PAGE;   
  52.     }   
  53.        
  54.     public int doStartTag() throws JspException {   
  55.         return EVAL_BODY_TAG;   
  56.     }   
  57.   
  58.     public int doAfterBody() throws JspException {   
  59.         return SKIP_BODY;   
  60.     }   
  61.   
  62.     public void doInitBody() throws JspException {   
  63.         super.doInitBody();   
  64.     }   
  65.        
  66.     public void setBodyContent(BodyContent content) {   
  67.         this.bodyContent = content;   
  68.     }   
  69.        
  70.     public int getNumOfRow() {   
  71.         return numOfRow;   
  72.     }   
  73.   
  74.     public void setNumOfRow(int numOfRow) {   
  75.         this.numOfRow = numOfRow;   
  76.     }   
  77.   
  78.     public int getTotal() {   
  79.         return total;   
  80.     }   
  81.   
  82.     public void setTotal(int total) {   
  83.         this.total = total;   
  84.     }   
  85. }  

 

 

  • (12.9 KB)
  • 描述: 自定义标签控制过长字符串的显示的例子(web项目)
阅读(1632) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~