当我们遇到查询分页、下拉列表回填、复选框回填…,重复代码过多,代码冗余代码的可读性较差,不便于阅读修改等问题,我们就可以使用自定义标签解决这些问题。
<开始标签 属性=“属性值”>标签体结束标签>
空标签
< br/>< hr/>
<开始标签>结束标签>
<开始标签/>
ui标签 专门用来展示内容--->select
控制标签 做流程控制用的--->if/foreach
数据标签 c:set将某一数据赋值给某变量
注意:tld文件必须保存到WEB-INF目录或其子目录
<tag>
<name>set</name>//标签名
<tag-class>com.zhangsiwen.jsp.day02.SetTag</tag-class>//助手类
<body-content>JSP</body-content>
<attribute>
<name>var</name>//属性名
<required>true</required>//是否必填
<rtexprvalue>false</rtexprvalue>//是否支持表达式
</attribute>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<%@taglib prefix="z" uri="/zhangsiwen" %>
标签属性必须与助手类的属性对应、且要提供对应get/set方法
package com.zhangsiwen.jsp.day02;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class SetTag extends BodyTagSupport {
private static final long serialVersionUID = 1L;
private String var;
private Object value;
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
@Override
public int doStartTag() throws JspException {
pageContext.setAttribute(var, value);
return SKIP_BODY;
}
}
SKIP_BODY 跳过主体
SKIP_PAGE 跳过页面的后续部分
EVAL_BODY_INCLUDE 计算标签主体内容并输出
EVAL_BODY_AGAIN 再计算主体一次
EVAL_PAGE 计算页面的后续部分
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>JSTL 1.1 core library</description>
<display-name>JSTL core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>c</short-name>
<uri>/zhangsiwen</uri>
<tag>
<name>set</name>
<tag-class>com.zhangsiwen.jsp.day02.SetTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>out</name>
<tag-class>com.zhangsiwen.jsp.day02.OutTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>if</name>
<tag-class>com.zhangsiwen.jsp.day02.IfTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
if标签助手类
package com.zhangsiwen.jsp.day02;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class IfTag extends BodyTagSupport {
private static final long serialVersionUID = 7889433543629128352L;
private boolean test;
public boolean isTest() {
return test;
}
public void setTest(boolean test) {
this.test = test;
}
@Override
public int doStartTag() throws JspException {
// TODO Auto-generated method stub
return test ? EVAL_BODY_INCLUDE : SKIP_BODY;
}
}
set标签助手类
package com.zhangsiwen.jsp.day02;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class SetTag extends BodyTagSupport {
private static final long serialVersionUID = 1L;
private String var;
private Object value;
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
@Override
public int doStartTag() throws JspException {
pageContext.setAttribute(var, value);
return SKIP_BODY;
}
}
out标签助手类
package com.zhangsiwen.jsp.day02;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class OutTag extends BodyTagSupport {
private static final long serialVersionUID = 1L;
private Object value;
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.print(value);
} catch (IOException e) {
e.printStackTrace();
}
return SKIP_BODY;
}
}
引用标签库
<%@taglib prefix="z" uri="/zhangsiwen" %>
jsp文件内容
<z:set var="sex" value="gay"></z:set>
<z:out value="${sex }"></z:out>
<z:if test="true">gay</z:if>
<z:if test="false">伪娘</z:if>