自定义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;
} @Override
public 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>
显示结果如下
自定义EL函数、自定义JSTL标签的更多相关文章
- 10.自定义EL函数和自定义标签
需要在JSP页面中进行一些常见逻辑操作(如对字符串进行操作),首先考虑是否可以用到sun公司提供的EL函数库(fn.tld)和JSTL 核心标签库 如果sun公司的EL函数库没有或者无法满足,就需要自 ...
- EL函数和自定义EL函数
简介 EL原本是JSTL1.0中的技术(所以EL和JSTL感情如此好就是自然的了),但是从JSP2.0开始,EL就分离出来纳入了JSP的标准了.但是EL函数还是和JSTL技术绑定在一起.下面将介绍如何 ...
- 【JSP】EL函数和自定义EL函数
简介 EL原本是JSTL1.0中的技术(所以EL和JSTL感情如此好就是自然的了),但是从JSP2.0开始,EL就分离出来纳入了JSP的标准了.但是EL函数还是和JSTL技术绑定在一起.下面将介绍如何 ...
- 自定义el函数
1.1.1 自定义EL函数(EL调用Java的函数) 第一步:创建一个Java类.方法必须是静态方法. public static String sayHello(String name){ retu ...
- [JSP]自定义EL函数以及使用
有时候在JSP页面需要进行一连串的字符串的处理,需要进行自定义EL函数. 先看EL函数的tld文件: standard.jar下面: 自定义EL函数: 1.编写EL函数(全是public static ...
- EL表达式、JSTL标签库
一.EL(Expression Language)表达式 语法结构:${var} 若要停用对EL表达式的评估的话,需要使用page指令将isELIgnored属性值设为true: <%@ pag ...
- JSP、EL表达式、JSTL标签库干货(建议收藏)
JSP(Java Server Pages)类似于ASP技术,它是在传统的网页HTML文件(.htm,.html)中插入Java程序段(Scriptlet)和JSP标记(tag),从而形成JSP文件, ...
- jsp EL表达式和JSTL标签if-else if-else用法
java web javaEE ,jsp EL表达式和JSTL标签if-else if-else四种用法一.条件运算符 ${user.gender==?'男':'女'} 二.if() <c:if ...
- (十)EL表达式和JSTL标签快速入门
目录 什么是 el 表达式 示例:使用EL表达式获取pageContext .request.session.application域中的数据: 示例:使用EL表达式获取Bean属性 获取当前WEB应 ...
随机推荐
- PHP将多级目录打包成zip文件
最近接触PHP,需要用到zip压缩,在网上搜索的一大堆,发现代码都不低于50行. 而且调用还很费事(基础太少看不懂).让我收获的是Php提供有一个ZipArchive类,并有如下方法. bool a ...
- xfs 文件系统损坏修复 fscheck
- billboard因为合批导致出问题的一个想法
由于unity中距离较近的2个billboard物体会动态合批,如果缩放不同,显示就有问题.还得在shader中"DisableBatching"="true" ...
- Zxing二维码解析——图文转换
一:文字转化为二维码图片. package com.xhm.tool; import java.util.Hashtable; import android.graphics.Bitmap; impo ...
- C语言合并两个集合(L,L1) 将L1中不在L中的元素插入到L线性表中
void main(){ Sqlist L,L1; InitList(&L); InitList(&L1); ListInsert(&L, 1, 2); ListInsert( ...
- PHP安全编程之php.ini配置
1.register_globals=On <?php//ex1.php if(check_admin()) { $is_admin=true; } if($is_admin) { ...
- ip地址设备信息
其实是个小问题,在项目中遇到要获取手机ip地址和mac地址的问题,mac地址获取用appcan的uexDevice插件可以实现. 但是ip地址,获取的方式很多,具体有搜狐/腾讯/新浪等提供的接口.但是 ...
- 创建美国地区的appleId
参考: https://zhuanlan.zhihu.com/p/36574047 美国人身份信息生成: https://www.fakeaddressgenerator.com/Index/in ...
- 导入android sdk samples工程报错"did you mean to use @+id instead of @+android:id?"
导入“D:\adt-bundle-windows-x86_64-20140702\sdk\samples\android-15”中的工程报错 did you mean to use @+id inst ...
- INT(M)表示什么意思?
根据官方文档描述,int(M)中的M表示数据显示的宽度,与实际存储的长度无关. 1.也就是int(3)和int(11)能够存储的数据是一样的,都是从-2147483648到2147483647(或者0 ...