aspose file tools*
The moose likes Struts and the fly likes Generate select menu from database 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 "Generate select menu from database" Watch "Generate select menu from database" New topic
Author

Generate select menu from database

Trevar Pearce
Greenhorn

Joined: Jul 13, 2006
Posts: 6
Hi,

I'm attempting to build a struts 1.1 form and I need to generate the options in a select menu from the database. The application will be only one jsp with two sections, the form and the results. I have written a database access class and a bean to store the arraylist of results. I am stumbling, however, on how exactly to call this class from the jsp. Is there a way to do this without going through an action class (which would require some sort of page refresh, which I'd like to avoid)? Can I use JavaScript to call the class (my first thought was using an onload event to call it, but I'm not clear on whether it's even possible to call a java class from javascript)?

Thanks,

Trevar
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
If you're going to use the Struts framework, you might as well do things in the Struts way.

The "Struts way" is to associate an Action and ActionForm with a JSP, and have Struts call the Action class when the form is submitted.

When I'm writing a search page, I typically do what you're doing: Use the same JSP for both entering search criteria and displaying results. I just put <logic:notEmpty></logic:notEmpty> tags around the result table so that it's only visible if there are results to display.

I'm not clear on whether it's even possible to call a java class from javascript)?


Before AJAX, I would have given an unqualified "No" to this question. The answer is still technically no, but now there are ways that make it seem as though you're calling server-side java from client-side JavaScript.

I'd recommend you look into the DWR if you want more information on how this is possible.

The problem is, though, that even if you get the results in JavaScript, you then have the tedious task of modifying the page using the Document Object Model (DOM) and JavaScript. It's much easier just to submit the form and redisplay the page with the results. In a situation like this, the difference in response time between this and the AJAX solution would be negligible.
[ July 20, 2006: Message edited by: Merrill Higginson ]

Merrill
Consultant, Sima Solutions
Trevar Pearce
Greenhorn

Joined: Jul 13, 2006
Posts: 6
Hi,

Thanks for the reply Merrill. I'm still unclear on how to do something though.

I want to populate an html select menu in the struts framework with query results from an Oracle database. The problem is that I want to do it as soon as the web-app starts. Is there a way to accomplish this?

Thanks, Trevar
Merrill Higginson
Ranch Hand

Joined: Feb 15, 2005
Posts: 4864
I would suggest you create an index.html file and register this file as a welcome page in your web.xml file.

In this page insert a meta tag to redirect to a Struts action like this;

<meta http-equiv="refresh" content="2;url=myAction.do">

Then have myAction get the data from the database, put the List in some scope (request, session, application) and then a forward specifying the JSP you want displayed.

In your JSP, include an <html:select> tag with either an <htmlptions> or <htmlptionsCollection> tag. This link give you information about how to use these tags.


If you want to populate the list of options and put it in application scope so that any page can use it and you want to do this as soon as the application starts, you need to write a class that extends org.apache.struts.action.Plugin, put your logic in the init() method, and register the plugin in your struts-config.xml file.
[ July 21, 2006: Message edited by: Merrill Higginson ]
 
 
subject: Generate select menu from database