实现jsp自定义标签

如何在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方法完成相应的处理




























  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值