如何在jsp页面中写自定义标签
这里我要实现一个自定义标签的小例子,我把用户名存在cookie中,在登录界面我要从cookie中取出用户名
一.创建和使用一个tag library的基本步骤:
1.创建标签的处理类URLDEcoderTag.java
package tm.change.tag;
import java.io.IOException;
import java.net.URLDecoder;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.SimpleTagSupport;
//标签
public class URLDEcoderTag extends SimpleTagSupport {
//标签的属性
private String context;//要转码的内容
private String encode;//编码格式
public String getContext() {
return context;
}
public void setContext(String context) {
this.context = context;
}
public String getEncode() {
return encode;
}
public void setEncode(String encode) {
this.encode = encode;
}
@Override //自定义标记要执行的方法
public void doTag() throws JspException, IOException {
//url解码
String URL= URLDecoder.decode(context, encode);
this.getJspContext().getOut().write(URL);
}
}
2.创建标签库的描述文件UserTag.tld
每个tld文件对应一个标签库,每个标签库中可以包含多个标签。该文件放在web-inf下面
<?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>UserTag</short-name>
<tag>
<name>URLDecoder</name>
<tag-class>tm.change.tag.URLDEcoderTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>context</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.String</type>
</attribute>
<attribute>
<name>encode</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
<type>java.lang.String</type>
</attribute>
</tag>
</taglib>
3.在web.xml中配置
<taglib-uri>对应你在jsp页面中引入标签库的uri
<taglib-location>对应tld文件的路径
<jsp-config>
<taglib>
<taglib-uri>find_in_web_xml</taglib-uri>
<taglib-location>/WEB-INF/UserTag.tld</taglib-location>
</taglib>
</jsp-config>
4.在index.jsp文件中应用标签库
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@taglib uri="find_in_web_xml" prefix="UserTag" %>
<!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>
<form action="${pageContext.request.contextPath }/LoginServlet">
<table>
<tr>
<td>用户名</td>
<td><input type="text" name="username" value="<UserTag:URLDecoder context="${cookie.remname.value}" encode="utf-8"/>" /></td>
</tr>
<tr>
<td>密码</td>
<td><input type="password" name="password"></td>
</tr>
<tr>
<td><input type="checkbox" value="true" name="autologin">自动登录</td>
<td><input type="checkbox" value="true" name="remname"
<c:if test="${cookie.remname !=null}">checked
</c:if>>记住用户名</td>
</tr>
<tr>
<td><button type="submit">提交</button></td>
</tr>
</table>
</form>
</body>
</html>
二.自定义jsp标签的处理过程:
1.在jsp中引用标签库
2.在jsp中引用标签
3.web容器根据第二个步骤中的prefix,获得第一个标签中申明的taglib的uri属性值
4.web容器根据uri属性在web.xml中找到对应的元素
5.从元素中获得对应的元素的值
6.web容器根据元素的值从WEB-INF目录下找到对应的tld文件
7.从tld文件中 找到与tagname对应的元素
8.从元素中获得对应的值
9.web容器根据元素的值创建相应的tag handle class的实例
10.web容器调用这个实例的doTag/dostartTag/doendTag方法完成相应的处理