一简单的自定义标签实现
1 新建一个标签处理类 TestTag ,继承 TagSupport 实现其中的方法(主要是doStartTag() 和doEndTag() 这两个方法)
package com.cn.hnust.unit;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.PageContext;
import javax.servlet.jsp.tagext.TagSupport;
/**
* 自定义标签
*/
public class TestTag extends TagSupport{
/**
* 用户姓名
*/
private String userName;
/**
* 用户性别
*/
private String sex;
/**
* 用户Id
*/
private String id;
private static final long serialVersionUID = -5137834063357478496L;
@Override
public int doStartTag() throws JspException {
try {
/**
* do something 例如 分页,权限控制
*/
if(userName.equalsIgnoreCase("abc")){
// pageContext 传递进来的对象
pageContext.getOut().print("用户名为adc");
}else{
HttpServletRequest request=(HttpServletRequest) pageContext.getRequest();
int ip=request.getRemotePort(); // 端口
/**
* 通过localhost访问的 所以返回的是 0:0:0:0:0:0:0:1
*/
String ss =request.getRemoteAddr();
pageContext.getOut().print(ip+" "+ss);
}
} catch (IOException e) {
e.printStackTrace();
}
return super.SKIP_BODY; // 返回0
}
@Override
public int doEndTag() throws JspException {
return super.doEndTag();
}
@Override
public void release() {
super.release();
}
@Override
public void setPageContext(PageContext pageContext) {
super.setPageContext(pageContext);
}
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
2 新建 tld文件
tld 文件的代码为
<?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 web-jsptaglibrary_2_0.xsd"
version="2.0">
<tlibversion>1.2</tlibversion>
<jspversion>1.2</jspversion>
<shortname>fi</shortname>
<uri>/WEB-INF/tlds/test.tld</uri>
<tag>
<!-- 自定义标签的名字 -->
<name>test</name>
<tagclass>com.cn.hnust.unit.TestTag</tagclass>
<bodycontent>JSP</bodycontent>
<attribute>
<name>userName</name>
<!-- true 必须填写 false 非 -->
<required>true</required>
<!-- 在使用标签时能不能使用表达式来动态指定数据 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>id</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>sex</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>
<bodycontent>JSP</bodycontent> <bodycontent><a target=_blank href="http://blog.csdn.net/aaa1117a8w5s6d/article/details/8157212" target="_blank">标签参数</a> 详解</bodycontent>
<bodycontent>
</bodycontent>
JSP页面的引用
<pre name="code" class="html"><%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<span style="color:#ff0000;"> <%@ taglib uri="/WEB-INF/tlds/test.tld" prefix="cd" %></span>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
标签的使用
</table>
<span style="color:#ff0000;"> <cd:test userName="abc"></cd:test><br/>
<cd:test userName="123"></cd:test></span>
</form>
<script type="text/javascript">
function submit(){
$('#ff').submit();
}
运行结果