當(dāng)前位置:
首頁(yè) >
前端技术
> javascript
>内容正文
javascript
自定义EL函数、自定义JSTL标签
生活随笔
收集整理的這篇文章主要介紹了
自定义EL函数、自定义JSTL标签
小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.
自定義EL函數(shù)
1.做一個(gè)類(lèi)(靜態(tài))
package com.maya.el;public class ELBiaoDaoShi {public static String TiHuan(String s){String txt=s.replaceAll("\"", ""e;").replaceAll("&","&").replaceAll("<","<").replaceAll(">", ">");return txt;}}2.配置:
在WEB-INF文件夾下創(chuàng)建后綴名為 ?.tld ??文件在jstl的jar文件fn中(復(fù)制粘貼)
<?xml version="1.0" encoding="UTF-8" ?> <taglib xmlns="http://java.sun.com/xml/ns/j2ee"xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"version="2.0"><description>自定義EL函數(shù)</description> <display-name>自定義函數(shù)</display-name><tlib-version>1.0</tlib-version><short-name>fn</short-name><uri>http://com.maya.el/myel</uri><function><description>對(duì)于特殊字符的轉(zhuǎn)化</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、導(dǎo)包、調(diào)用
<%@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" "http://www.w3.org/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>輸出結(jié)果如下
?
自定義JSTL標(biāo)簽
自定義jstl標(biāo)簽與自定義EL函數(shù)相似
做一個(gè)類(lèi),派生自SimpleTagSupport(繼承)
?
重寫(xiě)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(); //獲取標(biāo)簽中的值for(int i=0; i<s; i++){frag.invoke(null); }}}?
2、同樣是在WEB-INF下建一個(gè) ? .tid ? 配置文件
在WEB-INF文件夾下創(chuàng)建后綴名為 ?.tld ??文件在jstl的jar文件c中(復(fù)制粘貼)
<?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>自定義的一些jstl標(biāo)簽</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> <!--屬性名稱(chēng) --><required>true</required> <!--是否為必須 true為必須 --><rtexprvalue>false</rtexprvalue> <!--是否可以用EL表達(dá)式 --></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" "http://www.w3.org/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>顯示結(jié)果如下
?
轉(zhuǎn)載于:https://www.cnblogs.com/zhaotiancheng/p/6393708.html
總結(jié)
以上是生活随笔為你收集整理的自定义EL函数、自定义JSTL标签的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 第七课、Qt中的坐标系统--------
- 下一篇: 【笔记】metasploit渗透测试魔鬼