自定义 jstl 标签

 java 类 StringReplaceTag.java
import java.io.IOException;

import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspTagException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.BodyTagSupport;

import org.apache.taglibs.standard.tag.common.core.Util;

/**
 * 自定义jstl 标签 字符串替换
 * @author okttl
 *
 */
public class StringReplaceTag extends BodyTagSupport{
	
	private static final long serialVersionUID = 1L;
	private static final  String REPLACEMENT_DEFAULT = "*";
	private Object value;
	private String var;
	private Integer start;
	private Integer end;
	private String replacement;
	private int scope;  
	private int median; //位数
	
	/**
	 * 设置值
	 * @param value
	 * @throws JspTagException
	 */
	public void setValue(Object value) throws JspTagException {
	     this.value = value;
	}
	
	/**
	 * 设置变量名
	 * @param var
	 */
	public void setVar(String var) {
	     this.var = var;
	}
	
	/**
	 * 设置开始索引
	 * @param start
	 */
	public void setStart(Integer start) {
		this.start = start;
	}

	/**
	 * 设置结束索引
	 * @param end
	 */
	public void setEnd(Integer end) {
		this.end = end;
	}

	/**
	 * 设置替换字符 默认 为*
	 * @param replacement
	 */
	public void setReplacement(String replacement) {
		this.replacement = replacement;
	}
	
	/**
	 * 设置作用域 可取值
	 * request
	 * session
	 * application
	 * @param scope
	 */
	public void setScope(String scope) {
		this.scope = Util.getScope(scope);
	}
	
	/**
	 * 设置显示替换符位数 当替换符为单个字符时有效
	 * @param median
	 */
	public void setMedian(int median) {
		this.median = median;
	}

	/**
	 * 默认构造方法
	 */
	public StringReplaceTag() {
		super();
		init();
	}
	
	/**
	 * 初始化方法
	 */
	private void init(){
		start = end = 0;
		median = -1;
		replacement = REPLACEMENT_DEFAULT;
		scope = PageContext.PAGE_SCOPE;
	} 

	
	@Override
	public int doEndTag() throws JspException {
		String formatted = "";
		 if (value == null){ 
			formatted = "";
		 }
		 formatted = value.toString(); 
		  int l = formatted.length();
	      if(start == null){
	    	  start = 0; 
	      }else if(start < 0){
	    	  start = l;
	      }else if(start >= l){
	    	  start = l;
	      }
	      if(end == null){
	    	  end = l;
	      }else if(end < 0){
	    	  end = 0;
	      }else if(end > l){
	    	  end = l;
	      }
	      StringBuffer strBuf = new StringBuffer(formatted);
	      if(replacement.length()==1){
	    	  if(median==-1 && end > start){
	    		  median = end - start;
	    	  }
	    	  if(median>0){
	    		  String temp = replacement;
	    		  replacement = "";
	    		  for(int i=0;i	if (var != null) {
		    pageContext.setAttribute(var, formatted, scope);	
		} 
	    	try {
	    		pageContext.getOut().print(formatted);
	    	} catch (IOException ioe) {
	    		throw new JspTagException(ioe.toString(), ioe);
	    	}
               init();
		return EVAL_PAGE;
	}

tld 文件 str.tld 放到WEB-INF下



	okttl Tag Library
	1.0
	str
	
		
        replace        
        
	      Replacement string starting index position to the end position of the character index
	    
	    
        cn.anscm.taglibs.StringReplaceTag   
         empty
		 
		        
		        	value 要被替换的对象,
		        
		        value
		        false
		        true
		        
			    	java.lang.Object
		        
		 
         
	        
				start - The beginning index, inclusive.\r\n
				起始索引(包含)。
	        
	        start
	        int
	        false
	        false
	     
	     
	        
				end - The ending index, exclusive.\r\n
				结束索引(不包含)。
	        
	        end
	        int
	        false
	        false
	     
	     
	        
				It is replaced with the number of bits.  The default value is the number of bits of character.
				When the replacement is effective as a single character \r\n
				被替换成的字符位数,当"replacement" 的属性值是单个字符时,有效
				默认值是原字符索引间的位数。
	        
	        median
	        int
	        false
	        false
	     
	     
	        
				replacement symbol or character or string.
				default "replacement" is a single character \r\n
				被替换为的字符,符号,或字符串
	        
	        replacement
	        false
	        false
	     
         
	        
				Name of the exported scoped variable
				which stores the formatted result as a
				String.\r\n
				变量名
	        
	        var
	        false
	        false
	    
	    
	        
			Scope of var. \r\n
			变量作用域 
			可选值 request,sessioin,application
	        
	        scope
	        false
	        false
	    
    
jsp 文件 safe.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="str" uri="/WEB-INF/str.tld" %>







其它属性自己测试

你可能感兴趣的:(jstl,jsp,自定义标签)