• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Ranch Hand
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Author
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 12617
IntelliJ IDE Ruby
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..
reply
    Bookmark Topic Watch Topic
  • New Topic