这里写目录标题
jsp标签的生命周期
标签语言的特点
<开始标签 属性=“属性值”>标签体</结束标签>
空标签
<开始标签></结束标签>
<开始标签/>
ui标签
控制标签
数据标签
自定义标签的开发及使用步骤
3.1创建一个标签助手类(继承BodyTagSupport) 标签属性必须助手类的属性对应、且要提供对应get/set方法rtexprvalue
package com.luojun.jsp.day01;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
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 EVAL_BODY_AGAIN;
}
@Override
public int doEndTag() throws JspException {
System.out.println("doEndTag--------------");
return super.doEndTag();
}
}
3.2创建标签库描述文件(tld),添加自定义标签的配置
注:tld文件必须保存到WEB-INF目录或其子目录
<?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>z</short-name>
<uri>/zking</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.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.luojun.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.luojun.jsp.day02.IfTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
3.3定三个标签助手类
package com.luojun.jsp.day02;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
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 {
// TODO Auto-generated method stub
return test ? EVAL_BODY_INCLUDE : SKIP_BODY ;
}
}
package com.luojun.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 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 SKIP_BODY;
}
}
package com.luojun.jsp.day02;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class SetTag extends BodyTagSupport{
private static final long serialVersionUID = -4112295244857732351L;
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;
}
}
3.4在JSP通过taglib指令导入标签库
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="z" uri="/luojun" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>
</head>
<body>
<!-- set out if -->
<!--
现象
set,设置值,页面无输出
out,输出指定值
if,做判断用的
研究c标签
taglib uri指向了c.tid的uri标签内容,从而将c标签引用到当前jsp页面使用
使用标签的时候,本质是有一个助手类(tag-class)帮忙处理业务逻辑的
org.apache.taglibs.standard.tag.rt.core.SetTag
助手类组成部分
jsp页面使用的标签 c:set
<name>set</name>
使用标签的时候,本质是有一个助手类处理业务
<tag-class>brg.apache.taglibs.standard.tag.rt.core.SetTag</tag-class>
自定义jsp标签
<body-content>JSP</body-content>
自定义标签的属性
<attribute>
属性明
<name>value</name>
是否必填
<required>false</required>
是否支持表达式(el表达式,ognl表达式)
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>var</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
助手类怎么写?
自定义标签的生命周期图
定义助手类需要继承BodyTagSupport
-->
<c:set var="name" value="zs" ></c:set>
<hr/>
<c:out value="${name }"></c:out>
<hr/>
<c:if test="true">男</c:if>
<hr/>
<c:if test="false">女</c:if>
<hr/>
<%-- <z:demo>demo标签内容</z:demo> --%>
<hr/>
<z:set var="sex" value="gay" ></z:set>
<z:out value="${sex }"></z:out>
<hr/>
<z:if test="true">gay</z:if>
<z:if test="false">百合</z:if>
</body>
</html>
3.5效果图