jsp自定义标签
程序员文章站
2022-03-11 11:06:13
...
1. 标签语言特点
<开始标签 属性=“属性值”>标签体</结束标签>
2.三大类标签
2.1 ui标签 c:out
特点是显示数据,并且数据不是来源于标签体的,而是来源于jsp标签本身
2.2 控制标签 if /foreach/c
特点是控制的对象是标签体
2.3 数据标签 set
特点是存储数据,没有任何的页面效果
3.了解标签的生命周期图
4.自定义标签的开发及使用步骤
创建一个标签助手类(继承BodyTagSupport)标签属性必须助手类的属性对应、且要提供对应get/set方法和doStartTag()、doAfterBody()、doEndTag()方法,并了解
SKIP_BODY:跳过主体
EVAL_BODY_INCLUDE:计算标签主体内容并[输出]
EVAL_BODY_BUFFERED:计算标签主体内容并[缓存]
EVAL_PAGE:计算页面的后续部分
SKIP_PAGE:跳过页面的后续部分
EVAL_BODY_AGAIN:再计算主体一次
2 创建标签库描述文件(tld),添加自定义标签的配置
注:tld文件必须保存到WEB-INF目录或其子目录
3 在JSP通过taglib指令导入标签库,并通过指定后缀
访问自定义标签
下面代码演示:
SatTag:
package com.pyc;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* 数据标签(不需要展示类容)
* 作用:
* 是将value值赋予变量var
* @author 96345
*
*/
public class SetTag extends BodyTagSupport{
private static final long serialVersionUID = -6063398854261197972L;
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;
}
}
OutTag:
package com.pyc;
import java.io.IOException;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* out 属于UI标签(需要展示效果,是依靠标签属性展示页面效果)
* JspWriter
* * @author 96345
*
*/
public class OutTag extends BodyTagSupport{
private static final long serialVersionUID = 900890541735512755L;
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) {
e.printStackTrace();
}
return super.doStartTag();
}
}
IfTag:
package com.pyc;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class IfTag extends BodyTagSupport{
private static final long serialVersionUID = 3665095875292237752L;
private boolean flag;
public boolean isFlag() {
return flag;
}
public void setFlag(boolean flag) {
this.flag = flag;
}
@Override
public int doStartTag() throws JspException {
return flag ? EVAL_BODY_INCLUDE : SKIP_BODY;
}
}
ForeachTag:
package com.pyc;
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 = -2364828486746117631L;
private List<Object> items = new ArrayList<>();
private String var;
public List<Object> getItems() {
return items;
}
public void setItems(List<Object> items) {
this.items = items;
}
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
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;
}
}
}
selectTag:
package com.pyc;
import java.io.IOException;
import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
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.定义下拉框的默认值headerTextKey,headTextVal
* 4.下拉框需要加载数据 items
* 5.属性值接受数据库中保存的value值 selectedVal
*
* 需要UI标签
* @author 96345
*
*/
public class SelectTag extends BodyTagSupport{
private static final long serialVersionUID = 4863443457992720507L;
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;
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
// 之所以能够在页面上看到select下拉框的效果,原因在于后台做了html代码的拼接
out.print(toHTML());
} catch (IOException | NoSuchFieldException | SecurityException | IllegalArgumentException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e){
e.printStackTrace();
}
return super.doStartTag();
}
private String toHTML() throws NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
StringBuilder sb = new StringBuilder();
String value = "";
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>");
}
// items=data,textKey=sid,textVal=sname
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(value.equals(selectedVal)) {
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;
}
}
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>pro_qx 1.1 core library</description>
<display-name>pro_qx core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>c</short-name>
<uri>/pro_qx</uri>
<tag>
<!-- 填写的是标签库中的标签 -->
<name>demo</name>
<!-- 标签对应的后台助手类(每在页面上写标签都会去访问后台代码) -->
<tag-class>com.pyc.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.pyc.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.pyc.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.pyc.IfTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>flag</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<!-- foreach标签 -->
<tag>
<name>foreach</name>
<tag-class>com.pyc.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.pyc.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>true</rtexprvalue>
</attribute>
</tag>
</taglib>
demo2.jsp
<%@page import="com.pyc.Teacher"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@ taglib prefix="m" uri="/pro_qx" %>
<!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>jsp自定义标签</title>
</head>
<body>
<!-- set,out -->
<m:set var="name" value="pyc"></m:set>
<m:out value="${name}"></m:out>
<hr>
<!-- if -->
<m:if flag="true">xiaopan</m:if>
<hr>
<!-- foreach -->
<%
/* 模拟查过来的集合 */
List list = new ArrayList();
list.add(new Teacher("1","春"));
list.add(new Teacher("2","秋"));
list.add(new Teacher("3","冬"));
pageContext.setAttribute("data", list);
%>
<m:foreach items="${data}" var="t">
${t.id},${t.name}<br>
</m:foreach>
<hr>
<!-- select -->
<h2>新增查询页面下拉框</h2>
<m:select textVal="name" items="${data }" textKey="id"></m:select>
<hr>
<m:select headerTextKey="-1" headerTextVal="--请选择--" textVal="name" items="${data }" textKey="id"></m:select>
<hr>
<h2>修改页面下拉框</h2>
<m:select headerTextKey="-1" headerTextVal="--请选择--" textVal="name" items="${data }" textKey="id" selectedVal="2"></m:select>
</body>
</html>
运行结果: