jsp自定义标签
程序员文章站
2022-05-30 22:11:23
...
一,为什么要学习jsp自定义标签?
在JAVA项目开发中,JSP自定义标签,可以使前端的展现简洁与方便
二,自定义标签的开发及使用步骤
1.创建一个标签助手类(继承BodyTagSupport)
2.创建标签库描述文件(tld),添加自定义标签的配置
3.在JSP通过taglib指令导入标签库,并通过指定后缀访问自定义标签
4.架包
5.在jsp页面头部通过taglib标签导入tld文件
三,jsp自定义生命周期图
五,代码
1.set标签
<tag>
<name>set</name>
<tag-class>com.leiliang.jsp.SetTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>value</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
2.setTag助手类
package com.leiliang.jsp
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 {
// TODO Auto-generated method stub
pageContext.setAttribute(var, value);
return SKIP_BODY;
}
}
引用显示(set无输出)
<t:set var="sex" value="男"></t:set>
3.out标签
<tag>
<name>out</name>
<tag-class>com.leiliang.jsp.OutTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag-class>com.leiliang.jsp.OutTag</tag-class>
4.tTag助手类
package com.leiliang.jsp;
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 {
// TODO Auto-generated method stub
JspWriter out = pageContext.getOut();
try {
out.print(value);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.doStartTag();
}
}
引用显示(只做输出)
//取的之前的set值,用的是el表达式赋值
<t:out value="${sex }"></t:out>
5.if标签
<tag>
<name>if</name>
<tag-class>com.leiliang.jsp.IfTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
IfTag助手类
package com.leiliang.jsp;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class IfTag extends BodyTagSupport{
private static final long serialVersionUID = 1L;
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;
}
}
引用显示(进行判断)
<t:if test="true">男</t:if>
<t:if test="false">女</t:if>
上一篇: jsp自定义标签