aspose file tools*
The moose likes Struts and the fly likes Populating Struts Form on JSP init() or onload() Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Populating Struts Form on JSP init() or onload()" Watch "Populating Struts Form on JSP init() or onload()" New topic
Author

Populating Struts Form on JSP init() or onload()

Nina Anderson
Ranch Hand

Joined: Jul 18, 2006
Posts: 148
Hi all,

I have an default page that needs to be populated with values from the database whenever the page loads up. I'm using Struts in my JSP pages.

Currently I have a Java class that retrieve values from the database.
However, I don't know how to assign these values on my Struts form properties when the page loads up for the first time.

Can someone show me an example of how to load form values on a default jsp page using Struts?

I would appreciate your help...
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
The best way to do this is to put the logic that loads the data into an Action class and have that action class forward to the JSP that displays it.

If you want to make it work in a welcome page, one way to do it is to have an index.jsp with only the following lines:

Where myAction is the action that loads the data and forward to the JSP.


Merrill
Consultant, Sima Solutions
Nina Anderson
Ranch Hand

Joined: Jul 18, 2006
Posts: 148
Thanks for your suggestion. Yes, I would like to load default data on the Welcome page.

I tried <logic:redirect forward="myAction"/>.

The only problem is, my Welcome page is now stuck in an infinite loop. Each time the page loads up, it is sent right back to the redirect again. As a Result, the Welcome page never comes up.

Does anyone have any suggestions???
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
In this example myAction must forward to a different JSP than the one that redirected to myAction. This redirect statement should only go in your index.jsp and nowhere else.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Populating Struts Form on JSP init() or onload()