File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Struts and the fly likes Struts 2 updating record problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Struts 2 updating record problem" Watch "Struts 2 updating record problem" New topic

Struts 2 updating record problem

RamandeepS Singh
Ranch Hand

Joined: Aug 25, 2009
Posts: 63

I have the person class object in my action class

i made the getter and setter for person object in my action class

and in jsp page i update some fields

then when i submit the form new object is created instead of some of fields change....
if i set all other fields in hidden tag den its ok... is there any solution that we do not have to save other properties of person object in hidden tags

Thanks in Advance
Ramandeep S.
Joe Ess

Joined: Oct 29, 2001
Posts: 9189

Let me get this straight. You present data to the user, the user changes the data, and you are surprised that when the data is submitted to the server, it is changed? Sounds to me like it is working as designed.
If you don't want the data changed, perhaps you should save a copy off somewhere.

[How To Ask Questions On JavaRanch]
RamandeepS Singh
Ranch Hand

Joined: Aug 25, 2009
Posts: 63

First of all thanks for replying

No thats not the issue i can explain with you an example

say user has properties
1.first name
2. last Name
3. Email
4. Password

i displayed first name, last Name, password in page

and when i submit the form
email is set to null,
but if i set email as hidden then it works fine because new object is created and email is given new value

i want to know is there any way to retain the previous object and just change the changed values

i do not want to save the copy of object

Joe Ess

Joined: Oct 29, 2001
Posts: 9189

I see what you are saying. With Struts, you write an object's state to a JSP page, throw it over to the browser, then Struts reconstitutes the object and hands it to your action. If you don't put all the fields on the JSP, Struts can't retrieve that information. You have a couple of options:
1. use hidden fields, as you know.
2. put the object in the session and copy the updated values into that object.
3. only consider fields that you have on the JSP page, ignoring fields you know will be null
4. make your class structure reflect that of your JSP page. This is probably the "best" option, since we want our classes to model the real world, and they are less error prone than the previous suggestions. I usually make a class hierarchy with a simple (user name) object for list purposes, a more complex subclass for user purposes (address, phone), then a even more complex subclass for administration purposes (department assignment, mail code). This way, each JSP and action only deal with the fields then are presenting and editing.
I agree. Here's the link:
subject: Struts 2 updating record problem
It's not a secret anymore!