• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Generate select menu from database

 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ]
 
Trevar Pearce
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4864
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 <html:options> or <html:optionsCollection> 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 ]
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic