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

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

Java代码
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)+"...";   
                }   
                   
                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;   
    }   

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)+"...";
}

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

你可能感兴趣的:(jsp,servlet,css)