aspose file tools*
The moose likes Struts and the fly likes Select box with db data in welcome page Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Frameworks » Struts
Bookmark "Select box with db data in welcome page" Watch "Select box with db data in welcome page" New topic
Author

Select box with db data in welcome page

Gary Jones
Greenhorn

Joined: Dec 11, 2004
Posts: 22
When i load the first page of my web application )made in struts) - I want to prepopulate a select box with
dyanamic data from the database.
I am able to populate the select boxes in later pages where the form bean is populated with database data and this is extracted in the select box.
But for the first pagfe where no session scope is established how do we do this.


<blockquote><font size="1" face="Verdana, Arial">quote:</font><hr>I have no special talent. <b>I am only passionately curious.</b> <br /> <br />Albert Einstein <hr></blockquote>
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
In most web applications we want the welcome page to be displayed automatically when the user enters "http://www.myServer.com/". Normally, if we list index.htm or index.jsp in the welcome file list in web.xml, the appropriate file is displayed.

If we need to pre-populate data before displaying the welcome page, we could tell our users to bookmark "www.myServer.com/welcome.do", but generally this isn't acceptable.

One solution I have used is to create an index.htm page that redirects to a struts action which will populate the data and then forward to a JSP. Just put something like the following in the document:

<meta http-equiv="refresh" content="2;url=http://www.myServer.com/welcome.do">


Merrill
Consultant, Sima Solutions
Purushoth Thambu
Ranch Hand

Joined: May 24, 2003
Posts: 425
Let's assume the initial page is welcome.jsp and url-mapping element of action servlet is set to *.do

First step

- configure the <welcome-file> element in web.xml to welcome.do

Next

- create action element in struts-config as below

<action path="/welcome" name="welcomeForm" type="test.TestAction">
<forward name="welcome" path="/welcome.jsp"/>
</action>

and the form element looks like below

<form-bean name="welcomeForm" type="test.WelcomeForm"/>

Now when you hit the web-app from the url, action "test.TestAction"'s execute() method will be invoked where you can populate the action form and in the welcome.jsp file you can iterate through the form collection and populate the fields.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Purushothaman, I tried that method, and was unable to get it to work on the WebSphere V5.1 application server I'm using. It apparently looks for a physical file of the name given in the welcome file list, and since welcome.do isn't a physical file, it doesn't find it. Were you able to get this to work using another application server?
Purushoth Thambu
Ranch Hand

Joined: May 24, 2003
Posts: 425
I tested it against WL 8.1 SP5 and it works. But I am not sure about of couple of things in servlet specification like how the welcome-file is handled if it logical file. If you can I email you the entire web-app to be tested against WebSphere.
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
Thanks, Purushothaman. I just wanted to check to see if this was an application server specific issue, and it apparently is. I checked the Servlet 2.4 specification, and it is silent on whether the file in the welcome file list can be a logical uri, or has to be a physical file. It does use the word "file", however and not "uri". Apparently WebSphere expects it to be a physical file, and WebLogic allows it to be a logical uri.
Atchuta Penmatsa
Greenhorn

Joined: Feb 15, 2002
Posts: 6
Here's another way of doing this.

1: Define a welcome page in web.xml.

<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>

2: index.jsp redirects the request to initial page

<%
response.sendRedirect("/welcome.do");
%>

3: Struts action mapping

<action path="/welcome" name="welcomeForm" type="test.TestAction">
<forward name="welcome" path="/welcome.jsp"/>
</action>
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Select box with db data in welcome page
 
Similar Threads
Populating List box in the JSP from Action Class
Html Select - to make an option selected
Select box get refresh after populating the data into a table.
Reload page without losing data
built a program that can directly run in the same page