aspose file tools*
The moose likes Struts and the fly likes how to display database values in a table Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "how to display database values in a table" Watch "how to display database values in a table" New topic
Author

how to display database values in a table

Prashant K. Singh
Greenhorn

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

I am trying to disply the product column from the database into a table. I am able to add the items to the ArrayList
But the items are not being displayed in the jsp. My action class is as follows:



The jsp is not printing the product name stored in the database. The jsp is as follows:


Prashant Singh
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Line 38 creates a new, local "productsdealt" list. The action property is never set or filled, so there's nothing to display.

From a stylistic standpoint it's more common to use names like "productsDealt", which is arguably easier to read.
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
So either you can set "productsdealt" to session scope or request scope. And accordingly retrieve it in jsp.


SCJP 1.4, SCWCD 5, SCBCD 5, OCPJWSD 5,SCEA-1, Started Assignment Part 2
My blog- http://rkydesigns.blogspot.com
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

@Amandeep: That's not the preferred mechanism in Struts 2--there's no reason to set something to a JEE scope unless you have a specific requirement to do so.
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
so what is the recommended approach to do this in struts 2?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Expose an action property, as the OP did in the second code snippet (but never filled it with values).
Prashant K. Singh
Greenhorn

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

I modified the action class and the jsp a bit and now I am able to print the output on the jsp.

But Hulloa there is a problem. Also every time I hit the refresh button, the output gets increased by one ie if i have bariton as output then it becomes barition bariton.

I am again posting the code:

Here is the Productdisplay.java



Here is the Product.java



Here is the producthandles.jsp

David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Are you using Spring?

It's difficult to understand code's intent when there are ambiguities--for example, the action method creates a local variable called "productName", and the action has a property called "productName", and they're different types.
Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
David Newton wrote:Expose an action property, as the OP did in the second code snippet (but never filled it with values).


As i am a newbie to Struts 2. Just a question, in the previous code how Prashant is exposing the 'products' to the jsp.
Because inside the ActionSupport, we are not exposing it through any reference.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

how Prashant is exposing the 'products' to the jsp

Amandeep Singh
Ranch Hand

Joined: Jul 17, 2008
Posts: 844
this i know. i actually meant is this exposed as a action property inside struts-config.xml

as i know struts 1. this feature is available only in struts 2.

please correct me if i am wrong.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

You're not wrong--this is Struts 2, as was the original question.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: how to display database values in a table