自定义标签
自定义标签步骤
1 .创建助手类
2. 创建tld
3. 导入taglib
标签分类
ui标签
z:out
z:select
控制标签
z:if
z:forEach
自定义的开发及使用步骤
3.1创建set标签助手类
package com.luojun.jspTag;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import com.luojun.dao.DeptDao;
import com.luojun.entity.Dept;
public class SetTag extends BodyTagSupport {
private String value;//保存数据的key
private DeptDao deptdao=new DeptDao();
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public DeptDao getDeptdao() {
return deptdao;
}
public void setDeptdao(DeptDao deptdao) {
this.deptdao = deptdao;
}
public SetTag() {
}
public SetTag(String value, DeptDao deptdao) {
this.value = value;
this.deptdao = deptdao;
}
@Override
public int doStartTag() throws JspException {
// TODO Auto-generated method stub
List<Dept> list=deptdao.list();
pageContext.setAttribute(value,list);
return super.doStartTag();
}
}
3.2配置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>luojun 1.1 core library</description>
<display-name>luojun core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>z</short-name>
<uri>/luojun</uri>
<tag>
<name>demo</name>
<tag-class>com.luojun.jsp.day01.DemoTag</tag-class>
<body-content>JSP</body-content>
</tag>
<tag>
<name>set</name>
<tag-class>com.luojun.jspTag.SetTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
<tag>
<name>out</name>
<tag-class>com.luojun.jspTag.OutTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>if</name>
<tag-class>com.luojun.jspTag.IfTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>forEach</name>
<tag-class>com.luojun.jspTag.ForEachTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>varStatus</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
<tag>
<name>select</name>
<tag-class>com.luojun.jspTag.SelectTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>name</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>valuekey</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>textkey</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>css</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>
3.3在jsp页面导入刚创建的tld配置文件
<%@ taglib prefix="j" uri="/luojun" %>
3.4效果图