第一:定义个类
需要继承现有的类,具体不看情况。这里继承BodyTagSupport
建议细节查看API,这里只说流程,细节的书太多,我说很累赘,
public class TagName extends BodyTagSupport {
private String userName;
public void setUserName(String userName){
this.userName = userName;
}
public int doStartTag(){
if(userName.equals("admin")){
return EVAL_BODY_INCLUDE;
}
return SKIP_BODY;
}
}
第二:创建eltag.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 http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd"
version="2.0">
<description>by Brj</description>
<display-name>jstl tag</display-name>
<tlib-version>1.1</tlib-version>
<short-name>eval</short-name>
<uri>http://www.mingloveyang.com</uri>
<tag>
<name>eval</name>
<tag-class>com.el.tag.TagName</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>userName</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
第三:web.xml中这样
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.4" xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<jsp-config>
<taglib>
<taglib-uri>http://www.yang.com</taglib-uri>
<taglib-location>/WEB-INF/eltag.tld</taglib-location>
</taglib>
</jsp-config>
</web-app>
第四:在页面中这样用
头声明:
<%@ taglib prefix="cc" uri="http://www.yang.com" %>
标签:
<cc:eval userName="admin">
管理员可以看到的的东西。。。
</cc:eval>