1.jsp标签是什么?
JSP标签:指的是JavaServer Pages标签,是JavaWeb应用程序中使用的标签语言。JSP标签允许开发人员将Java代码嵌入到HTML中,以便在Web页面上动态生成内容。
2.jsp标签的特点:
2.1标签的格式:
<开始标签 属性="属性值">标签体</结束标签>
示例:
<c:if test="true">true</c:if>
2.2标签的分类:
空标签: br hr
UI标签 : input,table
控制标签: if foreach
数据标签: out
3.自定义标签的开发 :
配置属性:
<?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>k</short-name> <uri>/Java2EE_jsp/WebContent/WEB-INF/kun.tld</uri> <validator> <description> Provides core validation features for JSTL tags. </description> <validator-class> org.apache.taglibs.standard.tlv.JstlCoreTLV </validator-class> </validator> <tag> <!-- 标签库的名字 --> <name>demo1</name> <!-- 该标签对应的助手类 --> <tag-class>com.xuayhui.tag.DemoTag</tag-class> <!-- 代表是一个JSP标签 --> <body-content>JSP</body-content> <attribute> <!-- 该自定义JSP标签的属性名称 --> <name>var</name> <!-- 该属性是否必填 --> <required>false</required> <!-- 该属性值是否支持表达式 --> <rtexprvalue>false</rtexprvalue> </attribute> </tag> <tag> <name>if</name> <tag-class>com.xuayhui.tag.Iftag</tag-class> <body-content>JSP</body-content> <attribute> <name>test</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>set</name> <tag-class>com.xuayhui.tag.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.xuayhui.tag.OutTag</tag-class> <body-content>JSP</body-content> <attribute> <name>value</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
3.2使用步骤:
3.2.1编写助手类
/** * 助手类 必须继承BodyTagSupport * @author 86155 * */ public class DemoTag extends BodyTagSupport{ @Override public int doStartTag() throws JspException { System.out.println("===================doStartTag================="); return EVAL_BODY_INCLUDE; } @Override public int doAfterBody() throws JspException { System.out.println("===================doAfterBody================="); return super.doAfterBody(); // return EVAL_BODY_AGAIN; //return SKIP_PAGE; } @Override public int doEndTag() throws JspException { System.out.println("===================doEndTag================="); return super.doEndTag(); // return SKIP_PAGE; }
3.2.2编写.tid文件
<?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>k</short-name> <uri>/Java2EE_jsp/WebContent/WEB-INF/kun.tld</uri> <validator> <description> Provides core validation features for JSTL tags. </description> <validator-class> org.apache.taglibs.standard.tlv.JstlCoreTLV </validator-class> </validator> <tag> <!-- 标签库的名字 --> <name>demo1</name> <!-- 该标签对应的助手类 --> <tag-class>com.xuayhui.tag.DemoTag</tag-class> <!-- 代表是一个JSP标签 --> <body-content>JSP</body-content> <attribute> <!-- 该自定义JSP标签的属性名称 --> <name>var</name> <!-- 该属性是否必填 --> <required>false</required> <!-- 该属性值是否支持表达式 --> <rtexprvalue>false</rtexprvalue> </attribute> </tag> <tag> <name>if</name> <tag-class>com.xuayhui.tag.Iftag</tag-class> <body-content>JSP</body-content> <attribute> <name>test</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> <tag> <name>set</name> <tag-class>com.xuayhui.tag.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.xuayhui.tag.OutTag</tag-class> <body-content>JSP</body-content> <attribute> <name>value</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag> </taglib>
3.2.3导入
4.标签的生命周期:
@Override public int doStartTag() throws JspException { System.out.println("===================doStartTag================="); return EVAL_BODY_INCLUDE; } @Override public int doAfterBody() throws JspException { System.out.println("===================doAfterBody================="); return super.doAfterBody(); // return EVAL_BODY_AGAIN; //return SKIP_PAGE; } @Override public int doEndTag() throws JspException { System.out.println("===================doEndTag================="); return super.doEndTag(); // return SKIP_PAGE; }
5.if标签:
案列:
/** * if标签: * 如果满足条件,就打印标签体,dostarttag的返回值eval_body_include * 如果不满足条件,就不输出标签体,dostarttag的返回值skip_body * 需要获取到是否满足条件的结果值,那么标签就有一个属性,属性值是boolean * @author 86155 * */ public class Iftag extends BodyTagSupport{ private boolean test; public boolean isTest() { return test; } public void setTest(boolean test) { this.test = test; } @Override public int doStartTag() throws JspException { // 如果满足条件,就打印标签体,dostarttag的返回值eval_body_include // 如果不满足条件,就不输出标签体,dostarttag的返回值skip_body return test ? EVAL_BODY_INCLUDE : SKIP_BODY; }
6.set/out标签:
set:
public class SetTag extends BodyTagSupport{ 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 super.doStartTag(); } }
out:
public class OutTag extends BodyTagSupport{ 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) { // TODO Auto-generated catch block e.printStackTrace(); } return super.doStartTag(); } }
实列效果: