Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Select box with db data in welcome page

 
Gary Jones
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Merrill Higginson
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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">
 
Purushoth Thambu
Ranch Hand
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 425
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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>
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic