1. 标签语言特点
<开始标签 属性=“属性值”>标签体</结束标签>
空标签
<开始标签></结束标签>
<开始标签/>
ui标签 c:out
特点是显示数据,并且数据不是来源于标签体的,而是来源于jsp标签本身
控制标签 if /foreach/c
特点是控制的对象是标签体
数据标签 set
特点是存储数据,没有任何的页面效果
2. 自定义标签的开发及使用步骤
2.1 创建一个标签助手类(继承BodyTagSupport)
标签属性必须助手类的属性对应、且要提供对应get/set方法
rtexprvalue
2.2 创建标签库描述文件(tld),添加自定义标签的配置
注:tld文件必须保存到WEB-INF目录或其子目录
2.3 在JSP通过taglib指令导入标签库,并通过指定后缀
访问自定义标签
3. 标签生命周期
SKIP_BODY
3.1 实例化标签助手类->doStartTag()------------->doEndTag()
//主要用开发简单标签
EVAL_BODY_INCLUDE SKIP_BODY
3.2 实例化标签助手类->doStartTag()------------->doAfterBody---------------->doEndTag()…
EVAL_BODY_AGAIN
3.3 …
SKIP_BODY:跳过主体
EVAL_BODY_INCLUDE:计算标签主体内容并[输出]
EVAL_BODY_BUFFERED:计算标签主体内容并[缓存]
EVAL_PAGE:计算页面的后续部分
SKIP_PAGE:跳过页面的后续部分
EVAL_BODY_AGAIN:再计算主体一次
4.使用标签库的时候需要导入jar包
代码:
1.SetTag
package com.lx.jsp;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* @author lixiang
*
*/
public class SetTag extends BodyTagSupport {
private static final long serialVersionUID = 5501056294181190444L;
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;
}
}
2.OutTag
package com.lx.jsp;
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 static final long serialVersionUID = 3557870732871287252L;
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 super.doStartTag();
}
}
3.IfTag
package com.lx.jsp;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
* if控制标签(页面展示效果依赖的是标签体)
* @author lixiang
*
*/
public class IfTag extends BodyTagSupport {
private static final long serialVersionUID = 9214013706214652844L;
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;
}
}
4.FreachTag
package com.lx.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 FreachTag extends BodyTagSupport {
private static final long serialVersionUID = -8173894988754949262L;
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;
}
@Override
public int doStartTag() throws JspException {
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");
pageContext.setAttribute("it",it);
if(it.hasNext()) {
pageContext.setAttribute(var, it.next());
pageContext.setAttribute("it",it);
return EVAL_BODY_AGAIN;
}else {
return EVAL_PAGE;
}
}
}
6.ServerTag
package com.lx.jsp;
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;
public class ServerTag extends BodyTagSupport{
private static final long serialVersionUID = 7190263042105756953L;
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 {
out.print(toHTML());
} catch (Exception e) {
// TODO Auto-generated catch block
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 = "";
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);
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();
}
}
7.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>JSTL 1.1 core library</description>
<display-name>JSTL core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>c</short-name>
<uri>/lx</uri>
<tag>
<name>set</name>
<tag-class> com.lx.jsp.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.lx.jsp.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.lx.jsp.IfTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>FreachTag</name>
<tag-class> com.lx.jsp.FreachTag</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>
<tag>
<name>server</name>
<tag-class>com.lx.jsp.ServerTag</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>
8.jsp页面
<%@page import="com.lx.jsp.Task"%>
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=utf-8"
pageEncoding="utf-8"%>
<%@taglib prefix="lx" uri="/lx" %>
<!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>
<lx:set var="name" value="laowang"></lx:set>
<lx:out value="${name}"></lx:out>
<lx:if test="true">ls</lx:if>
<lx:if test="false">ls</lx:if>
<%
List list=new ArrayList();
list.add(new Task("1","老王"));
list.add(new Task("2","老陈"));
list.add(new Task("3","老刘"));
list.add(new Task("4","老宋"));
list.add(new Task("5","老李"));
pageContext.setAttribute("teas", list);
%>
<lx:FreachTag items="${teas }" var="t">
${t.tid },${t.tname }<br>
</lx:FreachTag>
<h2>下拉框查询</h2>
<lx:server textkey="tid" items="${teas}" textval="tname" ></lx:server>
<lx:server headkey="-1" headval="-请选择-" textkey="tid" items="${teas}" textval="tname"></lx:server>
<h2>修改页面下拉框</h2>
<lx:server textkey="tid" items="${teas}" textval="tname" selectedval="老王"></lx:server>
</body>
</html>
运行后的结果: