自定义标签public class DepartmentCustomSelectTag extends TagSupport {
private Object departments;
    
public void setDepartments(Object departments) throws JspException {
this.departments = ExpressionEvaluatorManager.evaluate("departments",departments.toString(), Object.class, this, pageContext);
}    public Object getDepartments() {
return departments;
} @Override
public int doEndTag() throws JspException {
try {
JspWriter out=pageContext.getOut();
out.println("</select>");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return EVAL_PAGE;
} @Override
public int doStartTag() throws JspException {
try {
JspWriter out = pageContext.getOut();
out.println("<select name='department' id='department'>");
System.out.println("--->"+departments.getClass().getName()+",value:"+departments);
if (departments instanceof List) {
List<Department> departs=(List<Department>)departments;
for (Department d : departs) {
out.println("<option value='" + d.getId() + "'>"+ d.getNames() + "</option>");
}
} else {
out.println("<option value=''>==========</option>");
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SKIP_BODY;
}

}
描述文件tld<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN"
                        "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
 <tlib-version>1.0</tlib-version>
 <jsp-version>2.0</jsp-version>
 <short-name>depart</short-name>
 <tag>
     <name>department</name>
     <tag-class>cn.ytdongli.util.DepartmentCustomSelectTag</tag-class>
     <body-content>empty</body-content>
     <attribute>
         <name>departments</name>
         <required>true</required>
         <rtexprvalue>true</rtexprvalue>
     </attribute>
 </tag>
</taglib>
调用页面
<%@ taglib prefix="depart" uri="/WEB-INF/angle-depart.tld"%>  
...
<td><depart:department departments="${requestScope.departs}" /></td>如何让它传进的是列表,不是字符串

解决方案 »

  1.   

    楼主你可以这样,传递一个字符串,departs
    分页从page,request,session,application取值
    request.getAttribute("departs");// el表达式也是这么做的
      

  2.   

    我知道可以!但这样不能复用!用tag不可以复用么?
      

  3.   

    怎么不可以复用啦??
    <depart:department departments="departs" />在标签中获取到这个departs这个字符串,你去作用域中拿数据就好啦,跟使用el表达式一样
      

  4.   


    哪我为什么要写tag呢!我理解错了么?我就是想把从请求拿集合,遍历集合的代码封装到了tag中!不然不要到处复制.
      

  5.   

    噢!我理解了!你说的是只把作用域中的集合名称传给tag,我想的是直接把集合传进tag
      

  6.   

    3ks!用你的方法我刚才试了完全可以!应该也可以传集合进去吧!我就想知道怎么把集合传进去!另外请教一下在自定义tag中调业务接口是不是不推荐的
      

  7.   

    问题已解决!传集合是可以的!在标签类中要用Collection类型,循环要用Iterator