1.1 助手类
package com.leiliang.jsp;
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 List<Object> items;
private String var;
private String varStatus;//保存index ,count对象属性的key
public static class Status{
public int index=0;
public int getIndex() {
return index;
}
public void setIndex(int index) {
this.index = index;
}
public int getCount() {
return this.index+1;
}
void increment() {
this.index++;
}
}
public String getVarStatus() {
return varStatus;
}
public void setVarStatus(String varStatus) {
this.varStatus = varStatus;
}
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;
}
@Override
public int doStartTag() throws JspException {
// TODO Auto-generated method stub
if(null!=varStatus && !"".equals(varStatus)) {
pageContext.setAttribute(varStatus, new Status());
}
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 doStartTag();
}
@Override
public int doAfterBody() throws JspException {
// TODO Auto-generated method stub
Iterator<Object> it = (Iterator<Object>) pageContext.getAttribute("it");
while(it.hasNext()) {
if(null!=varStatus && !"".equals(varStatus)) {
Status status = (com.tanle.jsp.ForeachTag.Status) pageContext.getAttribute(varStatus);
status.increment();
pageContext.setAttribute(varStatus, status);
}
Object next = it.next();
pageContext.setAttribute(var, next);
return EVAL_BODY_AGAIN;
}
return super.doAfterBody();
}
}
1.2 tld文件配置
<tag>
<name>forEach</name>
<tag-class>com.leiliang.jsp.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>
<attribute>
<name>varStatus</name>
<required>false</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
2.1 助手类
package com.leiliang;
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.BeanUtils;
public class SelectTag extends BodyTagSupport {
private String headValue = "-1";// 头value
private String headText = "---请选择---";// 头文本
private String valueProperty;// java对象属性名
private String textProperty;// java对象属性名
private List<Object> items;// 数据源
private String select;// 判断默认选中的id
public String getHeadValue() {
return headValue;
}
public void setHeadValue(String headValue) {
this.headValue = headValue;
}
public String getHeadText() {
return headText;
}
public void setHeadText(String headText) {
this.headText = headText;
}
public String getValueProperty() {
return valueProperty;
}
public void setValueProperty(String valueProperty) {
this.valueProperty = valueProperty;
}
public String getTextProperty() {
return textProperty;
}
public void setTextProperty(String textProperty) {
this.textProperty = textProperty;
}
public List<Object> getItems() {
return items;
}
public void setItems(List<Object> items) {
this.items = items;
}
public String getSelect() {
return select;
}
public void setSelect(String select) {
this.select = select;
}
@Override
public int doStartTag() throws JspException {
JspWriter out = pageContext.getOut();
try {
out.print(toHtml());
} catch (IOException | IllegalAccessException | InvocationTargetException | NoSuchMethodException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return super.doStartTag();
}
public String toHtml() throws IllegalAccessException, InvocationTargetException, NoSuchMethodException {
// 利用stringbuffer拼接标签
StringBuffer sb = new StringBuffer();
sb.append("<select>");
sb.append("<option value='" + this.headValue + "'>" + this.headText + "</option>");
if (items != null && items.size() > 0) {
for (Object object : items) {
// 利用反射获取到页面传过来的属性名对应的属性值
String deptId = BeanUtils.getProperty(object, valueProperty);
String deptName = BeanUtils.getProperty(object, textProperty);
if (deptId.equals(select)) {
sb.append("<option selected value='" + deptId + "'>" + deptName + "</option>");
}else {
sb.append("<option value='" + deptId + "'>" + deptName + "</option>");
}
}
}
sb.append("</select>");
return sb.toString();
}
}
2.2 tld文件配置
<tag>
<name>select</name>
<tag-class>com.leiliang.SelectTag</tag-class>
<body-content>JSP</body-content>
<attribute>
<name>headValue</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>headText</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>valueProperty</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>textProperty</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
<attribute>
<name>items</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>select</name>
<required>true</required>
<rtexprvalue>false</rtexprvalue>
</attribute>
</tag>
2.3 引用
List<Dept> deptLs = new ArrayList<Dept>();
Dept d = new Dept("1", "aa");
Dept d1 = new Dept("2", "ss");
Dept d2 = new Dept("3", "ff");
Dept d3 = new Dept("4", "ee");
deptLs.add(d);
deptLs.add(d1);
deptLs.add(d2);
deptLs.add(d3);
request.setAttribute("Dept", deptLs);