This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Struts and the fly likes struts2 modeldriven unable to transfer form data to model object 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 "struts2 modeldriven unable to transfer form data to model object" Watch "struts2 modeldriven unable to transfer form data to model object" New topic
Author

struts2 modeldriven unable to transfer form data to model object

olivier glorieux
Greenhorn

Joined: Jul 29, 2010
Posts: 4
Hello, here is an extract of my domain model :



my class SurveyAction implements ModelDriven, I use an edit() method to set my Survey object as the model, and i manage to populate my jsp form ok. But when i submit the form my saveOrUpdate() method receives another Survey object as the one i set in edit() !? a new Survey Object that has the 'freq' or 'information' modifications but has a null observations property ???

here is the form




any ideas?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Welcome to JavaRanch!

Why would it be the same object? Actions are instantiated per-request. You may want to look at the "scopedModelDriven" interceptor.
olivier glorieux
Greenhorn

Joined: Jul 29, 2010
Posts: 4
Thank you and for your answer,

It does not bother me if it is the same object, but it seems to me that it would be simpler : I retrieve a Survefy object from the database, i set it as the model object of the surveyAction class and display it through a form. the user modifies the data directly in the original Survey object and i save it in the database...

anyway the main problem is that in the object i 'receive' my observation list is null.
The original Survey object had a fully populated observations list, that is not set in the new modified Survey object.
As if my jsp code could not allow struts2 to set the observations collection it has displayed.

The Survey object does have getters and setters for the observations list.

and for now i check right away the 'scopedModelDriven' interceptor !

Olivier
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

But since it's a new object, and you don't have hidden form fields for the fields you don't enter directly, I guess that's exactly what I'd expect--the new object would only be initialized as much as it is initialized. That's why you'd either need to keep it in session, so its original values are retained and only the new ones are modified, or you'd need to retrieve it again before the "params" interceptor and save that modified object.
olivier glorieux
Greenhorn

Joined: Jul 29, 2010
Posts: 4
I have been looking for documentation example on how to use scopedmodeldriven and I almost find nothing... Do I have to change the interceptor stack? I have no experience with interceptors, i use <package name="fr.mnhn.obj.struts2.actions" namespace="/" extends="struts-default"> in my struts.xml

I implemented the this interceptor in my SurveyAction and added a new String scopedKey attribute with its getter setter.

how do I set scopedKey, in struts.xml? in my action mapping in struts.xml? Anything else?


also it happens that i already have my survey object to be modified in session, so i tried the following code :



and it does very well display the info in my CURREN_SURVEY object but will not modify it, still what you expect?
Raymond Holguin
Ranch Hand

Joined: Aug 11, 2009
Posts: 77
had the same problem, I just implemented the Prepareable interface so that my list was populated there instead of relying on the submitted form to send it back to me.
olivier glorieux
Greenhorn

Joined: Jul 29, 2010
Posts: 4
Hello,

i implemented scopedModelDriven for two of my methods of SurveyAction and it works well to display my Survey object stored in session, I can even edit it but only partially :

I can change the freq value of my Survey object but not the abundance value of an observation in the list of observations of my Survey object.
I can display that value but when I enter a new value and submit it has not changed in my object in session though the freq value has changed...

here is my editSurvey.jsp that fails to modify my observations



I tried to add test properties with thier getters and setters : one in my Survey class (test) and one in my Observation class (testObs) and value them with this jsp test gets valued but not testObs

how can I do?

could this issue really be solved thanks to the prepare interceptor as Raymond suggests?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: struts2 modeldriven unable to transfer form data to model object
 
Similar Threads
s:iterator issue
How to pre-populate an object to update not using DrivenModel aproach
Struts 2 checkboxlist error - list key could not be resolved
Struts2 Model Driven question for a complex model
Struts2 Checkbox: How to set value back to Bean