Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

struts2 modeldriven unable to transfer form data to model object

 
olivier glorieux
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic