自定义标签的开发及使用步骤
1、 创建一个标签助手类(继承BodyTagSupport) 标签属性必须助手类的属性对应、且要提供对应get/set方法
2、创建标签库描述文件(tld),添加自定义标签的配置 注意:tld文件必须保存到WEB-INF目录或其子目录
3、在JSP通过taglib指令导入标签库,并通过指定后缀 访问自定义标签
UI标签
- out 标签助手类
package com.houzhihong.jsp.Tag;
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 = 1L;
private Object value;
public Object getValue() {
return value;
}
public void setValue(Object value) {
this.value = value;
}
public OutTag() {}
public OutTag(Object value) {
this.value = value;
}
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.println(value==null?"":value.toString());
} catch (IOException e) {
throw new RuntimeException(e);
}
return SKIP_BODY;
}
}
- select 标签助手类
1.值得传递 id.name
2.数据源 items
3.展示列与数据存储列与实体类的对应关系 textKey textVal
4.数据回显 selectedtVal
5.可能下拉框有默认值(头标签) headerTextKey , headerTextVal
package com.houzhihong.jsp.Tag;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
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 SelectTag extends BodyTagSupport {
private static final long serialVersionUID = 1L;
private List<Object> items;
private String name;
private String valueKey;// 保存option中value的key
private String textKey;// 保存option中text的key
private String cssStyle;
private String headKey;
private String headValue;
public String getSelected() {
return selected;
}
public void setSelected(String selected) {
this.selected = selected;
}
private String selected; //自定义默认选中
public String getHeadValue() {
return headValue;
}
public void setHeadValue(String headValue) {
this.headValue = headValue;
}
public String getHeadKey() {
return headKey;
}
public void setHeadKey(String headKey) {
this.headKey = headKey;
}
public SelectTag(String cssStyle) {
super();
this.cssStyle = cssStyle;
}
public String getCssStyle() {
return cssStyle;
}
public void setCssStyle(String cssStyle) {
this.cssStyle = cssStyle;
}
public List<Object> getItems() {
return items;
}
public void setItems(List<Object> items) {
this.items = items;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValueKey() {
return valueKey;
}
public void setValueKey(String valueKey) {
this.valueKey = valueKey;
}
public String getTextKey() {
return textKey;
}
public void setTextKey(String textKey) {
this.textKey = textKey;
}
public SelectTag() {
}
public SelectTag(List<Object> items, String name, String valueKey, String textKey) {
super();
this.items = items;
this.name = name;
this.valueKey = valueKey;
this.textKey = textKey;
}
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
String html = toHtml();
System.out.println(html);
out.println(html);
} catch (Exception e) {
throw new RuntimeException(e);
}
return super.doStartTag();
}
private String toHtml() throws Exception {
StringBuffer sb = new StringBuffer();
// 拼接
// <select name="" style="">
sb.append("<select name='" + name + "'");
sb.append("style='"+cssStyle+"");
sb.append(">");
// <option value=""></option>
if (null != items && 0 != items.size()) {
if(null!=headKey && !"".equals(headKey.trim())) {
sb.append("<option value='" + headKey + "'>" + headValue + "</option>");
}
Object value = null;
Object text = null;
for (Object object : items) {
// 反射
value = PropertyUtils.getProperty(object, valueKey);
text = PropertyUtils.getProperty(object, textKey);
if(value.toString().equals(selected.trim())) {
sb.append("<option selected value='" + value + "'>" + text + "</option>");
} else {
sb.append("<option value='" + value + "'>" + text + "</option>");
}
}
}
sb.append("</select>");
return sb.toString();
}
}
控制标签
- if 标签助手类
package com.houzhihong.jsp.Tag;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class IfTag extends BodyTagSupport {
private static final long serialVersionUID = 1L;
private Boolean test;
public Boolean getTest() {
return test;
}
public void setTest(Boolean test) {
this.test = test;
}
public IfTag() {}
public IfTag(Boolean test) {
super();
this.test = test;
}
@Override
public int doStartTag() throws JspException {
if(test) {
return EVAL_BODY_INCLUDE;
}
return SKIP_BODY;
}
}
- forEach 标签助手类
package com.houzhihong.jsp.Tag;
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 = 1L;
private List<Object> items;
private String var; //保存集合中元素的key
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 ForeachTag() {}
public ForeachTag(List<Object> items, String var) {
super();
this.items = items;
this.var = var;
}
@Override
public int doStartTag() throws JspException {
if(null!=items && 0!=items.size()) {
Iterator<Object> it = items.iterator();
Object next = it.next();
pageContext.setAttribute(var, next);
pageContext.setAttribute("it", it);
return EVAL_BODY_INCLUDE;
}
return super.doStartTag();
}
@Override
public int doAfterBody() throws JspException {
// TODO Auto-generated method stub
Iterator<Object> it =(Iterator<Object>) pageContext.getAttribute("it");
while(it.hasNext()) {
Object next = it.next();
pageContext.setAttribute(var, next);
return EVAL_BODY_AGAIN;
}
return super.doAfterBody();
}
}
数据标签
- set 标签助手类
package com.houzhihong.jsp.Tag;
import java.util.List;
import java.util.Set;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
import com.houzhihong.dao.DeptDao;
import com.houzhihong.entity.Dept;
public class SetTag extends BodyTagSupport {
private static final long serialVersionUID = 1445153725840943379L;
private String value;//保存数据的key
private DeptDao deptDao = new DeptDao();
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public SetTag() {}
public SetTag(String value, DeptDao deptDao) {
this.value = value;
this.deptDao = deptDao;
}
@Override
public int doStartTag() throws JspException {
List<Dept> list = deptDao.list();
pageContext.setAttribute(value, list);
return super.doStartTag();
}
}
5.一一对应配置好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>h</short-name>
<uri>/houzhihong</uri>
<!--Out标签 -->
<tag>
<name>out</name>
<tag-class>com.houzhihong.jsp.Tag.OutTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<!--if标签 -->
<tag>
<name>if</name>
<tag-class>com.houzhihong.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.houzhihong.jsp.Tag.ForeachTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>var</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
<!--set标签 -->
<tag>
<name>set</name>
<tag-class>com.houzhihong.jsp.Tag.SetTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>value</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
<!--select标签 -->
<tag>
<name>select</name>
<tag-class>com.houzhihong.jsp.Tag.SelectTag</tag-class>
<body-content>empty</body-content>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>name</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>valueKey</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>textKey</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>cssStyle</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>headKey</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>headValue</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>selected</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
</taglib>
6.selectTag助手类使用到的反射
6.1需要使用到的两个jar包
7.最后附上运行效果和jsp案列
<%@page import="java.util.ArrayList"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib uri="/houzhihong" prefix="h"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<!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>
<h3>自定义标签二</h3>
<%
List<String> list = new ArrayList<String>();
list.add("aa");
list.add("bb");
list.add("cc");
request.setAttribute("name", list);
String sname = "pdd";
pageContext.setAttribute("sname", sname);
%>
<!--自定义Out标签 -->
<h4>自定义out标签</h4>
<h:out value="${name }" />
<!--自定义if标签 -->
<h4>自定义if标签</h4>
<h:if test="${sname eq 'pdd' }">
pdd猪头
</h:if>
<!--自定义forEach标签 -->
<h4>自定义forEach标签</h4>
<ul>
<h:forEach items="${name }" var="n">
<li>${n }</li>
</h:forEach>
</ul>
<!--自定义Set标签 -->
<h4>自定义set标签</h4>
<!-- 设置值 -->
<h:set value="deptlist" />
<!--通过out取值输出 -->
<h:out value="${deptlist }" />
<br/>
<!-- <select name="" style="">
<option value="-1">---请选择---</option>
<option value="1">长沙</option>
<option selected value="2">株洲</option>
</select> -->
<br/>
<!--自定义select标签 -->
<h4>自定义select标签</h4>
<h:select items="${deptlist }" valueKey="deptId" textKey="deptName"
cssStyle="width:150px" headKey="-1" headValue="---请选择---" selected="1"/>
</body>
</html>