自定义 jstl 标签
来源:互联网 发布:域名不备案能用吗 编辑:程序博客网 时间:2024/04/20 05:30
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;} @Overridepublic 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 variablewhich stores the formatted result as aString.\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 >其它属性自己测试
0 0
- JSTL自定义标签问题
- 自定义jstl标签库
- JSTL - 自定义标签
- JSTL自定义标签
- jstl自定义标签
- JSTL自定义标签
- JSTL自定义标签
- JSTL自定义标签
- 自定义JSTL标签
- JSTL自定义标签
- JSTL 自定义标签
- jstl自定义标签
- 自定义jstl标签库
- 自定义JSTL标签
- jstl自定义标签
- jstl之自定义标签
- 自定义标签JSTL+国际化
- 自定义JSTL标签
- Python学习笔记
- 你看起来很厉害,所以一事无成
- CSS权威指南-伪元素选择器
- lazy变量与双重检测锁(DCL)
- js面向对象
- 自定义 jstl 标签
- 第一次打字测试小结
- xml解析器
- 234. Palindrome Linked List
- JAXP-Java API for XML Processing
- java swing实现的学生教师选课管理系统代码
- 范数的物理意义
- cocos-lua 表
- 微信上传图片下载图片示例