html:options是Struts中比较复杂的一个tage lib,用法灵活,但是Sturts提供的源码exercise taglib中没有提出常用jsp+ActionForm这样形式的最直接的总结,现从中总结如下,分两种情况:数组和Collection。需求,要达到:
      <select name="beanCollectionSelect" multiple="multiple" size="10"><option value="Value 0">Label 0</option><option value="Value 1" selected="selected">Label 1</option><option value="Value 2">Label 2</option><option value="Value 3" selected="selected">Label 3</option><option value="Value 4">Label 4</option><option value="Value 5" selected="selected">Label 5</option><option value="Value 6">Label 6</option><option value="Value 7">Label 7</option><option value="Value 8">Label 8</option><option value="Value 9">Label 9</option></select>
要实现上述效果,需要两步:
第一:设置ActionForm,
也分两小步:第一小步必须在ActionForm中,有一句
private Collection beanCollection;
public Collection getBeanCollection();Collection beanCollection要确保是一个实现,如ArrayList,如果不是则会报No collection found的错误,Struts的最大不方便就是一旦出问题,定位很难,不知道什么地方使用错误,或忘记设置什么了。因为前面需求中option的value值和label值不一样,那么在beanCollection中保存的就是一个value和label组成的对象,名为LabelValueBean,在LabelValueBean中有两个属性value和label,在程序某个地方要为beanCollection赋值,如:
Vector entries = new Vector(10);            entries.add(new LabelValueBean("Label 0", "Value 0"));            entries.add(new LabelValueBean("Label 1", "Value 1"));            entries.add(new LabelValueBean("Label 2", "Value 2"));            entries.add(new LabelValueBean("Label 3", "Value 3"));            entries.add(new LabelValueBean("Label 4", "Value 4"));            entries.add(new LabelValueBean("Label 5", "Value 5"));            entries.add(new LabelValueBean("Label 6", "Value 6"));            entries.add(new LabelValueBean("Label 7", "Value 7"));            entries.add(new LabelValueBean("Label 8", "Value 8"));            entries.add(new LabelValueBean("Label 9", "Value 9"));
然后执行setBeanCollection(entries);
这样ActionForm中的beanCollection算有值了。
第二小步,需要设置Selected,selected有两种,单选和多选:
在ActionForm中必须有:    private String singleSelect = "Single 5";    public String getSingleSelect() {        return (this.singleSelect);    }    public void setSingleSelect(String singleSelect) {        this.singleSelect = singleSelect;    }或多选,多选必须是数组: private String[] beanCollectionSelect = { "Value 1", "Value 3",                                              "Value 5" };    public String[] getBeanCollectionSelect() {        return (this.beanCollectionSelect);    }    public void setBeanCollectionSelect(String beanCollectionSelect[]) {        this.beanCollectionSelect = beanCollectionSelect;    }第二:在Jsp中写入tang lib语句如下:   <html:select property="beanCollectionSelect" size="10" multiple="true">        <html:optionsCollection name="testbean" property="beanCollection"/>      </html:select>其中testbean是ActionForm的名称。以上是html:options的Collection解决方案,如果option值很少,简单地可以实现为数组,两步:
第一:在ActionForm中,   private String values[] =     { "Magazine", "Journal", "News Paper","Other" };    private String labels[] =     { "L-Magazine", "L-Journal", "L-News Paper","L-Other"};    private String selected = "Magazine";    public String getSelected(){      return selected;    }    public void setSelected(String selected){      this.selected = selected;    }    public String[] getValues(){      return values;    }    public void setValues(String[] values){      this.values = values;    }    public String[] getLabels(){      return values;    }    public void setLabels(String[] labels){      this.labels = labels;    }
第二步在jsp中:
     <html:select property="selected" >            <html:options name="testbean" property="values" labelProperty="label"/>      </html:select>

解决方案 »

  1.   

    看这个可能更直观一些:
    http://www.jdon.com/jive/article.jsp?forum=62&thread=9185
      

  2.   

    还没有别的例子?我想把主要逻辑写在 Action里,FormBean只获取选择结果
      

  3.   

    给你一个示例,这只是一个小程序的一小部分,要是需要给我发消息,讲源代码都给你import java.sql.*;
    import java.util.*;
    public class DepartmentBean {
    private   Connection conn ;

    public DepartmentBean()throws Exception
    {
    this.conn=DataBaseConnection.getConnection();
    }
    public void addDepartment(Department department)throws Exception
    {
     Statement stmt=conn.createStatement();
     stmt.executeUpdate("insert into Department values('"+department.getId()+"','"+department.getName()+"','"+department.getDescription()+"','"+department.getLeader()+"')");
    }
    public Collection getLeaders()throws Exception
    {
     Statement stmt=conn.createStatement();
     ResultSet rst=stmt.executeQuery("select * from Employee where id not in (select leader from Department)");
     Collection ret=new ArrayList();
     while(rst.next())
     {
    Employee temp=new Employee();
    temp.setId(rst.getString("id"));
    temp.setName(rst.getString("name"));
    ret.add(temp);
     }
     return ret;
      }
      
    public Collection getDepartments()throws Exception
    {
     Statement stmt=conn.createStatement();
     ResultSet rst=stmt.executeQuery("select * from Department");
     Collection ret=new ArrayList();
     while(rst.next())
     {
    Department temp=new Department();
    temp.setId(rst.getString("id"));
    temp.setName(rst.getString("name"));
    temp.setDescription(rst.getString("description"));
    temp.setLeader(rst.getString("leader"));
    ret.add(temp);
     }
     return ret;
      }
    }jsp文件:
    <%@page contentType="text/html;charset=gb2312"%>
    <%@ taglib uri="/struts-logic" prefix="logic" %>
    <%@ taglib uri="/struts-bean" prefix="bean" %>
    <%@ taglib uri="/struts-html" prefix="html" %>
    <%@ taglib uri="/jstl/c" prefix="c" %><head>
    <link href="<html:rewrite page="/rr.css" />" rel="stylesheet" type="text/css"><jsp:useBean id="departmentBean" class="beans.DepartmentBean" scope="session"/>//***注意这里的bean的导入,前面的.java文件中函数:public Collection getLeaders()**//
    <c:set var="leaders" value="${departmentBean.leaders}"/>
    <html:html locale="true">
    <title>创建一个部门</title>
    </head>
    <body>
    <center>
    <h1>创建一个部门</h1><logic:messagesPresent>
        <span id="errorsHeader"><bean:message key="errors.validation.header"/></span>
        <html:messages id="error">
          <li><c:out value="${error}"/></li>
        </html:messages>
        <hr>
    </logic:messagesPresent>
     <html:errors/>
    <html:form action="createDepartment.do" method="get">
    <table align="center" bgcolor="#B3C7EA" bordercolor="#FFFFFF" border="1" cellspacing="2" cellpadding="5">
    <tr>
        <td >id:</td>
        <td><html:text property="id"/></td>
    </tr>
    <tr>
        <td >名称:</td>
        <td><html:text property="name"/></td>
    </tr>
    <tr>
        <td>描述:</td>
        <td><html:text property="description"/></td>
    </tr>
    <tr>
        <td>部门管理员:</td>
        <td>     //***注意这里的用法,和前面bean的导入及其bean的初始化*******//
         <html:select name="departmentForm" property="leader">
            <html:options collection="leaders" property="id" labelProperty="name"/>
        </html:select>    </td>
    </tr>
    </table>
    <html:submit><bean:message key="button.submit"/></html:submit>
    </html:form> 
    </center>
    </body>
    </html:html>
      

  4.   

    在程序某个地方要为beanCollection赋值,这是一句不负责的话。到底是哪里?我在ActionForm和Action都试了,不行。最后能在jsp中实现,但在jsp中执行,我想肯定不是理解的方法。还请指教。而且,楼上的两位写的这些东西都上网上的, wxt1013(每晚抱着Java睡觉......) 的内容是最新流行的一本教程上的。
      

  5.   

    我要structs本身的标签,不是JSTL,谁有我想要的例子~~~~