當前位置:
                    首頁 >
                            前端技术
>                            javascript
>内容正文                
                        
                    javascript
自定义EL函数、自定义JSTL标签
                                                            生活随笔
收集整理的這篇文章主要介紹了
                                自定义EL函数、自定义JSTL标签
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.                        
                                自定義EL函數
1.做一個類(靜態)
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文件夾下創建后綴名為 ?.tld ??文件在jstl的jar文件fn中(復制粘貼)
<?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函數</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" "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>輸出結果如下
?
自定義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="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標簽</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" "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>顯示結果如下
?
轉載于:https://www.cnblogs.com/zhaotiancheng/p/6393708.html
總結
以上是生活随笔為你收集整理的自定义EL函数、自定义JSTL标签的全部內容,希望文章能夠幫你解決所遇到的問題。
 
                            
                        - 上一篇: 第七课、Qt中的坐标系统--------
- 下一篇: 【笔记】metasploit渗透测试魔鬼
