自定义标签控制过长字符串的显示

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

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;
	}
}

 

 

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值