Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
519 views
in Technique[技术] by (71.8m points)

java - How can I map multiple values from select tag in Struts 2?

I have a multi-selection combo in my Struts2 web application. I use the ui-multisection component. When I select and submit values "1" and "2" then check it in my action class, only the last selected value was set. What should I do for getting set all selected values?

JSP File :

<%@ 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>

Action class :

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;
    }


}

The struts.xml file:

?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>
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try changing

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

to

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

since your Getters and Setters are like that.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...