一、标签语言特点
<开始标签 属性=“属性值”>标签体</结束标签>
二、标签类别
1、ui标签 c:out
特点是显示数据,并且数据不是来源于标签体的,而是来源于jsp标签本身
2、 控制标签 if /foreach/c
特点是控制的对象是标签体
3、 数据标签 set
特点是存储数据,没有任何的页面效果
三、自定义标签的开发及使用步骤
1 、创建一个标签助手类(继承BodyTagSupport)
标签属性必须助手类的属性对应、且要提供对应get/set方法
rtexprvalue
2、 创建标签库描述文件(tld),添加自定义标签的配置
注:tld文件必须保存到WEB-INF目录或其子目录
3 、在JSP通过taglib指令导入标签库,并通过指定后缀
访问自定义标签
四、 标签生命周期图
SKIP_BODY:跳过主体
EVAL_BODY_INCLUDE:计算标签主体内容并[输出]
EVAL_BODY_BUFFERED:计算标签主体内容并[缓存]
EVAL_PAGE:计算页面的后续部分
SKIP_PAGE:跳过页面的后续部分
EVAL_BODY_AGAIN:再计算主体一次
五、自定义 c:out/c:foreach/c:if/c:set标签
1.out标签
package com.cjq.jsp.tag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* out属于UI(需要展示效果,是依靠标签升序展示页面效果)
* JspWriter
*
* @author cjq
*
*/public class OutTag extends BodyTagSupport{ private static final long serialVersionUID = 2163790218700691365L;
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 (Exception e) {
e.printStackTrace();
}
return super.doStartTag();
}}
2.set标签
package com.cjq.jsp.tag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* 数据标签(不需要展示内容)
* 作用:
* 是将value值赋给变量var
*
* @author cjq
*
*/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 SKIP_BODY;
}}
3.if标签
package com.cjq.jsp.tag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* if属于控制标签(页面展示效果依赖的是标签体)
* @author cjq
*
*/
public class IfTag extends BodyTagSupport {
private static final long serialVersionUID = -8065316329441275709L;
private boolean test; public boolean isTest() {
return test;
}
public void setTest(boolean test) {
this.test = test;
}
public int doStartTag() throws JspException {
return test ? EVAL_BODY_INCLUDE : SKIP_BODY;
}}
4.foreach标签
package com.cjq.jsp.tag;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class ForeachTag extends BodyTagSupport {
private static final long serialVersionUID = -2888366640462823432L;
private String var;
private List<Object> items = new ArrayList<>(); public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
public List<Object> getItems() {
return items;
}
public void setItems(List<Object> items) {
this.items = items;
}
public int doStartTag() throws JspException {
Iterator<Object> it = items.iterator();
pageContext.setAttribute(var, it.next());
pageContext.setAttribute("it", it);
return EVAL_BODY_INCLUDE;
}
public int doAfterBody() throws JspException {
// 获取原来状态的迭代器,而非创建一个迭代器
Iterator<Object> it=(Iterator<Object>) pageContext.getAttribute("it");
if(it.hasNext()) {
pageContext.setAttribute(var, it.next());
pageContext.setAttribute("it", it);
return EVAL_BODY_AGAIN;
}else {
return EVAL_PAGE; //EVAL_PAGE 计算页面后续的部分
}
}}
5.select标签
package com.cjq.jsp.tag;
import java.io.IOException;
import java.lang.reflect.Field;
import java.util.ArrayList;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;import org.apache.commons.beanutils.PropertyUtils;/**
* select 的功能:需求
* 1、新增查询页面,只要通过一个标签就可以完成数据的绑定,而不用使用c:foreach去循环绑定数据
* 2、修改页面,同样通过这个标签完成数据的遍历展示以及默认选中指定向
*
* 需要:
* 1、需要往后台传值 id,name
* 2、定义数据库存储的对应的标签属性、前台展示的标签属性 textkey textval
* 3、定义下拉框的默认值 headkey headval
* 4、下拉框加载数据 items
* 5、属性值接受数据库中保存的value值 被选中的值 selectedval
*
* 属于展示的ui标签,要展示内容
* @author cjq
*
*/
public class SelectTag extends BodyTagSupport{
private static final long serialVersionUID = 8680576328606908002L;
private String id;
private String name;
private List<Object> items = new ArrayList<>();
private String textkey;
private String textval;
private String headkey;
private String headval;
private String selectedval;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public List<Object> getItems() {
return items;
}
public void setItems(List<Object> items) {
this.items = items;
}
public String getTextkey() {
return textkey;
}
public void setTextkey(String textkey) {
this.textkey = textkey;
}
public String getTextval() {
return textval;
}
public void setTextval(String textval) {
this.textval = textval;
}
public String getHeadkey() {
return headkey;
}
public void setHeadkey(String headkey) {
this.headkey = headkey;
}
public String getHeadval() {
return headval;
}
public void setHeadval(String headval) {
this.headval = headval;
}
public String getSelectedval() {
return selectedval;
}
public void setSelectedval(String selectedval) {
this.selectedval = selectedval;
}
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
//能够在也面上看到select下拉框的效果,原因在于后台做了html代码的拼接
out.print(toHTML());
} catch (Exception e) {
e.printStackTrace();
}
return super.doStartTag();
}
private String toHTML() throws Exception, Exception {
StringBuilder sb = new StringBuilder();
sb.append("<select id='"+id+"' name='"+name+"'>");
if(!(headkey == null || "".equals(headkey) || headval == null || "".equals(headval))) {
sb.append("<option selected value='"+headkey+"'>"+headval+"</option>");
}
String val = "";//获取对象ID
String html = "";
for (Object obj : items) {
//底层代码
Field f = obj.getClass().getDeclaredField(textkey);
f.setAccessible(true);
val = (String) f.get(obj);
f = obj.getClass().getDeclaredField(textval);
f.setAccessible(true);//打开权限
html = (String) f.get(obj);
//第三方工具包写法:(commons-beanutils-1.8.0 和 commons-logging)
// val = (String) PropertyUtils.getProperty(obj, textkey);
// html = (String) PropertyUtils.getProperty(obj, textval);
//考虑如果是修改页面时,下拉框回选数据库所存储的值 也就是默认选中数据库所存储的值
if(val.equals(selectedval)) {
sb.append("<option selected value='"+val+"'>"+html+"</option>");
}else {
sb.append("<option value='"+val+"'>"+html+"</option>");
}
}
sb.append("</select>");
return sb.toString();//返回拼接的值
}
}
6.配置描述文件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>cjq 1.1 core library</description>
<display-name>cjq core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>c</short-name>
<uri>/cjq</uri>
<!-- demo -->
<tag>
<!-- 填写的是标签库中的标签名 -->
<name>demo</name>
<!-- 标签对应后台助手类 -->
<tag-class>com.cjq.jsp.tag.DemoTag</tag-class>
<!-- 标签类别 -->
<body-content>JSP</body-content>
<attribute>
<!-- 自定义标签中属性 -->
<name>test</name>
<!-- 属性值是否必填 -->
<required>true</required>
<!-- 是否支持表达式EL表达式/ognl表达式 -->
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
<!-- set -->
<tag>
<name>set</name>
<tag-class>com.cjq.jsp.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>
<!-- out -->
<tag>
<name>out</name>
<tag-class>com.cjq.jsp.tag.OutTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<!-- if -->
<tag>
<name>if</name>
<tag-class>com.cjq.jsp.tag.IfTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<!-- foreach -->
<tag>
<name>foreach</name>
<tag-class>com.cjq.jsp.tag.ForeachTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<!-- select -->
<tag>
<name>select</name>
<tag-class>com.cjq.jsp.tag.SelectTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>id</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>name</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>textkey</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>textval</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>headkey</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute><attribute>
<name>headval</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>selectedval</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag></taglib>
7.加入jsp动态网页
<%@page import="com.cjq.jsp.entity.Teacher"%>
<%@page import="java.util.List"%>
<%@page import="java.util.ArrayList"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="z" uri="/cjq" %>
<!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>
<h2>set、out标签</h2>
<z:set var="name" value="zs"></z:set>
<z:out value="${name }"></z:out>
<h2>if标签</h2>
<z:if test="true">ls</z:if>
<z:if test="false">ww</z:if><h2>list标签</h2>
<%
List list=new ArrayList();
list.add(new Teacher("1", "cjq1"));
list.add(new Teacher("2", "cjq2"));
list.add(new Teacher("3", "cjq3"));
pageContext.setAttribute("teas", list);
%>
<z:foreach items="${teas }" var="t">
</z:foreach><h2>新增查询下拉框</h2>
<z:select textkey="tid" items="${teas }" textval="tname"></z:select>
<z:select headkey="-1" headval="--请选择--" textkey="tid" items="${teas }" textval="tname"></z:select><h2>修改页面下拉框</h2>
<z:select textkey="tid" items="${teas }" textval="tname" selectedval="cjq"></z:select></body>
</html>
测试结果如下: