Thanks for your response, I will get right on it... And ttry to figure it out...
Have some questions though... I get the idea of the listener, but if you populate it with stuff from a database, this would be wrong I guess, since this collection never updates (except when you restart you application). So what you do here is to create a constant (based on stuff when the aplication starts)... Or did I get i wrong? If i'm not going to do the listner "thing", since my list must refresh from database once and awile... Where do I put the Colloction creating code, say this: Collection coll = new ArrayList(); for(int i = 0; i < 10; i++) { b = new LabelValueBean("1",1); coll.add(b); } request.setAttribute("MY_LIST", coll); And when I do this on my jsp-page: <html:select property="selectedValue" value="-1"> <html:options collection="MY_LIST" property="value" labelProperty="label" /> </html:select> Do "value" and "label" represent a getValue() and getLabel() function in LabelValueBean? Thanks again for your response, cleared up alot in my head :D /Jan -----Ursprungligt meddelande----- Från: A mailing list about Java Server Pages specification and reference [mailto:[EMAIL PROTECTED] För Christian Bollmeyer Skickat: den 20 oktober 2003 21:01 Till: [EMAIL PROTECTED] Ämne: Re: Struts question Am Montag, 20. Oktober 2003 15:29 schrieb Jan Arenö: > Hi Hi Jan, > I'm learning Struts at the moment and just finished a very easy > example. Now when I want to extend it I don't know for sure where to > put it, or if I have missunderstood something. > > I'm also new to taglibs. So i don't know exacly how they work either, > but I think I start to get a hang of it > > I have inputpage with ActionForm and Action-servlets There is only one ActionServlet. You surely mean Struts Actions? > On my input page i have 2 fields at the moments, destination and type. > These fields are validated and errormessages are returned as they > should etc etc. When validated I go to page2.jsp that writes "Hello > World" > > Ok. Heter is my problem. > I now want to change my destination from a inputform <html:text> to a > selectbox <html:select>. And I would like to populate it with a Vector > that I shall get from a database. First of all: Struts most elegantly supports this via the <html:options> tag. You might also want to have a look at the <html:optionsCollection> tag. Both are very flexible. The basic idea is that you populate your select box with values coming from a Bean. The selected value then is passed via the <html:select> property. The Collection itself can be put in any scope (request, session, application). For static lists, as may be your case as well, I usually use a Listener to put everything in application scope when the app starts. Here comes an code snippet of a very basic listener: package listeners; import java.util.*; import javax.servlet.*; import PersistenceFacade; // some Singleton DAO class that does the JDBC part public class ResourceListener implements ServletContextListener { public void contextInitialized(ServletContextEvent e) { Collection coll = PersistenceFacade.getInstance().getSomeList(); e.getServletContext().setAttribute("MY_LIST", coll); } public void contextDestroyed(ServletContextEvent e) { ((ServletContext) e.getServletContext()).removeAttribute("MY_LIST"); } The Collection itself is usually an ArrayList built from LabelValueBeans (org.apache.struts.util) as follows (note: code heavily simplified here for sake of brevity) - your DAO that does the database access might look like this somewhere (though I prefer the iBATIS Database Layer recently, credits to Ted Husted who pointed me there (www.ibatis.com)): [..] Collection coll = new ArrayList(); while (rs.next()) { b = new LabelValueBean(rs.getString(<LABEL_COLUMN_NAME>), rs.getString(<VALUE_COLUMN_NAME>)); coll.add(b); } return coll; So you finally have your your list stored in some scope. Now, in the JSP page, you can just put something like this: <html:select property="selectedValue" value="-1"> <html:options collection="MY_LIST" property="value" labelProperty="label" /> </html:select> When the users selects something, it ends up in the 'selectedValue' property of your ActionForm. You can specify a default value ("-1" in this case) by setting the 'value' property of <html:select>. > Earlier (before struts) I would create a bean conn. Then call : > > conn.createDBConnection(); If your server allows, don't use DriverManager anymore, but make use of DataSources and use JNDI to locate it. Some more code in this direction: private DataSource getDataSource() { DataSource ds = null; try { Context ctx = new InitialContext(); ds = (DataSource) ctx.lookup(Constants.SC_JNDI_DATASOURCE); } catch (NamingException e) { log(e.getMessage()); } return ds; } If you have luckily got a DataSource, it's easy to get a Connection object from that: private Connection getConnection() { Connection conn = null; try { conn = getDataSource().getConnection(); if (conn != null) { conn.setAutoCommit(false); } } catch (SQLException e) { log(e.getMessage()); } return conn; } Then, it's business as usual. > Vector myDest = conn.getDestinations(); conn.closeDBConnection(); > > Where should I now do this? Where should I store the Vector? No. Though it can be done, you should store the Vector (better not say 'Vector', as that is implementation-specific, but make use of interfaces instead, and Vector implements the Collection interface) apart from the form. You can use any scope you like, request for 'per-incident' lists, session scope for each user individually or application scope for all of them. Each 'scope' has a setAttribute() method, so you're free to choose :-) > In the ActionForm? I thought that it only had references to the fields > that the user are supposed to submit. The Action class is not called > until ActionForm is validated, ain't it so? Exactly. The information *what* is in the list normally comes from somewhere else. The value the user finally selects ends up as a property in your ActionForm. > How should this be done... > > Thanks for any help HTH, -- Chris (SCPJ2) ======================================================================== === To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST". For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST". Some relevant archives, FAQs and Forums on JSPs can be found at: http://java.sun.com/products/jsp http://archives.java.sun.com/jsp-interest.html http://forums.java.sun.com http://www.jspinsider.com --- Incoming mail is certified Virus Free. Checked by AVG anti-virus system (http://www.grisoft.com). Version: 6.0.528 / Virus Database: 324 - Release Date: 2003-10-16 =========================================================================== To unsubscribe: mailto [EMAIL PROTECTED] with body: "signoff JSP-INTEREST". For digest: mailto [EMAIL PROTECTED] with body: "set JSP-INTEREST DIGEST". Some relevant archives, FAQs and Forums on JSPs can be found at: http://java.sun.com/products/jsp http://archives.java.sun.com/jsp-interest.html http://forums.java.sun.com http://www.jspinsider.com