如何在Struts 2中映射select标签中的多个值?

时间:2014-01-10 15:29:07

标签: java jquery jquery-ui jsp struts2

我的Struts2 Web应用程序中有一个多选组合。我使用ui-multisection组件。当我选择并提交值"1""2"然后在我的操作类中检查它时,只设置了最后选择的值。如何设置所有选定的值?

JSP文件:

<%@ taglib prefix="s" uri="/struts-tags"%>
<link rel="stylesheet"
    href="<s:url value="/css/jquery.multiselect.css"/>" type="text/css" />
<link rel="stylesheet"
    href="<s:url value="/css/jquery.multiselect.filter.css"/>"
    type="text/css" />
<link rel="stylesheet"
    href="<s:url value="/css/themes/base/jquery.ui.theme.css"/>"
    type="text/css" />
<script type="text/javascript"
    src="<s:url value="/js/jquery-1.9.1.min.js" />"></script>
<script type="text/javascript"
    src="<s:url value="/js/jquery-ui.min.js" />"></script>
<script type="text/javascript"
    src="<s:url value="/js/jquery.multiselect.min.js" />"></script>
<script type="text/javascript"
    src="<s:url value="/js/jquery.multiselect.filter.js" />"></script>


<script language="javascript">
    $(function() {

        $("select").multiselect({
            multiple : true,
            selectedList : 4,
            noneSelectedText : 'Tous',
            height : 180
        });

        $("select").multiselect().multiselectfilter();
    });
</script>
<s:form id="idForm" action="testOMB.do" name="myForm">

    <div class="contentTable">
        <div class="tbl">

            <table width="100%">
                <tr>
                    <td><s:select id="idCombo1" multiple="multiple"
                            list="listItems" name="listIdSelected"
                            value="listIdSelected" listKey="id" listValue="label"
                            emptyOption="true" /></td>
                </tr>
            </table>
        </div>
    </div>

    <div class="bloc_action">
        <s:submit cssClass="btn_action" value="TEST OMB" />
    </div>
</s:form>

行动类:

package com.omb.controller.report;

import java.util.ArrayList;
import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.transaction.annotation.Isolation;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;

import com.omb.ui.item.Item;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.Preparable;

@SuppressWarnings("serial")
public class MyAction extends ActionSupport implements Preparable {

    Log log = LogFactory.getLog(MyAction.class);

    private List<Item> listItems = new ArrayList<String>();
    private List<String> listIdSelected = new ArrayList<String>();

    public void prepare() throws Exception {
        listItems.add(new Item("1", "Item 1"));
        listItems.add(new Item("2", "Item 2"));
        listItems.add(new Item("3", "Item 3"));
        listItems.add(new Item("4", "Item 4"));
    }

    @Transactional(propagation = Propagation.REQUIRED, isolation = Isolation.DEFAULT, readOnly = true)
    public String testOMB() throws Exception {
        log.debug("listIdSelected = " + listIdSelected.size());
        return SUCCES;
    }

    public List<Item> getListItems() {
        return this.listItems;
    }

    public void setListItems(List<Item> listItems) {
        this.listItems = listItems;
    }

    public List<String> getListIdSelected() {
        return this.listIdSelected;
    }

    public void setListIdSelected(List<String> listIdSelected) {
        this.listIdSelected = listIdSelected;
    }


}

struts.xml文件:

?xml version="1.0" encoding="UTF-8" ?>

<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <constant name="struts.devMode" value="false" />
    <constant name="struts.action.extension" value="do" />
    <constant name="struts.custom.i18n.resources"
        value="com.omb.i18n.StrutsResourceBundle" />
    <constant name="struts.objectFactory" value="spring" />
    <constant name="struts.objectFactory.spring.autoWire" value="name" />
    <constant name="struts.i18n.encoding" value="ISO-8859-1" />
    <constant name="struts.i18n.reload" value="false" />
    <constant name="struts.configuration.xml.reload" value="false" />
    <constant name="struts.locale" value="fr" />
    <constant name="struts.multipart.maxSize" value="100000000000" />
    <constant name="struts.enable.SlashesInActionNames" value="true" />
    <constant name="struts.mapper.alwaysSelectFullNamespace" value="false"/>

    <constant name="struts.codebehind.classSuffix" value="Controller"/>
    <constant name="struts.codebehind.action.checkImplementsAction" value="false"/>
    <constant name="struts.codebehind.action.checkAnnotation" value="false"/>
    <constant name="struts.codebehind.action.defaultMethodName" value="index"/>
    <constant name="struts.configuration.classpath.defaultParentPackage" value="rest-default" />

    <package name="default" extends="tiles-default" namespace="/">

        <interceptors>

            <interceptor name="userAware"
                class="com.omb.interceptor.UserAwareInterceptor" />

            <interceptor name="params-filter"
                class="com.opensymphony.xwork2.interceptor.ParameterFilterInterceptor" />

            <interceptor-stack name="defaultStack">
                <interceptor-ref name="exception" />
                <interceptor-ref name="alias" />
                <interceptor-ref name="servletConfig" />
                <interceptor-ref name="i18n" />
                <interceptor-ref name="chain" />
                <interceptor-ref name="modelDriven" />
                <interceptor-ref name="fileUpload">
                    <param name="maximumSize">11204928</param>
                </interceptor-ref>
                <interceptor-ref name="staticParams" />
                <interceptor-ref name="conversionError" />
                <interceptor-ref name="params" />
                <interceptor-ref name="prepare" />
                <interceptor-ref name="validation" />
                <interceptor-ref name="workflow" />
                <interceptor-ref name="userAware" />

            </interceptor-stack>

        </interceptors>

        <default-interceptor-ref name="defaultStack" />

        <global-results>
            <result name="technicalError" type="chain">
                errorAction
            </result>
            <result name="sessionInvalidError" type="tiles">
                sessionInvalid
            </result>
            <result name="blank" type="tiles">blank</result>
        </global-results>

        <global-exception-mappings>
            <exception-mapping exception="java.lang.Exception"
                result="technicalError" />
            <exception-mapping
                exception="com.omb.service.exception.UserSessionInvalidException"
                result="sessionInvalidError" />

        </global-exception-mappings>

        <action name="errorAction"
            class="com.omb.controller.ErrorAction">
            <result name="success" type="tiles">errorTile</result>
        </action>

        <action name="*Cache"
            class="com.omb.controller.CacheAction" method="{1}">
            <result name="success" type="tiles">resultCache</result>
            <result name="input" type="tiles">inputCache</result>
        </action>

        <action name="*Login"
            class="com.omb.controller.LoginAction" method="{1}">
            <result name="success" type="tiles">login</result>
            <result name="password" type="tiles">changePassword</result>
        </action>

        <action name="*Welcome"
            class="com.omb.controller.WelcomeAction" method="{1}">
            <result name="success" type="tiles">welcome</result>
            <result name="report" type="redirectAction">
                <param name="namespace">/report</param>
                <param name="actionName">perimetersReport</param>
            </result>
        </action>

    </package>

    <package name="report" extends="default" namespace="/report">
        <action name="*Action"
            class="com.omb.controller.report.MyAction"
            method="{1}">
            <result name="succes" type="tiles">succesTile</result>
        </action>
    </package>
</struts>

2 个答案:

答案 0 :(得分:1)

尝试更改

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

private List<Integer> listIdSelected = new ArrayList<Integer>();

因为你的Getters and Setters就是这样。

答案 1 :(得分:1)

  

我应该怎样做才能获得所有价值?

您应该制作select标记以接受多个选项。这是通过struts multiple="true"标记中的select完成的。然后你的拦截器堆栈不完整,因为缺少multiselect interceptor。在<interceptor-ref name="multiselect"/>拦截器引用之后,您应该将fileupload添加到拦截器堆栈。