自定义标签库
# 标签语言特点
- <开始标签 属性=“属性值”>标签体</结束标签>
1 jsp标签大概分为哪几类:
- ui标签 c:out
特点是显示数据,并且数据不是来源于标签体的,而是来源于jsp标签本身 - 控制标签 if /foreach/c
特点是控制的对象是标签体 - 数据标签 set
特点是存储数据,没有任何的页面效果
2 自定义标签的开发及使用步骤
-
创建一个标签助手类(继承BodyTagSupport)
标签属性必须助手类的属性对应、且要提供对应get/set方法
rtexprvalue -
创建标签库描述文件(tld),添加自定义标签的配置
注:tld文件必须保存到WEB-INF目录或其子目录 -
在JSP通过taglib指令导入标签库,并通过指定后缀
访问自定义标签
3 标签生命周期
SKIP_BODY:跳过主体
EVAL_BODY_INCLUDE:计算标签主体内容并[输出]
EVAL_BODY_BUFFERED:计算标签主体内容并[缓存]
EVAL_PAGE:计算页面的后续部分
SKIP_PAGE:跳过页面的后续部分
EVAL_BODY_AGAIN:再计算主体一次
4.开发出c:out/c:foreach/c:if/c:set/ 自定义select标签应该具备的功能
注1:JspWriter writer = pageContext.getOut();//把类容写入网页
注2:page(pageContext)|request(…)|session(…)|application(…)
存储和交换数据
out标签
out实体类:
package com.hyf.jsp.day;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* out属于UI标签(需要展示效果,是依靠标签属性展示页面效果)
* @author 17628
*
*/
public class OutTag extends BodyTagSupport {
private static final long serialVersionUID = 5670254507867249879L;
private Object value;
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.print(value);
} catch (IOException e) {
e.printStackTrace();
}
return SKIP_BODY;
}
}
set
set类:
package com.hyf.jsp.day;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* 数据标签(不需要打印类容)
* 作用:
* 是将value值赋给变量var
* @author 17628
*
*/
public class SetTag extends BodyTagSupport {
private static final long serialVersionUID = 6674809123052015115L;
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;
}
public int doStartTag() throws JspException {
pageContext.setAttribute(var, value);
// 跳过主题
return SKIP_BODY;
}
}
if
if类:
package com.hyf.jsp.day;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* if属于控制标签(页面展示效果依赖的是标签体)
* @author 17628
*
*/
public class IfTag extends BodyTagSupport {
private static final long serialVersionUID = -7966823144473885578L;
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;
}
}
foreach标签
foreach类:
package com.hyf.jsp.day;
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 Foreach extends BodyTagSupport {
private static final long serialVersionUID = -5069696275447566503L;
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 计算页面后续的部分
}
}
}
select
自定义select标签应该具备的功能
- 1、新增查询页面,只要通过一个标签就可以完成数据的绑定,而并非使用c:foreach去循环绑定数据
- 2、修改页面,同样通过一个自定义标签完成数据的遍历展示,以及默认选中指定项
package com.hyf.jsp.day;
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;
/**
* 需要定义的属性
* 1.要往后台传值 id,name
* 2、定义数据库存储的对应的标签属性、前台页面展示的标签属性 textKey,TextVal
* 3、定义下拉框的默认值 headerTextKey,headerTextVal
* 4、下拉框需要加载数据 items
* 5、属性值接受数据库中保存的value值 selectedVal
*
* 属于UI标签
* @author 17628
*
*/
public class SelectTag extends BodyTagSupport {
private static final long serialVersionUID = -5207293165231778411L;
private String id;
private String name;
private List<Object> items=new ArrayList<>();
private String textKey;
private String textVal;
private String headerTextKey;
private String headerTextVal;
private String selectedVal;
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
// 之所以能够在也面上看到select下拉框的效果,原因在于后台做了html代码的拼接
out.print(toHTML());
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
}
return super.doStartTag();
}
private String toHTML() throws Exception {
StringBuilder sb =new StringBuilder();
String value=""; //获取对象的Id
String html="";
sb.append("<select id='"+id+"' name='"+name+"'>");
if(!(headerTextKey ==null || "".equals(headerTextKey)|| headerTextVal == null || "".equals(headerTextVal))) {
sb.append("<option selected value='"+headerTextKey+"'>"+headerTextVal+"</option>");
}
for (Object obj : items) {
// 底层代码
Field f = obj.getClass().getDeclaredField(textKey);
f.setAccessible(true);// 打开权限
value = (String)f.get(obj);
// 第三方工具包
html=(String)PropertyUtils.getProperty(obj, textVal);
// 考虑如果是修改页面需要下拉框回选数据库所存储的值
if(selectedVal ==null || "".equals(selectedVal)) {
sb.append("<option value='"+value+"'>"+html+"</option>");
}else {
if(selectedVal.equals(value)) {
sb.append("<option selected value='"+value+"'>"+html+"</option>");
}else {
sb.append("<option value='"+value+"'>"+html+"</option>");
}
}
}
sb.append("</select>");
return sb.toString();
}
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 getHeaderTextKey() {
return headerTextKey;
}
public void setHeaderTextKey(String headerTextKey) {
this.headerTextKey = headerTextKey;
}
public String getHeaderTextVal() {
return headerTextVal;
}
public void setHeaderTextVal(String headerTextVal) {
this.headerTextVal = headerTextVal;
}
public String getSelectedVal() {
return selectedVal;
}
public void setSelectedVal(String selectedVal) {
this.selectedVal = selectedVal;
}
tld 文件
把每个助手类进行配置
<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"
version="2.1">
<description>hyff 1.1 core library</description>
<display-name>hyff core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>c</short-name>
<uri>/hyff</uri>
<tag>
<!-- 填写的是标签库中的标签名 -->
<name>set</name>
<!-- 标签对应后台助手类 -->
<tag-class>com.hyf.jsp.day.SetTag</tag-class>
<!-- 标签类别 -->
<body-content>JSP</body-content>
<attribute>
<!-- 自定义标签中的属性 -->
<name>var</name>
<!-- 属性值是否必选填 -->
<required>true</required>
<!-- 是否支持EL表达式/ongl表达式 -->
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>out</name>
<tag-class>com.hyf.jsp.day.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.hyf.jsp.day.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.hyf.jsp.day.Foreach</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.hyf.jsp.day.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>headerTextKey</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>headerTextVal</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>selectedVal</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>
网页显示: