
<%@page import="am_cn.itcast.domain.UrlOpertation"%> |
<%@ page language="java" contentType="text/html; charset=UTF-8" |
pageEncoding="UTF-8"%> |
|
<!-- 首先 要 导入 标签库 , taglib --> |
<%@ taglib uri="http://www.itcast.cn/myel" prefix="itcast"%> |
<!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> |
<% |
String value = UrlOpertation.encodeContent("廖海林"); |
|
out.print(value); |
%> |
|
<%-- 需求, 通过 el 表达式 ,实现 上面 同样的 功能 |
通过 自定义 el 函数 来实现了 . |
|
实现 步骤 : |
1. 要实现的 那个 java 类的 那个 方法 必须 要 是 静态的 . |
|
|
2. 要通过 tld 文件 来 描述 el 函数 与 java 类的方法 对应 关系 |
在 WEB-INF目录下建一个 tld文件, 这个tld文件不能 放到 classes目录或者 lib 目录 |
|
<?xml version="1.0" encoding="UTF-8"?> |
<taglib version="2.0" 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"> |
<tlib-version>1.0</tlib-version> |
<short-name>itcast</short-name> |
<uri>http://www.itcast.cn/myel</uri> |
|
可以 参考 , tomcat的 webapps目录下 : D:\tomcat\apache-tomcat-7.0.40\webapps\examples\WEB-INF\jsp2 的 tld文件 |
|
3.建立其映射关系, el函数名称与 java 类的 方法的对应关系 |
<function> |
<!-- 表示 这个 el 函数 对外 提供 一个 调用 名称 --> |
<name>encode</name> |
<!-- 建立其 类 与 encode 函数的 映射 关系 --> |
<function-class>am_cn.itcast.domain.UrlOpertation</function-class> |
<function-signature>String encodeContent(java.lang.String)</function-signature> |
</function> |
4. 在jsp中 导入 自定义的 标签库 . 通过 taglib 导入 . |
|
直接使用了 . |
--%> |
<hr/> |
${itcast:encode("廖海林") } |
</body> |
</html> |



