aspose file tools*
The moose likes Struts and the fly likes Retrieving request parameters (from the url) in a jsp page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Retrieving request parameters (from the url) in a jsp page" Watch "Retrieving request parameters (from the url) in a jsp page" New topic
Author

Retrieving request parameters (from the url) in a jsp page

Dmitri Christo
Ranch Hand

Joined: Jan 19, 2007
Posts: 81
Hello,

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.

Thanks in advance for any help.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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.
Dmitri Christo
Ranch Hand

Joined: Jan 19, 2007
Posts: 81
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 :
,

and:


I certainly hope this additional information might help shed some more light this issue.

Thanks again for your response.
Regards.
rahul khanna
Ranch Hand

Joined: Sep 14, 2005
Posts: 48
Please modify to NOT pass primary keys in urls.

As for reading the request/session parameters on jsp you can use JSTL.
Dmitri Christo
Ranch Hand

Joined: Jan 19, 2007
Posts: 81
Yes, I'll do that thanks. But is there a way to use struts2 tags, within the existing <s:textfield> tag? Is there an example for that perhaps?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

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.)
Dmitri Christo
Ranch Hand

Joined: Jan 19, 2007
Posts: 81
Thanks David,

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..
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Retrieving request parameters (from the url) in a jsp page