自定义 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<median;i++){
	 	    		 replacement=replacement+temp; 
	 	    	 }
	    	  }
	      };
	      formatted = strBuf.replace(start, end, replacement).toString();
	<span style="white-space:pre">	</span>if (var != null) {
		    pageContext.setAttribute(var, formatted, scope);	
		} 
	    <span style="white-space:pre">	</span>try {
	    	<span style="white-space:pre">	</span>pageContext.getOut().print(formatted);
	    <span style="white-space:pre">	</span>} catch (IOException ioe) {
	    	<span style="white-space:pre">	</span>throw new JspTagException(ioe.toString(), ioe);
	    <span style="white-space:pre">	</span>}
               init();
		return EVAL_PAGE;
	}

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

<?xml version="1.0" encoding="UTF-8"?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
    version="2.1">
	<description>okttl Tag Library</description>
	<tlib-version>1.0</tlib-version>
	<short-name>str</short-name>
	<tag>
		<!-- 标签名称 -->
        <name>replace</name>        
        <description>
	      Replacement string starting index position to the end position of the character index
	    </description>
	    <!-- java类的全名 -->
        <tag-class>cn.anscm.taglibs.StringReplaceTag</tag-class>   
         <body-content>empty</body-content>
		 <attribute>
		        <description>
		        	value 要被替换的对象,
		        </description>
		        <name>value</name>
		        <required>false</required>
		        <rtexprvalue>true</rtexprvalue>
		        <deferred-value>
			    	<type>java.lang.Object</type>
		        </deferred-value>
		 </attribute>
         <attribute>
	        <description>
				start - The beginning index, inclusive.\r\n
				起始索引(包含)。
	        </description>
	        <name>start</name>
	        <type>int</type>
	        <required>false</required>
	        <rtexprvalue>false</rtexprvalue>
	     </attribute>
	     <attribute>
	        <description>
				end - The ending index, exclusive.\r\n
				结束索引(不包含)。
	        </description>
	        <name>end</name>
	        <type>int</type>
	        <required>false</required>
	        <rtexprvalue>false</rtexprvalue>
	     </attribute>
	     <attribute>
	        <description>
				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" 的属性值是单个字符时,有效
				默认值是原字符索引间的位数。
	        </description>
	        <name>median</name>
	        <type>int</type>
	        <required>false</required>
	        <rtexprvalue>false</rtexprvalue>
	     </attribute>
	     <attribute>
	        <description>
				replacement symbol or character or string.
				default "replacement" is a single character \r\n
				被替换为的字符,符号,或字符串
	        </description>
	        <name>replacement</name>
	        <required>false</required>
	        <rtexprvalue>false</rtexprvalue>
	     </attribute>
         <attribute>
	        <description>
				Name of the exported scoped variable
				which stores the formatted result as a
				String.\r\n
				变量名
	        </description>
	        <name>var</name>
	        <required>false</required>
	        <rtexprvalue>false</rtexprvalue>
	    </attribute>
	    <attribute>
	        <description>
			Scope of var. \r\n
			变量作用域 
			可选值 request,sessioin,application
	        </description>
	        <name>scope</name>
	        <required>false</required>
	        <rtexprvalue>false</rtexprvalue>
	    </attribute>
    </tag>
</taglib>
jsp 文件 safe.jsp
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="str" uri="/WEB-INF/str.tld" %>
<!DOCTYPE html>
<html >
<head>
<str:replace value="身份证号" start="1" end="3"   />
</head>
<body>
</html >
其它属性自己测试

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值