浏览 2121 次
锁定老帖子 主题:自定义标签控制过长字符串的显示
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
|
|
---|---|
作者 | 正文 |
发表时间:2008-06-27
最后修改:2010-05-26
问题描述: import javax.servlet.jsp.JspException; import javax.servlet.jsp.JspTagException; import javax.servlet.jsp.tagext.BodyContent; import javax.servlet.jsp.tagext.BodyTagSupport; public class TagTextLimit extends BodyTagSupport{ private int total = 0; private int numOfRow = 0; public int doEndTag()throws JspException{ try{ if(bodyContent!=null){ String str = bodyContent.getString(); String result = ""; if( numOfRow!= 0){ String[] temp = str.split(" "); for(int i = 0;i<temp.length;i++){ if(temp[i].length() > numOfRow){ String str1 = temp[i].substring(0,numOfRow-1)+"-"; String str2 = temp[i].substring(numOfRow-1); while(str2.length()>numOfRow-1){ str1 += str2.substring(0,numOfRow-1)+"-"; str2 = str2.substring(numOfRow-1); } temp[i] = str1.concat(str2); } result += temp[i]+" "; } str = result; } if(total!=0){ result = str.substring(0, total)+"..."; }else{ result = str; } if(numOfRow == 0 && total == 0){ bodyContent.writeOut(bodyContent.getEnclosingWriter()); }else{ pageContext.getOut().write(result); } } }catch(java.io.IOException ex){ throw new JspTagException("IOError:"+ex.getMessage()); } return EVAL_PAGE; } public int doStartTag() throws JspException { return EVAL_BODY_TAG; } public int doAfterBody() throws JspException { return SKIP_BODY; } public void doInitBody() throws JspException { super.doInitBody(); } public void setBodyContent(BodyContent content) { this.bodyContent = content; } public int getNumOfRow() { return numOfRow; } public void setNumOfRow(int numOfRow) { this.numOfRow = numOfRow; } public int getTotal() { return total; } public void setTotal(int total) { this.total = total; } }
声明:ITeye文章版权属于作者,受法律保护。没有作者书面许可不得转载。
推荐链接
|
|
返回顶楼 | |