aspose file tools*
The moose likes Struts and the fly likes Struts2 <s:select multiple=true> -> Eventbean.setMap(Map map_from_multile_struts2) Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts2 <s:select multiple=true> -> Eventbean.setMap(Map map_from_multile_struts2)" Watch "Struts2 <s:select multiple=true> -> Eventbean.setMap(Map map_from_multile_struts2)" New topic
Author

Struts2 <s:select multiple=true> -> Eventbean.setMap(Map map_from_multile_struts2)

J Owens
Greenhorn

Joined: Jun 24, 2009
Posts: 10
I am having problems with the OGNL syntax and possibly action setup of my web-application.

Scenerio, I want to create a struts2 <struts2:select name="event.dj_map" list="event.dj_map" size="5" multiple="true"> that will submit a map<string,string> to my custom javabean that my action places on the stack (Event.java). After I get this working I'll change to map<long, string> (my id's are stored as longs), I don't think that change will incur any problems.

Also, I don't think this matters but, my .jsp has javascript code that can add elements (options with value & bodytext elements) if the user should want to.

JSP: manage-event.jsp called from /secure/ManageEvents_create.action?subdomain=yadayadayada (accessible only by logged in users via authenticationInterceptor)

XML: struts.xml snippet

ACTION: ManageEvents.java

JAVA: Event.java (relevant parts)

ERROR: ognlNoSuchMethodException
WARNING: Error setting value
ognl.MethodFailedException: Method "setDj_map" failed for object com.mycityonline.events.Event@e2fa0e [java.lang.NoSuchMethodException: setDj_map([Ljava.lang.String;)]
at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:823)
at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:964)
at ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccessor.java:75)
at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:131)
at com.opensymphony.xwork2.ognl.accessor.ObjectAccessor.setProperty(ObjectAccessor.java:28)
at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:1656)
at ognl.ASTProperty.setValueBody(ASTProperty.java:101)
at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:177)
at ognl.SimpleNode.setValue(SimpleNode.java:246)
at ognl.ASTChain.setValueBody(ASTChain.java:172)
at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:177)
at ognl.SimpleNode.setValue(SimpleNode.java:246)
at ognl.Ognl.setValue(Ognl.java:476)
at com.opensymphony.xwork2.ognl.OgnlUtil.setValue(OgnlUtil.java:192)
at com.opensymphony.xwork2.ognl.OgnlValueStack.setValue(OgnlValueStack.java:155)
at com.opensymphony.xwork2.ognl.OgnlValueStack.setValue(OgnlValueStack.java:143)
at com.opensymphony.xwork2.interceptor.ParametersInterceptor.setParameters(ParametersInterceptor.java:273)
.....
at java.lang.Thread.run(Thread.java:619)
/-- Encapsulated exception ------------\
java.lang.NoSuchMethodException: setDj_map([Ljava.lang.String;)
at ognl.OgnlRuntime.callAppropriateMethod(OgnlRuntime.java:810)
at ognl.OgnlRuntime.setMethodValue(OgnlRuntime.java:964)
at ognl.ObjectPropertyAccessor.setPossibleProperty(ObjectPropertyAccessor.java:75)
at ognl.ObjectPropertyAccessor.setProperty(ObjectPropertyAccessor.java:131)
at com.opensymphony.xwork2.ognl.accessor.ObjectAccessor.setProperty(ObjectAccessor.java:28)
at ognl.OgnlRuntime.setProperty(OgnlRuntime.java:1656)
at ognl.ASTProperty.setValueBody(ASTProperty.java:101)
at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:177)
at ognl.SimpleNode.setValue(SimpleNode.java:246)
at ognl.ASTChain.setValueBody(ASTChain.java:172)
at ognl.SimpleNode.evaluateSetValueBody(SimpleNode.java:177)
at ognl.SimpleNode.setValue(SimpleNode.java:246)
at ognl.Ognl.setValue(Ognl.java:476)
at com.opensymphony.xwork2.ognl.OgnlUtil.setValue(OgnlUtil.java:192)
...
at java.lang.Thread.run(Thread.java:619)

Can anyone shed any light or links on how to get a mapped multiple select into your pojo with OGNL and STRUTS2? The examples I've seen are ALL of multiple struts2:textfields going into a map, I have YET to see any code of a multiple select being mapped to a map in a pojo. Seems like it should be extremely common. I even tried to "trick" java by implementing a dummy setDj_map(String str_map) just to see if I could kill the error to no avail :-/

research thus far:
http://www.zorched.net/2009/07/02/struts2-map-form-to-collection-of-objects/
http://struts.apache.org/2.0.14/docs/select.html
http://www.roseindia.net/struts/struts2/struts2uitags/select-tag.shtml

Thanks for any assistance/guidance/direction you can provide :-)!
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

How is the select tag rendering? I don't recall having had any problems using map-backed selects, but I can look in to it further if you're still stuck.

A few mildly unrelated comments:

- Consider coding to the Map interface rather than having the property hard-coded to an implementation.

- If you're using 1.5+ then it's better to genericize the map.

- The create() method seems unnecessarily obfuscated: deep nesting makes code difficult to read, because you're not always sure where you need to stop reading. Consider create() written like this:- I'm also not a big fan of not following Java naming conventions (like Event_Registry or dj_map etc.)

- Your validate method won't work--empty fields are sent back by the browser as empty strings--not nulls. You should check for empty or null; possibly using Apache Commons Lang's StringUtils.isBlank().
J Owens
Greenhorn

Joined: Jun 24, 2009
Posts: 10
Still not working yet ,

The form renders as:



the above value/content text is with the test map that I whipped up in the actions prepare() method as an attempt to get any kind of action / form object communication working.
- I'm using the latest Java, I think its JDK 6 u14
- as for the validate, I threw that in at the last minute, but its good to know it was not going to work if I needed it lol, I usually test for string length < 1 in addition to null (guess I can drop the null test)

- I DEFINITELY like your create method more, I'll have to bone up on that syntax and add it to my "toolbox"
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

If you see the error, it is looking for a setDj_map which takes a String[] not a String. Try that once...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
J Owens
Greenhorn

Joined: Jun 24, 2009
Posts: 10
Ankit Garg wrote:If you see the error, it is looking for a setDj_map which takes a String[] not a String. Try that once...


I tried also, same error produced
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Struts2 <s:select multiple=true> -> Eventbean.setMap(Map map_from_multile_struts2)
 
Similar Threads
problems with custom converters and move from Struts 2.0.11 to 2.1.6
from struts 2.0. to 2.1 - tokenSession
How does strut actions cope with hidden fields such as token
Passing value from one action redirect
ognl.OgnlException: target is null for setProperty(null, "preventCache", [Ljava.lang.String;@6dc5f0b