aspose file tools*
The moose likes Struts and the fly likes How to retain value in struts 2 using <s:select></s:select> tag Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "How to retain value in struts 2 using <s:select></s:select> tag" Watch "How to retain value in struts 2 using <s:select></s:select> tag" New topic
Author

How to retain value in struts 2 using <s:select></s:select> tag

Sai Prasad
Ranch Hand

Joined: Feb 08, 2009
Posts: 30
Hi,
I am trying to retain selected values in .jsp using select tag but I could not get proper solution.Can anyone help with the better solution for this.

Thanks in advance...!


Take No Challenge Lightly....

Strive for Excellence..!!!
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 684


hi,

Declare your select tag like ..


<s:select theme="simple" cssClass="textfield"
headerValue="Select Region" headerKey="0"
name="region.categoryId"
value="%{categoryId}" list="regionList"
listKey="categoryId" listValue="categoryName" />

That's it.

just make sure your listKey and value filed match property name you have give to it.

Like here it is listKey="categoryId" and for select give it to value categoryId .

that will make your select box value selected.


Thanks, Nishan Patel
SCJP 1.5, SCWCD 1.5, OCPJWSD Java Developer,My Blog
Sai Prasad
Ranch Hand

Joined: Feb 08, 2009
Posts: 30
Nishan Patel wrote:
hi,

Declare your select tag like ..


<s:select theme="simple" cssClass="textfield"
headerValue="Select Region" headerKey="0"
name="region.categoryId"
value="%{categoryId}" list="regionList"
listKey="categoryId" listValue="categoryName" />

That's it.

just make sure your listKey and value filed match property name you have give to it.

Like here it is listKey="categoryId" and for select give it to value categoryId .

that will make your select box value selected.




Hi, Can you explain me more clearly.
I am still suffering with same problem

Sai Prasad
Ranch Hand

Joined: Feb 08, 2009
Posts: 30
Sai Prasad In Search of technologies wrote:
Nishan Patel wrote:
hi,

Declare your select tag like ..


<s:select theme="simple" cssClass="textfield"
headerValue="Select Region" headerKey="0"
name="region.categoryId"
value="%{categoryId}" list="regionList"
listKey="categoryId" listValue="categoryName" />

That's it.

just make sure your listKey and value filed match property name you have give to it.

Like here it is listKey="categoryId" and for select give it to value categoryId .

that will make your select box value selected.




Hi, Can you explain me more clearly.
I am still suffering with same problem




generally i am sending the addition of two values as key

Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 684

Hi,

I want to know this select tag is working fine at your end.



because I have seen this type of field {companyCode + ',' + branchCode} first time.

Sai Prasad
Ranch Hand

Joined: Feb 08, 2009
Posts: 30
Yes it is working fine, (+) is an OGNL expression used for addition of two values.

apart from that is anything wrong in my code
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 684



Hi,

if you want to add like {companyCode + ',' + branchCode}"

then why you use ',' in between companyCode and branchCode.

Simple addition can looks like "{companyCode + branchCode}"



may be like that.

Sai Prasad
Ranch Hand

Joined: Feb 08, 2009
Posts: 30
Actually, key sent from jsp is separated using " , " in action class and stored in string[ ] and then using these keys value data is retrieved from database.
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 684


Hi,

Actually you listKey="" attribute and value="" attribute must match.

If you write in listKey="" with " , " comma separated then your value="" attribute not match with your listKey="".


so make it same.

Sai Prasad
Ranch Hand

Joined: Feb 08, 2009
Posts: 30
i did not understand it can you explain it more clearly please
Nishan Patel
Ranch Hand

Joined: Sep 07, 2008
Posts: 684


hi,

Key your code as it is just change ...



Just change in value="%{companyCode + ',' + branchCode}"

add % at front value tag.

see above.

Sai Prasad
Ranch Hand

Joined: Feb 08, 2009
Posts: 30
Sorry boss.......................! sitll as usual
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

@Nishan: There's no reason the listKey and listValue have to be the same format.

@Sai: How are you declaring the action's companyList property?
Sai Prasad
Ranch Hand

Joined: Feb 08, 2009
Posts: 30
I am declaring it as list< Company Object>
Sai Prasad
Ranch Hand

Joined: Feb 08, 2009
Posts: 30
Company object has four properties

companyCode

branchCode

company

branch

Sai Prasad
Ranch Hand

Joined: Feb 08, 2009
Posts: 30
Hi David,

Can you Help me with the solution.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

#session.COMPANY_LIST is meaningless--you either need to use something like:

#session[@complete.pkg.reference@COMPANY_LIST]

or

#session['companyList']

or something else that makes sense. Other than that it should be fine.
Sai Prasad
Ranch Hand

Joined: Feb 08, 2009
Posts: 30
By using the same #session value i am getting the values from session and is displayed in drop down list also, even the codes are displayed when tried to print.

Only problem is it is not retaining. :thumbup:
Sai Prasad
Ranch Hand

Joined: Feb 08, 2009
Posts: 30
COMPANY_LIST is the key for the list<Company Object> stored in session
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What do you mean, "not retaining"?
Sai Prasad
Ranch Hand

Joined: Feb 08, 2009
Posts: 30
After refreshing the value in the select option should be the same as selected....... this is what i am trying for
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

If you have a setter for companyList in the action, and it contains the values you want "pre-selected", then it should work fine.
Bilal A. Siddiqui
Ranch Hand

Joined: Sep 14, 2008
Posts: 53

Hi Sai

is your drop down coming from a list or its a static drop down ?


Cheers!!! Bilal
Handsome Android||@bilal_speed||LinkedIn
Sai Prasad
Ranch Hand

Joined: Feb 08, 2009
Posts: 30
No, it is the list that is generated in action class from database
Bilal A. Siddiqui
Ranch Hand

Joined: Sep 14, 2008
Posts: 53

i faced the same problem sometime back.

i was using Map and it looked something like

countryMap.put(capital+"."+code,name);

and then separated capital and code using split method.


Sai Prasad
Ranch Hand

Joined: Feb 08, 2009
Posts: 30
Ya me to doing the same logic for separating company code and branch code. but the logic is in action class. everything is working fine.

problem is value is not retained.


Bilal A. Siddiqui
Ranch Hand

Joined: Sep 14, 2008
Posts: 53

Sai can you be more clear on what you exactly mean by value is not retained.
Sai Prasad
Ranch Hand

Joined: Feb 08, 2009
Posts: 30
when i select a value in drop down and click submit button i get related data in the table, but the selected option is not displayed in drop down list. it shows the default value " -- Select -- "
Bilal A. Siddiqui
Ranch Hand

Joined: Sep 14, 2008
Posts: 53

can you use autocompleter instead of select tag.

For example if your autocompleter name is fruit

then in your action class declare 2 fields

String fruit;
String fruitKey; and make getter and setter for them. Your problem will be solved.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

@bilal: Not using an autocompleter isn't the issue.
Bilal A. Siddiqui
Ranch Hand

Joined: Sep 14, 2008
Posts: 53

@David: I said this because i faced the same problem and i rectified it by using autocompleter.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

That may be, but a whole bunch of us use <s:select...> with no issues. Switching to an autocompleter brings its own set of issues, and the Dojo tags are deprecated and won't always be around.
Sai Prasad
Ranch Hand

Joined: Feb 08, 2009
Posts: 30
Please anyone can help me out with the sample code please
Bilal A. Siddiqui
Ranch Hand

Joined: Sep 14, 2008
Posts: 53

Lets wait for some one to place a code for select tag.

if incase anytime you want to shift on autocompleter i can give you the code.
Sai Prasad
Ranch Hand

Joined: Feb 08, 2009
Posts: 30
ya will try with that also can you
please help me
Bilal A. Siddiqui
Ranch Hand

Joined: Sep 14, 2008
Posts: 53

This part comes in jsp page (EditStudentInfo.jsp)



xml



Action class (insertEditStudentInfo.java)


Method to populate country, this part of code is also from action class, i am adding 2 countries hard coded, you can add them dynamically. and call this method in execute method.


If still you face any problem feel free to send a message.

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to retain value in struts 2 using <s:select></s:select> tag