Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
Sai Prasad
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...!
 
Nishan Patel
Ranch Hand
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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.

 
Sai Prasad
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i did not understand it can you explain it more clearly please
 
Nishan Patel
Ranch Hand
Posts: 689
Eclipse IDE Java Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry boss.......................! sitll as usual
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am declaring it as list< Company Object>
 
Sai Prasad
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Company object has four properties

companyCode

branchCode

company

branch

 
Sai Prasad
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi David,

Can you Help me with the solution.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
#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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
COMPANY_LIST is the key for the list<Company Object> stored in session
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What do you mean, "not retaining"?
 
Sai Prasad
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Sai

is your drop down coming from a list or its a static drop down ?
 
Sai Prasad
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it is the list that is generated in action class from database
 
Bilal A. Siddiqui
Ranch Hand
Posts: 53
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sai can you be more clear on what you exactly mean by value is not retained.
 
Sai Prasad
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 53
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@bilal: Not using an autocompleter isn't the issue.
 
Bilal A. Siddiqui
Ranch Hand
Posts: 53
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@David: I said this because i faced the same problem and i rectified it by using autocompleter.
 
David Newton
Author
Rancher
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please anyone can help me out with the sample code please
 
Bilal A. Siddiqui
Ranch Hand
Posts: 53
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya will try with that also can you
please help me
 
Bilal A. Siddiqui
Ranch Hand
Posts: 53
Android Chrome Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.

 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic