Struts2中的多选下拉列表

时间:2013-12-17 13:00:41

标签: java-ee struts2

<div class="select_wrap">
    <select name="sampleselect"  class="select_field" multiple="multiple" >

        <option>Select Id  </option>
        <s:iterator value="userList" var="usrList" status="s">
        <option value=<s:property value="#usrList"></s:property>><s:property value="#usrList"></s:property></option>

        </s:iterator>
    </select>
    </div>      
<br /><br />
            <s:submit value="Compliance Export" name="Export Data" align="left" action="exportExcelAction4"/>

usrList将从数据库中填充

在我的动作类中

private List<String> sampleselect=new ArrayList<String>();

    public List<String> getSampleselect() {
        return sampleselect;
    }


    public void setSampleselect(List<String> sampleselect) {
        this.sampleselect = sampleselect;
    }

但是这个样本选择没有填充。

2 个答案:

答案 0 :(得分:2)

list属性用于获取可迭代源,而name属性用于设置所选项。还可以使用<s:select/>标记来获得更清晰(更简单)的代码。例如:

struts.xml中

<action name="firstAction" class="foo.bar.FirstAction">
    <result>firstPage.jsp</result>
</action>

<action name="secondAction" class="foo.bar.SecondAction">
    <result>secondPage.jsp</result>
</action>

FirstAction.java

private List<String> userList;

/* Getter and Setter */

public String execute(){
    // Load your data from database
    userList = getMyService().findUserList();
    return SUCCESS;
}

FirstPage.jsp

<s:form action="secondAction" >
    <div class="select_wrap">
        <s:select list = "userList"
                  name = "selectedItem"
              cssClass = "select_field"
              multiple = "multiple" />
    </div>

    <s:submit value="Post your selection to second Action" />
</s:form>

SecondAction.java

private String selectedItem;

/* Getter and Setter */ 

public String execute(){
    log.debug("Selected item is: " + selectedItem);
    return SUCCESS;
}

SecondPage.jsp

<div>
    Selected item is: <s:property value="selectedItem" />
</div>

答案 1 :(得分:0)

使用Struts2标记<s:select>而不是元素上的迭代器。此外,您的sampleselect变量已初始化,但其中没有任何项目。

你的行动应该有:

private List<String> sampleselect = Arrays.asList("some", "data", "items");

然后您的JSP可以:

<s:select list="sampleselect" multiple="true"/>

参考:http://struts.apache.org/release/2.3.x/docs/select.html