set设置数据标签助手类
package com.jsp;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
public class SetTag extends BodyTagSupport {
private static final long serialVersionUID = 4965191755704898746L;
private String var;
private Object value;
@Override
public int doStartTag() throws JspException {
pageContext.setAttribute(var, value);
return SKIP_BODY;
}
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;
}
}
out输出数据标签助手类
package com.jsp;
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 = 6097318304486137249L;
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.toString());
} catch (Exception e) {
e.printStackTrace();
}
return SKIP_BODY;
}
}
foreach遍历输出数据标签助手类
package com.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() {
}
}
package com.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 ForeachTag extends BodyTagSupport {
private static final long serialVersionUID = -2643407962233076616L;
private String var;
private List<Object> items =new ArrayList<>();
@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_AGAIN;
}
return EVAL_PAGE;
}
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;
}
}
select下拉框数据标签助手类
package com.jsp;
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 SelectTag extends BodyTagSupport {
private static final long serialVersionUID = 5375902073989817712L;
private String id;
private String name;
private List<Object> items =new ArrayList<Object>();
private String textKey;
private String textVal;
private String selectedVal;
private String headerTextKey;
private String headerTextVal;
@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 NoSuchFieldException, SecurityException {
StringBuffer sb =new StringBuffer();
sb.append("<select id='"+id+"' name='"+name+"'>");
if(!(headerTextKey ==null || "".equals(headerTextKey)||
headerTextVal ==null ||"".equals(headerTextVal) )) {
sb.append("<option checked value='"+headerTextKey+"'>"+headerTextVal+"</option>");
}
String value = null;
String html = null;
for (Object obj : items) {
Field field = obj.getClass().getDeclaredField(textKey);
field.setAccessible(true);
try {
value = (String) field.get(obj);
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
try {
html =(String) PropertyUtils.getProperty(obj,textVal);
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (InvocationTargetException e) {
e.printStackTrace();
} catch (NoSuchMethodException e) {
e.printStackTrace();
}
if(value.equals(selectedVal)) {
sb.append("<option checked 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 getSelectedVal() {
return selectedVal;
}
public void setSelectedVal(String selectedVal) {
this.selectedVal = selectedVal;
}
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;
}
}
给标签配置标签名和属性名
<?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>linfan 1.1 core library</description>
<display-name>linfan core</display-name>
<tlib-version>1.1</tlib-version>
<short-name>c</short-name>
<uri>/linfan</uri>
<tag>
<!-- 标签库中的标签名 -->
<name>set</name>
<!-- 标签对应的助手类的全路径名 -->
<tag-class>com.jsp.SetTag</tag-class>
<!-- jsp -->
<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.jsp.OutTag</tag-class>
<!-- jsp -->
<body-content>JSP</body-content>
<attribute>
<!-- 属性名 -->
<name>value</name>
<!-- 属性值是否必填 -->
<required>true</required>
<!-- 是否支持表达试 -->
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<!-- 标签库中的标签名 -->
<name>foreach</name>
<!-- 标签对应的助手类的全路径名 -->
<tag-class> com.jsp.ForeachTag</tag-class>
<!-- jsp -->
<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>select</name>
<!-- 标签对应的助手类的全路径名 -->
<tag-class>com.jsp.SelectTag</tag-class>
<!-- jsp -->
<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>selectedVal</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>
</taglib>
测试出数据
<%@page import="com.zking.jsp.day02.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="/linfan" prefix="z" %>
<!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>
<<p>1.利用set设置值并且用out标签输出的值</p>
<z:set var="name" value="zhansan"></z:set>
<z:out value="${name }"></z:out>
<br>
<p>2.利用foreach遍历输出的值</p>
<%
List list =new ArrayList();
list.add(new Student("001","xiaocheng"));
list.add(new Student("002","xiaoli"));
list.add(new Student("003","xiaowang"));
request.setAttribute("stus", list);
%>
<z:foreach items="${stus }" var="stu">
${stu.id },${stu.name }<br>
</z:foreach>
<p>3.利用select遍历的值</p>
<z:select headerTextKey="-1" headerTextVal="===请选择===" textVal="name" items="${stus }" selectedVal="2" textKey="id"></z:select>
</body>
</html>
- 输出为
自定义一个checkbox标签助手类
public class CheckboxTag extends BodyTagSupport {
private static final long serialVersionUID = -5301514341025414346L;
private String [] items;
private String name;
public String[] getItems() {
return items;
}
public void setItems(String[] items) {
this.items = items;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public CheckboxTag(String[] items, String name) {
super();
this.items = items;
this.name = name;
}
public CheckboxTag() {
super();
}
@Override
public int doStartTag() throws JspException {
JspWriter out =pageContext.getOut();
try {
out.print(toHTML());
} catch (IOException e) {
e.printStackTrace();
}
return super.doStartTag();
}
private String toHTML() {
StringBuffer sb =new StringBuffer();
for (String it : items) {
sb.append("<input type='checkbox' value ='"+it+"' name ='"+name+"' >"+it+" " );
}
return sb.toString();
}
<tag>
<name>checkbox</name>
<tag-class>com.zking.jsp.day02.CheckboxTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>name</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<p>4.复选框</p>
<%
String [] str ={"aaa","aaa","aaa"};
request.setAttribute("str", str);
%>
<z:checkbox items="${str }" name="name"></z:checkbox>
- 运行结果