1200字范文,内容丰富有趣,写作的好帮手!
1200字范文 > 自定义EL函数 自定义JSTL标签

自定义EL函数 自定义JSTL标签

时间:2019-05-16 07:19:39

相关推荐

自定义EL函数 自定义JSTL标签

自定义EL函数

1.做一个类(静态)

package com.maya.el;public class ELBiaoDaoShi {public static String TiHuan(String s){String txt=s.replaceAll("\"", "&quote;").replaceAll("&","&amp;").replaceAll("<","&lt;").replaceAll(">", "&gt;");return txt;}}

2.配置:

在WEB-INF文件夹下创建后缀名为.tld文件在jstl的jar文件fn中(复制粘贴)

<?xml version="1.0" encoding="UTF-8" ?><taglib xmlns="/xml/ns/j2ee"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/xml/ns/j2ee /xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"version="2.0"><description>自定义EL函数</description> <display-name>自定义函数</display-name><tlib-version>1.0</tlib-version><short-name>fn</short-name><uri>http://com.maya.el/myel</uri><function><description>对于特殊字符的转化</description><name>zh</name><function-class>com.maya.el.ELBiaoDaoShi</function-class><function-signature>String ZhuanHuan(java.lang.String) </function-signature><example></example></function></taglib>

3、导包、调用

<%@page import="java.util.ArrayList"%><%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@ taglib prefix="my" uri="http://com.maya.el/myel" %> <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body>${ my:zh("<h1>hehe</h1>")}</body></html>

输出结果如下

自定义JSTL标签

自定义jstl标签与自定义EL函数相似

做一个类,派生自SimpleTagSupport(继承)

重写doTag()方法 ( a/t + / )

package com.maya.jstl;import java.io.IOException;import javax.servlet.jsp.JspException;import javax.servlet.jsp.tagext.*;public class MyJSTL extends SimpleTagSupport {private int s;public void setS(int s) {this.s = s;}@Overridepublic void doTag() throws JspException, IOException {JspFragment frag=this.getJspBody();//获取标签中的值for(int i=0; i<s; i++){frag.invoke(null);}}}

2、同样是在WEB-INF下建一个.tid配置文件

在WEB-INF文件夹下创建后缀名为.tld文件在jstl的jar文件c中(复制粘贴)

<?xml version="1.0" encoding="UTF-8" ?><taglib xmlns="/xml/ns/javaee"xmlns:xsi="/2001/XMLSchema-instance"xsi:schemaLocation="/xml/ns/javaee /xml/ns/javaee/web-jsptaglibrary_2_1.xsd"version="2.1"><description>自定义的一些jstl标签</description><display-name>自定义jstl</display-name><tlib-version>1.0</tlib-version><short-name>myjstl</short-name><uri>http://com.maya.jstl/myjstl</uri><tag><description></description><name>forxh</name><tag-class>com.maya.jstl.MyJSTL</tag-class><body-content>scriptless</body-content><!--属性 --><attribute><description></description><name>s</name><!--属性名称 --><required>true</required> <!--是否为必须 true为必须 --><rtexprvalue>false</rtexprvalue> <!--是否可以用EL表达式 --></attribute></tag></taglib>

3、引用

<%@ page language="java" contentType="text/html; charset=UTF-8"pageEncoding="UTF-8"%><%@taglib prefix="my" uri="http://com.maya.jstl/myjstl" %><!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "/TR/html4/loose.dtd"><html><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8"><title>Insert title here</title></head><body><my:forxh s="5">a</my:forxh></body></html>

显示结果如下

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。