user1449197
user1449197

Reputation: 53

Struts2 multi select list: how to get values in action class

I am very new to Struts2, struck with multi select tag. I have searched in many places but failed to get the solution. Here is my problem:

In JSP i have used struts2 select tag like this

<s:form action="AjaxSaveSelectedStatus">
   <s:select label="Select Status" name="masterStatusLists" id="masterStatusLists" 
      list="#{'1':'status1','2':'status2','3':'status3','4':'status4'}" 
      multiple="true" required="true"/>
   <input type="submit" value="Save"/>
</s:form>

In struts.xml mapping

<action name="Ajax*" class="com.mypackage.actions.forms.Ajax{1}">
        <result name="redirect">${redirectUrl}</result>
    </action>

In Action class I have used like this

public class AjaxSaveSelectedStatus extends BaseAjaxActionWithSession {
ArrayList<MasterStatusList> masterStatusLists;

@Override
public String execute(){

    for (MasterStatusList masterStatusList : masterStatusLists) {
        System.out.println(masterStatusList.getStatusId());
    }
    return SUCCESS;
}
public ArrayList<MasterStatusList> getMasterStatusLists() {
    return masterStatusLists;
}

public void setMasterStatusLists(ArrayList<MasterStatusList> masterStatusLists) {
    this.masterStatusLists = masterStatusLists;
}
}

Here is MasterStatusList Bean class

public class MasterStatusList implements java.io.Serializable {
   private int statusId;
   private String statusName;
   public MasterStatusList() {}
   public int getStatusId() {return this.statusId;}
   public void setStatusId(int statusId) {this.statusId = statusId;}
   public String getStatusName() {return this.statusName;}
   public void setStatusName(String statusName) {this.statusName = statusName;}
}

Now I want to get the list of selected items in client side to the action.

Upvotes: 1

Views: 15103

Answers (1)

Anupam
Anupam

Reputation: 8016

The value submitted by a multi select tag will be a List(or array or string of csv) of string(in your case integer 1,2,3..). So declare the List of Integers in you action class

List<Integer> masterStatusLists; //and its getter/setter

This must solve the issue

Upvotes: 3

Related Questions