自定义标签库
1. 标签语言特点
<开始标签 属性=“属性值”>标签体</结束标签>
空标签
<开始标签></结束标签>
<开始标签/>
ui标签
控制标签
数据标签
2. 自定义标签的开发及使用步骤
- 创建一个标签助手类(继承BodyTagSupport)
标签属性必须助手类的属性对应、且要提供对应get/set方法
rtexprvalue - 创建标签库描述文件(tld),添加自定义标签的配置
注:tld文件必须保存到WEB-INF目录或其子目录 - 在JSP通过taglib指令导入标签库,并通过指定后缀
访问自定义标签
<?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>lrc 1.1 core library</description>
<display-name>lrc</display-name>
<tlib-version>1.1</tlib-version>
<short-name>c</short-name>
<uri>/lrc</uri>
<tag>
<!-- 标签库中的标签名 -->
<name>demo</name>
<!-- 标签对应的后台助手类 -->
<tag-class>com.lrc.Demo</tag-class>
<!-- 标签类别 -->
<body-content>JSP</body-content>
<attribute>
<!-- 自定义标签携带的属性 -->
<name>test</name>
<!-- 属性值是否必填 -->
<required>true</required>
<!-- 是否支持表达式(EL/ognl) -->
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>
3. 标签生命周期
- 实例化标签助手类->doStartTag()------------->doEndTag()
- 3.2 实例化标签助手类->doStartTag()--------->doAfterBody--------->doEndTag()…
- SKIP_BODY:跳过主体
EVAL_BODY_INCLUDE:计算标签主体内容并[输出]
EVAL_BODY_BUFFERED:计算标签主体内容并[缓存]
EVAL_PAGE:计算页面的后续部分
SKIP_PAGE:跳过页面的后续部分
EVAL_BODY_AGAIN:再计算主体一次
package com.lrc;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class Demo extends BodyTagSupport {
/**
* 序列号的作用是方便对象序列化
* 序列化就是将对象按照特定的规则持久化到硬盘
* 反序列化就是将硬盘中的对象读取到内存里
*/
private static final long serialVersionUID = 1L;
private String test;
public String getTest() {
return test;
}
public void setTest(String test) {
this.test = test;
}
//开始
@Override
public int doStartTag() throws JspException {
System.out.println(test);
System.out.println("doStartTag--------------------");
return EVAL_BODY_INCLUDE;//SKIP_BODY跳过
}
//结束
@Override
public int doEndTag() throws JspException {
System.out.println("doEndTag--------------------");
return super.doEndTag();
}
//主体
@Override
public int doAfterBody() throws JspException {
System.out.println("doAfterBody--------------------");
return EVAL_BODY_AGAIN;//EVAL_BODY_AGAIN循环
//结束需要定义boolean开关
//return flag ? EVAL_BODY_AGAIN : EVAL_PAGE;
}
//doAfterBody方法是否执行的因素
//是否有标签体
//doStartTag方法的返回值
}
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib uri="/lrc" prefix="l" %>
<!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>
<l:demo test="LLL">JSP标签</l:demo>
</body>
</html>
SKIP_BODY跳过
EVAL_BODY_INCLUDE:计算标签主体内容并[输出]
EVAL_BODY_AGAIN:再计算主体一次
4.自定义标签开发步骤
- 助手类
- tld
- taglib
5.标签类型
- UI标签
z:out 1
m:select 5 - 控制标签
m:if 2
m:forEach 3 - 数据标签
m:set 4
数据标签就是用来存储数据的
下面展示一些 `内联代码片
package com.lrc.jsp;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class SetDemo extends BodyTagSupport {
/**
* 开发三个标签
*/
private static final long serialVersionUID = 1L;
private String var;
private Object value;
@Override
public int doStartTag() throws JspException {
return SKIP_BODY;
}
@Override
public int doEndTag() throws JspException {
pageContext.setAttribute(var, value);
//HttpServletRequest request=(HttpServletRequest)pageContext.getRequest();
return super.doEndTag();
}
@Override
public int doAfterBody() throws JspException {
return super.doAfterBody();
}
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;
}
}
package com.lrc.jsp;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class OutDemo extends BodyTagSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private Object value;
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
@Override
public int doStartTag() throws JspException {
return SKIP_BODY;
}
@Override
public int doEndTag() throws JspException {
return super.doEndTag();
}
@Override
public int doAfterBody() throws JspException {
return super.doAfterBody();
}
}
package com.lrc.jsp;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class IfDemo extends BodyTagSupport {
/**
*
*/
private static final long serialVersionUID = 1L;
private boolean test;
public boolean isTest() {
return test;
}
public void setTest(boolean test) {
this.test = test;
}
@Override
public int doStartTag() throws JspException {
return test? EVAL_BODY_INCLUDE:SKIP_BODY;
}
@Override
public int doEndTag() throws JspException {
return super.doEndTag();
}
@Override
public int doAfterBody() throws JspException {
return super.doAfterBody();
}
}
package com.lrc.jsp;
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 ForeachDemo extends BodyTagSupport {
private static final long serialVersionUID = 1L;
private String var;
private List<Object> items=new ArrayList<>();
/**
* 执行完这个方法的时候,var所代表的指针要往下移
*/
@Override
public int doStartTag() throws JspException {
if(items.size()==0) {
return SKIP_BODY;
}else {
Iterator<Object> it=items.iterator();
pageContext.setAttribute(var, it.next());
pageContext.setAttribute("it", it);
return EVAL_BODY_INCLUDE;
}
}
@Override
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_INCLUDE;
}
return EVAL_PAGE;
}
@Override
public int doEndTag() throws JspException {
return super.doEndTag();
}
public String getVar() {
return var;
}
public void setVar(String var) {
this.var = var;
}
}
package com.lrc.jsp;
public class Student {
private String id;
private String name;
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 Student(String id, String name) {
super();
this.id = id;
this.name = name;
}
public Student() {
super();
}
}
package com.lrc.jsp;
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;
public class SelectDemo extends BodyTagSupport {
/**
* 值的传递 id name
* 数据源 items
* 展示列与数据存储列与实体类对应关系 textKey textVal
* 数据回显 selectdVal
* 可能下拉框有默认值(头标签)handerTextKey handerTextVal
*/
private static final long serialVersionUID = 1L;
private String id;
private String name;
private List<Object> items=new ArrayList<Object>();
private String textKey;//textKey=id
private String textVal;
private String selectdVal;
private String headerTextKey;
private String headerTextVal;
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 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 getSelectdVal() {
return selectdVal;
}
public void setSelectdVal(String selectdVal) {
this.selectdVal = selectdVal;
}
@Override
public int doStartTag() throws JspException {
JspWriter out=pageContext.getOut();
try {
out.print(toHtml());
} catch (Exception e) {
e.printStackTrace();
}
return super.doStartTag();
}
private String toHtml() throws Exception {
StringBuffer sb=new StringBuffer();
sb.append("<select id='"+id+"' name='"+name+"'>");
if(!(headerTextKey==null || "".equals(headerTextKey) || headerTextVal==null || "".equals(headerTextVal))) {
sb.append("<option selected value='"+headerTextKey+"'>"+headerTextVal+"</option>");
}
String value;
String html;
for (Object obj : items) {
Field textKeyField=obj.getClass().getDeclaredField(textKey);
textKeyField.setAccessible(true);
value=(String) textKeyField.get(obj);
html=(String) PropertyUtils.getProperty(obj, textVal);
if(value.equals(selectdVal)) {
sb.append("<option selected value='"+value+"'>"+html+"</option>");
}else {
sb.append("<option value='"+value+"'>"+html+"</option>");
}
}
sb.append("</select>");
return sb.toString();
}
}
<?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>lrc 1.1 core library</description>
<display-name>lrc</display-name>
<tlib-version>1.1</tlib-version>
<short-name>c</short-name>
<uri>/lrc</uri>
<tag>
<!-- 标签库中的标签名 -->
<name>set</name>
<!-- 标签对应的后台助手类 -->
<tag-class>com.lrc.jsp.SetDemo</tag-class>
<!-- 标签类别 -->
<body-content>JSP</body-content>
<attribute>
<!-- 自定义标签携带的属性 -->
<name>var</name>
<!-- 属性值是否必填 -->
<required>true</required>
<!-- 是否支持表达式(EL/ognl) -->
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>out</name>
<tag-class>com.lrc.jsp.OutDemo</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>lf</name>
<tag-class>com.lrc.jsp.IfDemo</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>foreach</name>
<tag-class>com.lrc.jsp.ForeachDemo</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>select</name>
<tag-class>com.lrc.jsp.SelectDemo</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>selectdVal</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>headerTextKey</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>headerTextVal</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>
<%@page import="com.lrc.jsp.Student"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib uri="/lrc" prefix="l"%>
<!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>
<l:set var="name" value="momo"></l:set>
<l:out value="${name }"></l:out>
<l:lf test="true">lolo</l:lf>
<l:lf test="false">aaaaaa</l:lf>
<%
List l = new ArrayList();
l.add(new Student("1", "aa"));
l.add(new Student("2", "bb"));
l.add(new Student("3", "cc"));
l.add(new Student("4", "dd"));
request.setAttribute("stus", l);
%>
<%-- <l:foreach items="${stus }" var="stu">
${stu.id },${stu.name }<br>
</l:foreach> --%>
<l:select headerTextKey="-1" headerTextVal="===请选择===" textVal="name" selectdVal="2" items="${stus }" textKey="id">
</l:select>
</body>
</html>