aspose file tools*
The moose likes Struts and the fly likes need to transfer productID from url to action class in struts 2 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "need to transfer productID from url to action class in struts 2" Watch "need to transfer productID from url to action class in struts 2" New topic
Author

need to transfer productID from url to action class in struts 2

Prashant K. Singh
Greenhorn

Joined: May 11, 2009
Posts: 18
Hi dear,

I am trying to transfer the productId from url to action class and have not been able to do that.

my front end jsp is as follows:




My action class showpdtsuppliers.java is as follows:



My Supplier.java class is as follows:



As you can see that I am trying to get the productId and have used:



but its been of no help.

I have also used:



butthis method has also not been able to get the productID from the front end jsp.

I am appending the output.

xxxxxxxxxxxxxx
Product Id =
Driver Loaded
Database is connected
Product Id =
Product Name = null
com.mysql.jdbc.PreparedStatement@8ab08f: Select suppliername from supplier_proddetails where productid=''[B@14d921a
rscom.mysql.jdbc.ResultSet@1b32627
xxxxxxxxxxxxxxxxx

Please help.




Prashant Singh
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

...

Use the productId action class property; there's zero reason to use anything from the request in this usecase.

The advice given you on struts-user to move the database code into a service is good advice: it makes the action and service far, far more testable.
Prashant K. Singh
Greenhorn

Joined: May 11, 2009
Posts: 18
Hi Dave,

I have made changes in the action class as you have proposed. But I have still not been able to get the productId from the front end jsp.



Here is the output

xxxxxxxxxxxxx
Driver Loaded
Database is connected
Product Id = null
com.mysql.jdbc.PreparedStatement@ad157f: Select suppliername from supplier_proddetails where productid=null[B@15e538e
rscom.mysql.jdbc.ResultSet@1bfa3d3
xxxxxxxxxxxxx
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Remove line 28.
Prashant K. Singh
Greenhorn

Joined: May 11, 2009
Posts: 18
Hi dear,

I have been trying to read the productID from the url but it goes into an infinite loop at point 1. I am posting the code below. Please help.

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Continued on struts-user.

Nirmal Jatania
Greenhorn

Joined: Apr 17, 2009
Posts: 19
Just add <s:hidden name="productId" /> It will pass your productId at URL automatically.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

No, that's only if there's a form, and isn't the easiest way to do this--you'd have to create a form and submit for every item.

Putting the productId in the URL is the canonical solution.
Nirmal Jatania
Greenhorn

Joined: Apr 17, 2009
Posts: 19
I am giving you my example through which i am implementing my all action classes.



Now implement all of your action classes in following way, i am putting one of my action class as for example :


You have to create view bean & in which you have to add all the setter & getter method of your front side jsp page. Your setter & getter method will handle all the thing & you will get all the data from jsp to action classes.


David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Besides that his question has already been answered (on the struts-user mailing list), this doesn't really address the original question, which was how to get a value from a URL into an action. A complete example of that is a few lines of JSP and a few lines of Java. Adding more to it than that obfuscates any solution.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: need to transfer productID from url to action class in struts 2