wood burning stoves 2.0*
The moose likes Struts and the fly likes Can I access the value of a listKey in <s:select> from my Action Class? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Can I access the value of a listKey in <s:select> from my Action Class?" Watch "Can I access the value of a listKey in <s:select> from my Action Class?" New topic
Author

Can I access the value of a listKey in <s:select> from my Action Class?

Alia Huss
Ranch Hand

Joined: Feb 13, 2009
Posts: 63
Hi!
I was wondering if there is any way that I can access the value of listKey (in a <s:select>) from my Action Class?

My s:select looks as follows:

<s:select label="Namn"
name="person"
list="persons"
listKey="%{id}"
listValue="%{firstName+lastName}"
required="true"
/>

And I want to use the value of %{id} in my Action class, is that possible?
Or can I put the id together with the name in listValue without it showing in the dropdown list?

Thanks in advance
Alia
Bilal A. Siddiqui
Ranch Hand

Joined: Sep 14, 2008
Posts: 53

Hi Alia,

Just make getter and setter for

person and personKey in your action class.





Cheers!!! Bilal
Handsome Android||@bilal_speed||LinkedIn
Alia Huss
Ranch Hand

Joined: Feb 13, 2009
Posts: 63
Thank you very much!
But as it seems the value is never set.

I created both getters and setters for personKey in my Action class, as you said.
I didn't create any for Person since I have no use for the names in my action class.
Sadly it doesn't seem like the value which is selected when I press Save is actually set in my Action class.
Something does happen because the listValue returns an empty String instead of a null but the listKey doesn't call setPersonKey in my Action class at all..

I'll post the code below:
CreateReservationAction.java


I'm aware that the code isn't pretty, and I'm not planning on keeping it. I'm just trying to see if it'll work to create a Reservation. Once it's working I'll refine the code.

AddReservation.jsp


I've checked and rechecked that the "name" values match the getter and setter names, so that shouldn't be the issue.

Appreciate the help
Regards
Alia
Bilal A. Siddiqui
Ranch Hand

Joined: Sep 14, 2008
Posts: 53

Alia do one thing

for testing purpose change your method="POST" to method="GET" and see in url what all values are going on submit.
Alia Huss
Ranch Hand

Joined: Feb 13, 2009
Posts: 63
The URL after pressing Save looks as follows:
http://localhost:8080/RoomBooking/CreateReservationAction.action?startTime=&endTime=&room=&person=&company=&action%3ACreateReservationAction_save=save

Thanks
Alia
Bilal A. Siddiqui
Ranch Hand

Joined: Sep 14, 2008
Posts: 53

Oh, I am sorry.

I thought you are using autocompleter.

but you are using select tag. you dont need key term.

you will get key value just by using name.

in your case just make getter and setter of person .

no need to make getter and setter of personKey.

Let me know whether your problem is solved or not.
Alia Huss
Ranch Hand

Joined: Feb 13, 2009
Posts: 63
Still doesn't work.
All the Strings are set to "" instead of null.
And setPerson and setRoom aren't used at all since the values of room and person are still -1.
I was under the impression that setRoom and setPerson would return the value found in listValue, not listKey.
If so, the logical answer to why person and room are still set to -1 would be that the values of listValue are Strings and not ints. Which doesn't help me at all since I want the id, which is an int, and is found in listKey.

This is printed to my console:
02-Mar-2009 13:33:12 com.opensymphony.xwork2.interceptor.ParametersInterceptor setParameters
SEVERE: ParametersInterceptor - [setParameters]: Unexpected Exception catched: Error setting expression 'person' with value '[Ljava.lang.String;@1be72ee'
02-Mar-2009 13:33:12 com.opensymphony.xwork2.interceptor.ParametersInterceptor setParameters
SEVERE: ParametersInterceptor - [setParameters]: Unexpected Exception catched: Error setting expression 'room' with value '[Ljava.lang.String;@a7887e'

I guess that's proof that it's trying to use setRoom/setPerson with the String value of listValue.

Thanks
Alia
Bilal A. Siddiqui
Ranch Hand

Joined: Sep 14, 2008
Posts: 53

Alia what value of select tag person is going in your url ??

have you checked that?
Alia Huss
Ranch Hand

Joined: Feb 13, 2009
Posts: 63
If you mean the same URL I posted, the one that is shown when I switched POST to GET, then I don't know, since it doesn't show.

localhost:8080/RoomBooking/CreateReservationAction.action?startTime=&endTime=&room=&person=&company=&action%3ACreateReservationAction_save=save

Or do you mean another URL? If so, how do I find it? (I'm quite new to this)

Thanks
Alia
Bilal A. Siddiqui
Ranch Hand

Joined: Sep 14, 2008
Posts: 53

According to the url posted it seems that you have not selected and drop down while submiting the page.

as you can see clearly all the select tags are going empty at the time of submit. Select the values in drop downs then press submit.
Alia Huss
Ranch Hand

Joined: Feb 13, 2009
Posts: 63
I have selected values in the dropdowns,
Atleast according to what I can see.
All the boxes are showing values, such as times, room names and person names.

Strange isn't it.

Thanks
Alia
Bilal A. Siddiqui
Ranch Hand

Joined: Sep 14, 2008
Posts: 53

But your url is not carrying any value.

very strange.
Bilal A. Siddiqui
Ranch Hand

Joined: Sep 14, 2008
Posts: 53

Can you show me the code your using to populate these selects tags. ?
Alia Huss
Ranch Hand

Joined: Feb 13, 2009
Posts: 63
It's all posted further up , though personKey and roomKey have been changed back to person and room.
The rest hasn't changed.

Regards
Alia
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

hey alia,

The normal behavior --,

if you have the s:select tag and you have selected the values in the dropdown. Then the only thing which you can get is the listKey and nothing else. the "name" attribute is very important in this case, the "name" will always contain the "listKey"...

The "name" will contain a string which is your "id" which will not be a problem while storing, the way it comes is.... as a string if you have a getter setter of strings in action class of person... you can always parse it to an integer if you are sure its always an int...

it comes as -1 because either you have no proper getter setters and you have initialized the person integer as -1.

Please check the value of your %{id} if its rendering the value or not viewing the source or s:property will do... or giving the "listValue" (its only used for displaying) as the same as listKey as follows :

<s:select label="Namn"
name="person"
list="persons"
listKey="%{id}"
listValue="%{id}"
required="true"
/>

Regards.
Alia Huss
Ranch Hand

Joined: Feb 13, 2009
Posts: 63
Hello Robin John!
Thanks for your answer!
All you've said has made perfect sense, so I've changed person and room to Strings in my action.

And I can see the id values in the lists, 1 and 8 for the persons and 1 and 2 for the rooms.
So that's working as it should.
But I still get an empty String when calling getPerson() getRoom() getStartTime() getEndTime() and getCompany() in the Action Class.

But since all the Strings in my action class are initiated with null values, it's obvious that something's happening.
The problem is just that the values in the selects aren't sent along.

Thanks
Alia
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

i can see in your class which is posted up.... has

private int person = -1;

and that is the "person" which you should use... if you have to use an int you can go ahead and use it, there should be no problem....


okie if you get to see the id's in the dropdown then the front end is absolutely fine.....
but now i suppose ther's some problem in the configuration

if the values are not turning up in action... i want to know about the interceptor stacks you have included( if any)...in your struts xml in your action mappings....

Alia Huss
Ranch Hand

Joined: Feb 13, 2009
Posts: 63
Right, I'm not sure what an interceptor stack is..
But here's the entire Struts.xml

Struts.xml


Hope that helps.

Thanks
Alia
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

although we should not use the entire default stack if it is not neccessary, but just to test this whether its working or not... try these 2 mappings as your below mapping for the class (just add these mappings no modification required and see if the parameters come)

Method 1)



Method 2)



and if you got time go through...

http://struts.apache.org/2.x/docs/interceptors.html

regards.
Alia Huss
Ranch Hand

Joined: Feb 13, 2009
Posts: 63
Method 1 made no difference, I still got the empty Strings.
Method 2 couldn't even start, the following error message was shown in my console:

SEVERE: Exception starting filter struts2
Unable to find interceptor class referenced by ref-name servletConfig - interceptor-ref - file:/D:/Workspaces/.metadata/.plugins/org.eclipse.wst.server.core/tmp0/wtpwebapps/RoomBooking/WEB-INF/classes/struts.xml:50:56
at com.opensymphony.xwork2.config.providers.InterceptorBuilder.constructInterceptorReference(InterceptorBuilder.java:50)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.lookupInterceptorReference(XmlConfigurationProvider.java:864)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.buildInterceptorList(XmlConfigurationProvider.java:453)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addAction(XmlConfigurationProvider.java:302)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.addPackage(XmlConfigurationProvider.java:384)
at com.opensymphony.xwork2.config.providers.XmlConfigurationProvider.loadPackages(XmlConfigurationProvider.java:239)
at org.apache.struts2.config.StrutsXmlConfigurationProvider.loadPackages(StrutsXmlConfigurationProvider.java:111)
at com.opensymphony.xwork2.config.impl.DefaultConfiguration.reload(DefaultConfiguration.java:152)
at com.opensymphony.xwork2.config.ConfigurationManager.getConfiguration(ConfigurationManager.java:52)
at org.apache.struts2.dispatcher.Dispatcher.init_PreloadConfiguration(Dispatcher.java:398)
at org.apache.struts2.dispatcher.Dispatcher.init(Dispatcher.java:455)
at org.apache.struts2.dispatcher.FilterDispatcher.init(FilterDispatcher.java:201)
at org.apache.catalina.core.ApplicationFilterConfig.getFilter(ApplicationFilterConfig.java:275)
at org.apache.catalina.core.ApplicationFilterConfig.setFilterDef(ApplicationFilterConfig.java:397)
at org.apache.catalina.core.ApplicationFilterConfig.<init>(ApplicationFilterConfig.java:108)
at org.apache.catalina.core.StandardContext.filterStart(StandardContext.java:3709)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4363)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:719)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:516)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:578)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:288)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:413)

This is the filter I'm using in my web.xml


Thanks
Alia
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

not able to find servletConfig class !! great... this is definitely some configuration issue some jars missing from classpath...

have you mentioned this line... in your struts xml..?? if not include it in the first line of your xml... as in the following...

Alia Huss
Ranch Hand

Joined: Feb 13, 2009
Posts: 63
No I had not, but now I have. Still getting the same error message though

You have no idea how thankful I am for the time your spending trying to solve this issue!
Thanks!
Alia
Robin John
Ranch Hand

Joined: Sep 10, 2008
Posts: 270

welcome alia... no probs...

but i foresee... that i wouldn't be of much help now...


try method 1 and method 2 both after including the same line (posted up) ... and one more question (i should have asked it in the beginning )

Q) which version of struts 2 are you using ?? if possible upgrade it to the latest one... servletConfig interceptor should work.. and if that works then try method 2...

Q2) Are all the fields in the Page coming as null ?? or only the dropdowns??

regards.
Alia Huss
Ranch Hand

Joined: Feb 13, 2009
Posts: 63
I'm using the latest Struts version, which is 2.1.6 right?
I only have dropdowns in this page and all those are coming as empty Strings, not nulls.
But I have another page for adding a User which only has textfields and those work just fine.

I'll try out those methods again as soon as Eclipse starts again, it's refusing to work at the moment.
I just hope it starts working soon so I can try them out before I have to rush home.
Else I'll try it out tomorrow.
Either way, thank you so much for your help!
I really appreciate it.

Thanks
Alia

Alia Huss
Ranch Hand

Joined: Feb 13, 2009
Posts: 63
Right, a new day, and it's starting out to be great!
I did as you said and updated my struts2-core-2.0.x jar to the new struts2-core-2.1.x jar and made sure that all the jars it's dependent on were there too. And now it works just fine!
Thank you so much!

Kind regards
Alia
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can I access the value of a listKey in <s:select> from my Action Class?
 
Similar Threads
How to retain the latest selected value of drop down box.
How to get Struts select tag's current selected value/id
Data loading problem in select tag using ajax in struts2
s:select and data conversion
Using Struts 2 Select Tag for dynamic values