substringtag.java

来自「移动彩信管理平台」· Java 代码 · 共 75 行

JAVA
75
字号
package com.my7g.zj.mobile.mms.web.tag;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;

/**
 * @author zhangyu
 *
 */
public class SubStringTag extends BodyTagSupport {
	
	/**控制字符长度
	 * 
	 */
	private static final long serialVersionUID = -6994383164478055234L;
	
	protected String name = null; //标签的一个属性
	private int length = 0;
	private String back = "";
	
	public int getLength() {
		return length;
	}
	public void setLength(int length) {
		this.length = length;
	}
	public String getName() {
	      return (this.name);
	}
	public void setName(String name) {
	      this.name = name;
    } 
	public String getBack() {
		return back;
	}
	public void setBack(String back) {
		this.back = back;
	}
	public int doStartTag() throws JspException{
	    // TODO Auto-generated method stub 
	    JspWriter out = pageContext.getOut();
	    try {
	    	List<String> str = new ArrayList<String>(); 
	    	String sname = "";
	    	int strNum = 0;
	    	for (int i = 0; i < this.getName().length(); i++) {
				if (this.getName().substring(i,i+1).getBytes().length == 2){
					strNum += 2;
				}else{
					strNum++;
				}
				if (strNum <= this.getLength() * 2)
					str.add(this.getName().substring(i,i+1));
			}
	    	
	    	for (int i = 0; i < str.size(); i++) {
				sname += str.get(i);
			}
	    	if (this.getName().getBytes().length > this.getLength() * 2)
	    		this.setName(sname + this.getBack());
	        out.append(name);
	        } catch (IOException e) {
	              // TODO Auto-generated catch block
	              e.printStackTrace();
	        }
	    
	    return (SKIP_BODY);
	  }

}

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?