1、var
2、items
FoeachTag类代码:
package com.xnx.tag;
import java.util.Iterator;
import java.util.List;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.tagext.BodyTagSupport;
/**
*
* 分析会有两个属性:
* items:List
public class FoeachTag extends BodyTagSupport{
private String var;
private List<Object> items;
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();
//
// var = c,it.next()是集合中的某一个对象
// pageContext.setAttribute("c", it.next());
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;
}
else {
return EVAL_PAGE;
}
}
}
测试代码:
<%
List<Teacher> list=new ArrayList<>();
list.add(new Teacher("t001","zs"));
list.add(new Teacher("t002","ls"));
list.add(new Teacher("t003","ll"));
request.setAttribute("list", list);
%>
<z:for items="${list}" var="t">
${t.tid}:${t.name}
</z:for>
配置文件代码:
<tag>
<name>for</name>
<tag-class>com.xnx.tag.FoeachTag</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>
1、id
2、name
3、items
4、textKey
5、textVal
6、headerTextKey
7、headerTextVal
8、selectedVal
SelectTag类代码:
package com.xnx.tag;
import java.io.IOException;
import java.lang.reflect.Field;
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;
/**
* 1.省略遍历的过程
*
* 2.当前数据回显时,无需增加if判断,无需增加新的代码
*
* 分析:
* 1.后台要遍历-->数据源-->items
* 2.需要一个对象的属性代表下拉框对应的展示内容-->textVal
* 3.需要一个对象的属性代表下拉框对应的展示内容-->textVal
* 4.默认的头部选项展示内容-->headerTextVal
* 5.默认的头部选项值--》headerTextKey
* 6.数据中存储的值,为了方便做数据回显-->selectedVal
* 7.id
* 8.name
* 9.cssStyle..
* @author xnx
*
* 2022年6月21日上午8:37:16
*/
public class SelectTag extends BodyTagSupport{
private List<Object> items;
private String textVal;//teacher的name
private String textKey;
private String headerTextVal;
private String headerTextKey;
private String selectedVal;
private String id;
private String name;
@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(");
if(headerTextVal!=null&&!"".equals(headerTextVal)) {
sb.append("+headerTextVal+"");
}
for (Object obj : items) {
//
Field textKeyFiled = obj.getClass().getDeclaredField(textKey);
textKeyFiled.setAccessible(true);
Object value = textKeyFiled.get(obj);//真正下拉框展示的值
if(selectedVal!=null&&!"".equals(selectedVal)&&selectedVal.equals(value)) {
sb.append("+PropertyUtils.getProperty(obj, textVal)+"");
}else {
sb.append("+PropertyUtils.getProperty(obj, textVal)+"");
}
}
sb.append("");
return sb.toString();
}
public List<Object> getItems() {
return items;
}
public void setItems(List<Object> items) {
this.items = items;
}
public String getTextVal() {
return textVal;
}
public void setTextVal(String textVal) {
this.textVal = textVal;
}
public String getTextKey() {
return textKey;
}
public void setTextKey(String textKey) {
this.textKey = textKey;
}
public String getHeaderTextVal() {
return headerTextVal;
}
public void setHeaderTextVal(String headerTextVal) {
this.headerTextVal = headerTextVal;
}
public String getHeaderTextKey() {
return headerTextKey;
}
public void setHeaderTextKey(String headerTextKey) {
this.headerTextKey = headerTextKey;
}
public String getSelectedVal() {
return selectedVal;
}
public void setSelectedVal(String selectedVal) {
this.selectedVal = 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;
}
}
teacher类代码:
package com.xnx.entity;
public class Teacher {
private String tid;
private String name;
public String getTid() {
return tid;
}
public void setTid(String tid) {
this.tid = tid;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Teacher() {
// TODO Auto-generated constructor stub
}
public Teacher(String tid, String name) {
super();
this.tid = tid;
this.name = name;
}
}
配置文件代码:
<tag>
<name>select</name>
<tag-class>com.xnx.tag.SelectTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>textVal</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>textKey</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>headerTextVal</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>headerTextKey</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>selectedVal</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>id</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>name</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
测试代码:
<select>
<option value="1">zhangsan</option>
<option value="2">lisi</option>
</select>
<z:select selectedVal="t002" id="mySel" headerTextKey="-1" headerTextVal="===请选择===" textKey="tid" textVal="name" items="${list}"></z:select>
checkTag类代码:
package com.xnx.jsp;
import java.lang.reflect.Field;
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;
/**
* name
* id
* 代表复选框的展示内容 fxKey
* 代表复选框的value fxVal
* 数据回显 checkedVal
* 数据源 items
*
* @author xnx
*
* 2022年6月21日上午10:35:54
*/
public class CheckTag extends BodyTagSupport{
private List<Object> items;
private String fxKey;
private String fxVal;
private String name;
private String id;
private List<Object> checkedVal;
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.write(toHtml());
} catch (Exception e) {
e.printStackTrace();
}
return super.doStartTag();
}
private String toHtml() throws Exception{
StringBuffer sb=new StringBuffer();
for (Object obj : items) {
Field hx = obj.getClass().getDeclaredField(fxKey);
hx.setAccessible(true);
Object value = hx.get(obj);
if(checkedVal.contains(value)) {
// if(checkedVal.equals(value)&&checkedVal!=null&&!"".equals(checkedVal)) {
sb.append(""+PropertyUtils.getProperty(obj, fxVal)+"");
}
else {
sb.append(""+PropertyUtils.getProperty(obj, fxVal)+"");
}
}
return sb.toString();
}
public List<Object> getItems() {
return items;
}
public void setItems(List<Object> items) {
this.items = items;
}
public String getFxKey() {
return fxKey;
}
public void setFxKey(String fxKey) {
this.fxKey = fxKey;
}
public String getFxVal() {
return fxVal;
}
public void setFxVal(String fxVal) {
this.fxVal = fxVal;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public List<Object> getCheckedVal() {
return checkedVal;
}
public void setCheckedVal(List<Object> checkedVal) {
this.checkedVal = checkedVal;
}
}
checkbox配置:
<tag>
<name>check</name>
<tag-class>com.xnx.jsp.CheckTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>fxVal</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>fxKey</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>checkedVal</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>id</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>name</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
测试代码:
<%@page import="java.util.ArrayList"%>
<%@page import="com.xnx.entity.Hobby"%>
<%@page import="java.util.List"%>
<%@ page language="java" contentType="text/html; charset=UTF-8"
pageEncoding="UTF-8"%>
<%@taglib prefix="z" uri="http://jsp.veryedu.cn" %>
<!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>
<%
List<Hobby> list=new ArrayList<>();
list.add(new Hobby("h001","aa"));
list.add(new Hobby("h002","bb"));
list.add(new Hobby("h003","cc"));
list.add(new Hobby("h004","dd"));
request.setAttribute("list", list);
List<Object> ls=new ArrayList<>();
ls.add("h001");
request.setAttribute("ls", ls);
%>
<z:check checkedVal="${ls}" items="${list}" fxVal="hname" fxKey="hid"></z:check>
</body>
</html>