File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Struts and the fly likes Passing row id from html table on jsp to Action Class in Struts Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Passing row id from html table on jsp to Action Class in Struts" Watch "Passing row id from html table on jsp to Action Class in Struts" New topic
Author

Passing row id from html table on jsp to Action Class in Struts

Robert Wiscup
Ranch Hand

Joined: Oct 18, 2009
Posts: 45
Hi All,

I have a Struts application that has a .jsp page that has a table on it that displays rows from a database table. In each row displayed there is a button entitled "Delete". When the "Delete" button is clicked I want to pass the DefectID of the particular row to my Action Class. I don't know how to do this. Can someone tell me how to accomplish this?
The code is shown below.

Thanks so much,

Robert


David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Please UseCodeTags when posting code or configuration. Unformatted code and configuration is very difficult to read. You can edit your post to include them by using the button.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You either need a form-per-row, or some JavaScript to set a hidden value with the defect ID clicked on combined w/ a programmatic submit, or just a link with the defect ID as a URL parameter--which can lead to horrible consequences if the page is crawled. Try to avoid that.
Robert Wiscup
Ranch Hand

Joined: Oct 18, 2009
Posts: 45
Hi David,

Thank you for responding . . .

I was wondering if you could provide some more details on how to solve this problem . . . nothing I try seems to work . . .

Thanks!

Rob
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

What have you tried? It's a pretty common requirement, and examples abound. It's literally just a few lines of additional code--there's not too much that can go wrong (famous last words ;)
Robert Wiscup
Ranch Hand

Joined: Oct 18, 2009
Posts: 45
Yes, should be simple . . . but when you are first learning everything can be a bit . . . shall we say . . . "painful" . . . LOL

At any rate, seems to me the simplest way would be to append the defectId to the URL . . . something like this:



However, per your previous comments this might not be the best approach . . . Javascript which receives the defectId might be better. However, I don't know how to pass the particular defectId of the row in question when I click on either a hyperlink or submit button. The defectId doesn't seem to be getting associated with the particular row. I always seem to get the first or last defectId. I'm not sure why . . .

Rob
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

A couple of comments before moving on.

Naming an individual defect "myCollection" is a bit misleading--if I saw a variable called "myCollection" I'd assume it was... a collection. And I already knew it was "mine". How about naming the collection "defects" and having each defect be named "defect"?

Using scriptlets is considered a relatively poor practice, and has been for some time. Particularly since EL is available via a JSP 2.0 container, and the Struts EL-enabled tags for containers that don't support JSP 2.0 (woe be unto you if you're using a container that old). And there's a form of the <html:link> tag that will create a link with a single parameter.

Consider not using the scriptlet, and just embedding the defectId in the link all in one shot first. If it's still not working, post the current complete code, and we'll take a look. Either that, or run with the form idea, using either a form-per-defect, or some simple JavaScript to set a hidden value with the defect ID you care about and submitting.
Robert Wiscup
Ranch Hand

Joined: Oct 18, 2009
Posts: 45
Hi David,

Ok . . . I solved this problem using:



Funny, how it all seems so simple once you know . . . And your "naming" comments are well-taken . . .

Thanks so much for your help,

Rob
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

No problem.

Just to reiterate: don't let that page get crawled.
 
Consider Paul's rocket mass heater.
 
subject: Passing row id from html table on jsp to Action Class in Struts
 
Similar Threads
How to pass JSTL value to javascript function
Problem in hiding the row of data
dynamic text box value into database
Setting Value Attribute of Bean Property in jsp in Struts Application
Help me in send redirct