好长时间都没有写代码了,感觉啥都忘了,咋办啊。
今天要自定义jstl标签,仿照struts2的<s:token/>标签,在页面中生成token,从而防止表单重复提交,下面就记录下如何自定义jstl标签,jstl标签其实也就是对Java代码进行封装:
1.自定义标签处理器(自定义类实现TagSupport或BodyTagSupport,重写需要重写的方法)
package cn.itcast.util;
import java.io.IOException;
import java.util.UUID;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.TagSupport;
/**
* 自定义jstl标签实现token令牌机制
* 1.继承TagSupport或BodyTagSupport,重写需要重写的方法
* 2.编写自定义标签的tld文档,tld文档相当于自定义标签的说明书
* 3.在页面中引入自定义的标签
* 4.创建自己的tld文档,推荐目录在WEB-INF目录下
* 5.在标签的tld文件中如果没有进行uri(uri指定标签引用时的别名)的定义,那么需要在web.xml中对需要引用的标签配置文件进行定义
* 6.web.xml中配置tld文档
* <jsp-config>
<!-- 每个tld文档对应一个taglib标签 -->
<taglib>
<taglib-location></taglib-location>
<taglib-uri></taglib-uri>
</taglib>
</jsp-config>
*/
public class TokenTag extends TagSupport{
//处理开始标签时执行
@Override
public int doStartTag() throws JspException {
try {
/**
* 注意:
* writer();
* print();
*/
pageContext.getOut().print("<input type=\"hidden\" value=\"123456\"/>");
} catch (IOException e) {
e.printStackTrace();
}
return this.EVAL_BODY_INCLUDE;
}
//处理结束标签是执行
@Override
public int doEndTag() throws JspException {
System.out.println("execute end tag!");
//表示将不再执行jsp页面后边的标签
return this.SKIP_PAGE;
}
public String generateToken(){
String token = UUID.randomUUID().toString().replace("-", "");
return token;
}
}
2.编写自定义标签的tld文档,也相当于自定义标签的描述文档(位置推荐位/WEB-INF/tld/..)
<?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 1.1 customer library</description>
<display-name>JSTL customer</display-name>
<tlib-version>1.1</tlib-version>
<short-name>cus</short-name>
<!-- -->
<uri>http://java.sun.com/jsp/jstl/customer</uri>
<tag>
<description>
自定义标签实现令牌机制
</description>
<name>token</name>
<tag-class>cn.itcast.util.TokenTag</tag-class>
<body-content>JSP</body-content>
</tag>
</taglib>
3.在web.xml中声明或配置自定义标签(如果在tld文档中已经定义了自定义标签的uri,那么此步可以省去)
<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5"
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-app_2_5.xsd">
<display-name></display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<jsp-config>
<!-- 每个tld文档对应一个taglib标签 -->
<taglib>
<taglib-location></taglib-location>
<taglib-uri></taglib-uri>
</taglib>
</jsp-config>
</web-app>
4.自定义标签的使用
<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib prefix="cus" uri="http://java.sun.com/jsp/jstl/customer" %>
<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
<head>
<base href="<%=basePath%>">
<title>自定义标签</title>
<meta http-equiv="pragma" content="no-cache">
<meta http-equiv="cache-control" content="no-cache">
<meta http-equiv="expires" content="0">
<meta http-equiv="keywords" content="keyword1,keyword2,keyword3">
<meta http-equiv="description" content="This is my page">
</head>
<body>
<form action="">
姓名:<input type="text" />
<br/>
年龄:<input type="text" />
<br>
<cus:token></cus:token>
<!-- 自定义标签后边的标签 -->
<input type="text" value="自定义标签后边的标签"/>
</form>
</body>
</html>