目录
1.foreach标签
<c:forEach items="${clas12}" var="c"
分析会有两个属性
items:List《Object》
var:String
分析路线
第二条:eval_body_include
第三条:eval_body_again
package com.hz.tag;
/**
* <c:forEach items="${clas12}" var="c"
* 分析会有两个属性
* items:List《Object》
* var:String
* 分析路线
* 第二条:eval_body_include
* 第三条:eval_body_again
* @author Administrator
*
*/
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 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();
// <option value="where cid='${c.cid}'">${c.cname}</option>
// var = c,it.next()是集合的的摸一个对象
// pageContext.setAttribute("c", items.get(0));
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;
}
}
}
2.select标签
1、省略遍历的过程
<z:select></select>
2、当做回显时、无需增加if判断、无需增加新的代码
分析:
1、后台要遍历-->数据源-->items
2、需要一个对象的属性代表下拉框对应的展示内容-->textVal
3、需要一个对象的属性代表下拉框对应的value值-->textKey
4、默认的头部选项展示内容-->headerTextVal
5、默认的头部选项值-->headerTextKey
6、数据库中存储的值,为了方便做数据回显->selectedVal
7、id
8、name
9、cssStyle
package com.hz.tag;
import java.io.IOException;
import java.lang.reflect.Field;
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;
/**
* 1、省略遍历的过程
* <z:select></select>
* 2、当做回显时、无需增加if判断、无需增加新的代码
*
*
* 分析:
* 1、后台要遍历-->数据源-->items
* 2、需要一个对象的属性代表下拉框对应的展示内容-->textVal
* 3、需要一个对象的属性代表下拉框对应的value值-->textKey
* 4、默认的头部选项展示内容-->headerTextVal
* 5、默认的头部选项值-->headerTextKey
* 6、数据库中存储的值,为了方便做数据回显->selectedVal
* 7、id
* 8、name
* 9、cssStyle
* @author Administrator
*
*/
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 NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException, InvocationTargetException, NoSuchMethodException {
StringBuffer sb = new StringBuffer();
sb.append("<select id='"+id+"' name='"+name+"'>");
if(headerTextVal!=null && !"".equals(headerTextVal)) {
sb.append("<option value='"+headerTextKey+"'>"+headerTextVal+"</option>");
}
for (Object obj : items) {
Field textKeyField = obj.getClass().getDeclaredField(textKey);
textKeyField.setAccessible(true);
Object value = textKeyField.get(obj);//真正下拉框展示的值
if(selectedVal!=null && !"".equals(selectedVal) && selectedVal.equals(value)) {
sb.append("<option selected value='"+value+"'>"+PropertyUtils.getProperty(obj, textVal)+"</option>");
}else {
sb.append("<option value='"+value+"'>"+PropertyUtils.getProperty(obj, textVal)+"</option>");
}
}
sb.append("</select>");
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;
}
}
本文介绍了如何自定义JSP标签库,包括`<c:forEach>`标签的实现,用于遍历集合并设置变量,以及一个简化版的`<select>`标签,它自动处理遍历、回显等功能。这两个标签简化了前端页面的代码,提高了开发效率。

被折叠的 条评论
为什么被折叠?



