EL函数
1、EL函数的作用:操作字符串
2、在JSP页面中要引入EL函数库
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %>
3、语法
${ fn:方法名(参数) }
4、实例
-
<%@ page language="java" contentType="text/html; charset=UTF-8" -
pageEncoding="UTF-8"%> -
<!-- 引入EL函数库 --> -
<%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %> -
<!-- 引入jstl标签库 --> -
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> -
-
<html> -
<head> -
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> -
<title>Insert title here</title> -
</head> -
<body> -
<!-- 操作域对象中的字符串 --> -
<c:set var="username" value="abcdefg" scope="page"></c:set> -
<h4>EL函数操作page域的内容</h4> -
${ fn:indexOf(pageScope.username,"de") } -
${ fn:startsWith(pageScope.username,"a") } -
${ fn:toUpperCase(username) } -
${ fn:substringAfter(username,"de") } -
${ fn:substringBefore(username,"de") } -
</body> -
</html>

自定义EL函数
步骤:
(1)编写一个java类,提供一个静态方法,方法必须有返回值
-
package com.myel; -
-
/** -
* 自定义EL函数 -
* @author 58351 -
* -
*/ -
public class ELDemo { -
/** -
* say hello 方法 -
* @param str -
* @return -
*/ -
public static String sayHello(String str){ -
return "hello"+str; -
} -
}
(2)在WEB-INF目录下,编写.tld结尾的文件(xml文件),进行配置
- 不要在lib或者classes目录下创建
-
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"> -
-
<tlib-version>1.0</tlib-version> -
<short-name>myfn</short-name> -
<uri>http://www.sutu.com/myfn</uri> -
<!-- 配置自定义EL函数 --> -
<function> -
<!-- 配置方法的名称 --> -
<name>sayHello</name> -
<!-- 配置方法所在的类 --> -
<function-class>com.myel.ELDemo</function-class> -
<!-- 配置方法的签名(描述类中的方法) --> -
<function-signature>java.lang.String sayHello(java.lang.String)</function-signature> -
</function> -
-
-
</taglib>
(3)在JSP页面上引入自己编写的EL函数库
-
<%@ page language="java" contentType="text/html; charset=UTF-8" -
pageEncoding="UTF-8"%> -
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %> -
<!-- 引入自己的EL函数 --> -
<%@ taglib prefix="myfn" uri="http://www.sutu.com/myfn" %> -
-
<html> -
<head> -
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> -
<title>Insert title here</title> -
</head> -
<body> -
-
<h4>使用自己编写的EL函数</h4> -
${ myfn:sayHello("张三") } -
</body> -
</html>
自定义标签
1、自定义标签主要用于移除JSP页面中的java代码,提高代码的复用性
2、开发步骤:
(1)编写一个类,继承SimpleTagSupport类,重写3个方法
(2)在tld文件中,编写配置文件
(3)在 JSP页面中引入自定义的标签库
自定义标签(没有标签主体)
自定义标签的java类
-
package com.myjstl; -
-
import java.io.IOException; -
-
import javax.servlet.jsp.JspContext; -
import javax.servlet.jsp.JspException; -
import javax.servlet.jsp.PageContext; -
import javax.servlet.jsp.tagext.JspFragment; -
import javax.servlet.jsp.tagext.SimpleTagSupport; -
-
/** -
* 自定义标签没有标签主体 -
* @author 58351 -
* -
*/ -
public class JSTLDemo1 extends SimpleTagSupport { -
-
private PageContext pc; -
private JspFragment body; -
-
/** -
* 标签执行了就会调用该方法 -
*/ -
public void doTag() throws JspException, IOException { -
//向页面输出hello -
pc.getOut().write("hello"); -
} -
/** -
* 获取标签主体,默认调用该方法,把JspFragment(标签主体对象)传入进来 -
*/ -
public void setJspBody(JspFragment jspBody) { -
this.body = jspBody; -
} -
/** -
* Tomcat服务器默认先调用该方法,获取pageContext对象 -
*/ -
public void setJspContext(JspContext pc) { -
this.pc = (PageContext) pc; -
} -
-
}
配置文件
-
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"> -
-
<tlib-version>1.0</tlib-version> -
<short-name>myc</short-name> -
<uri>http://www.sutu.com/myc</uri> -
<!-- 配置自定义标签 --> -
<tag> -
<!-- 配置标签名称 --> -
<name>print</name> -
<!-- 自定义标签所在的类 --> -
<tag-class>com.myjstl.JSTLDemo1</tag-class> -
<!-- 配置标签主体 --> -
<body-content>empty</body-content> -
</tag> -
-
</taglib>
JSP页面引入自定义标签库
<%@ taglib prefix="myc" uri="http://www.sutu.com/myc" %>
使用标签
-
<body> -
<!-- 自定义标签 --> -
<!-- 在页面上输出hello --> -
<myc:print/> -
</body>
自定义标签(有标签主体)
-
package com.myjstl; -
-
import java.io.IOException; -
-
import javax.servlet.jsp.JspContext; -
import javax.servlet.jsp.JspException; -
import javax.servlet.jsp.PageContext; -
import javax.servlet.jsp.tagext.JspFragment; -
import javax.servlet.jsp.tagext.SimpleTagSupport; -
/** -
* 自定义标签有标签主体 -
* @author 58351 -
* -
*/ -
public class JSTLDemo2 extends SimpleTagSupport { -
-
private PageContext pc; -
private JspFragment body; -
/** -
* 最后调用该方法,目的:输出标签主体 -
*/ -
public void doTag() throws JspException, IOException { -
//直接调用JspFragment的invoke(Writer out),标签主体向外输出 -
body.invoke(pc.getOut()); -
} -
-
public void setJspBody(JspFragment jspBody) { -
this.body = jspBody; -
} -
-
public void setJspContext(JspContext pc) { -
this.pc = (PageContext) pc; -
} -
-
}
配置文件
-
<!-- 配置自定义标签含有标签主体 --> -
<tag> -
<name>out</name> -
<tag-class>com.myjstl.JSTLDemo2</tag-class> -
<body-content>scriptless</body-content> -
</tag>
带有属性的自定义标签
配置类
-
public class JstlDemo3 extends SimpleTagSupport{ -
private PageContext pc; -
// 代表标签主体 -
private JspFragment jspBody; -
// test和JSP页面上的if test属性名称必须是相同的 -
// 必须给我提供test属性的set方法 -
private boolean test; -
public void setTest(boolean test) { -
this.test = test; -
} -
-
/** -
* 最后调用该方法,目的:输出标签主体,由于属性决定 -
*/ -
public void doTag() throws JspException, IOException { -
// 直接调用JspFragment的invoke(Writer out) ,标签主体向外输出 -
if(test){ -
jspBody.invoke(pc.getOut()); -
} -
} -
-
public void setJspContext(JspContext pc) { -
this.pc = (PageContext) pc; -
} -
-
public void setJspBody(JspFragment jspBody) { -
this.jspBody = jspBody; -
} -
}
配置文件
-
<tag> -
<!-- 配置标签名称 --> -
<name>if</name> -
<!-- 标签使用的类 --> -
<tag-class>cn.itcast.jstl.JstlDemo3</tag-class> -
<!-- 配置标签主体 --> -
<body-content>scriptless</body-content> -
<!-- 配置属性 --> -
<attribute> -
<!-- 配置数据名称 --> -
<name>test</name> -
<!-- 属性是否是必须出现的 --> -
<required>true</required> -
<!-- 支持EL表达式 --> -
<rtexprvalue>true</rtexprvalue> -
<!-- 属性的类型 --> -
<type>boolean</type> -
</attribute> -
</tag>
---------------------
作者:Tommy5553
来源:CSDN
原文:https://blog.csdn.net/Tommy5553/article/details/86362449
版权声明:本文为作者原创文章,转载请附上博文链接!
内容解析By:CSDN,CNBLOG博客文章一键转载插件