I am building a Struts2 application using Hibernate and after the user navigates through the pages, they follow a link:
Basically this brings up a form where the user will modify an existing record on a database. The record's primary key, (id) is provided as a parameter in the URL the user clicked on.
My problem is retrieving and storing this request parameter in the jsp page which contains the form. So far my form looks like this:
For a reason that I am not able to understand, the Primary Key field on the form, instead of being pre-populated, it is blank, waiting for the user to enter the value.
I know it must have something to do with how I use the struts tags, or the ognl expression, but so far, I was not able to solve this. The consequences is that instead of doing an update, the action ends up doing a new insert. The action code checks if the id is null and if it is then a new insert occurs, if the id is not null, then an update should take place:
So, how do I use a struts2 tag in a jsp to retrieve request parameters from the url? i believe that this is the root cause of my problem.
I don't know what the action that retrieves the user to edit looks like, so it's hard to help. If the person is retrieved properly then the id should be populated as well (if that's what the field is called).
I'd suggest that allowing people to edit the primary key is a Really Bad Idea, though.
Joined: Jan 19, 2007
Yes, that field should be hidden actually, but, is there a way to retrieve that parameter from the URL at the jsp level? Using a struts 2 tag?
I am not sure if the following can help, but here are the related classes and struts.xml:
..And the action class:
..The Service implementation:
..And the two pages the user will navigate: list.jsp and edit.jsp :
I certainly hope this additional information might help shed some more light this issue.
You can use JSTL as mentioned, or access it via OGNL using #attr.id. There's no reason to access it from the request, though--it's in the person object (or should be if you're doing it right).
Consider a slight save() refactoring:(Although I never recommend catching RuntimeException; if anything I'd catch HibernateException and re-throw as an application-specific exception.)
Joined: Jan 19, 2007
I changed that textfield to:
Is my ognl syntax correct for the value parameter? The plan is to pre-populate the field with the id parameter taken from the URL, but keep it invisible to the user.
As you can see in my code in the above posts the Action class calls a save method which is delegated to the ServiceImpl class. Now during debugging, when the save method of the ServiceImpl class returns, the id is null (which causes a new insert) -- but when I query the person object at the Action class level, the id equals that new integer id defined automatically by the database.
I am not sure why the id is not retrieved from the URL successfully. It has to be grabbed there, or else a new record will be generated instead..