Struts 2 multiple select and pre selecting


Struts2Logo

In this Tutorial, I will show you how to use  s:select multiple select option and also how to use multiple pre-selecting.

Using Java list:
Suppose you have a jsp page as input screen and having s:select with multiple-select option. Here is the code:


<s:select 
name="exType"
multiple="true"
list="exTypeList"
listKey="exTypeId"
listValue="exTypeName"/>


Now in output screen (jsp page) we will get selected items using value attribute of s:select tag. Here is the code:


<s:select name="exType" 
multiple="true"
value="exTypeValues"
list="exTypeList"
listKey="exTypeId"
listValue="exTypeName"/>


Action class code for setting value attribute's values (pre-selecting values).


public class ShowAllExercisesData extends ActionSupport {

private List<Integer> exTypeValues;

public List<Integer> getExTypeValues() {
return Arrays.asList(2, 4);
}

public void setExTypeValues(List<Integer> exTypeValues) {
this.exTypeValues = exTypeValues;
}
}


So in output jsp 2 and 4 keys values will be  pre-selected.

Description of attribute s:select multiple :
Creates a multiple select. The tag will pre-select multiple values if the values are passed as an Array or a Collection(of appropriate types) via the value attribute. If one of the keys equals one of the values in the
Collection or Array that will be selected.

Using OGNL list:


<s:select 
name="exType"
multiple="true"
size="8"
list="#{1:'Cardio', 2:'Strength', 3:'Stretches', 4:'CrossFit', 5:'Others'}"
value="%{{2, 4}}"
/>


 OR

<s:select 
name="exType"
multiple="true"
size="8"
list="#{1:'Cardio', 2:'Strength', 3:'Stretches', 4:'CrossFit', 5:'Others'}"
value="exTypeValues"
/>


Set pre-selected values in Action class like this:

public List<Integer> getExTypeValues() {
return Arrays.asList(2, 4);
}

Share on Google Plus

About JK STACK

This is a short description in the author block about the author. You edit it by entering text in the "Biographical Info" field in the user admin panel.
    Blogger Comment
    Facebook Comment

0 comments:

Post a Comment