Advertisement

10.01.2003 at 12:57AM PDT, ID: 20753680
[x]
Attachment Details

Fill options list w/ DB values using STRUTS and JSP

Asked by rusco in Java Server Pages (JSP)

Tags: struts, fill, using, jsp, list

Hi!

I'm trying to find a way to get values from a database to fill up a drop down box. I've been using the strut tags of: <html:select> and <html:options> but for some odd reason, when I use this my html code get's 'cut off' when I view it.
First, I click a link on my 'initial' page (list_reports) and it should fill up the listbox (activities) in the create_new_report page.
Attached are my codes (or snippets of them):

list_reports.jsp:

                    <tr>
                        <td width="10"></td>
                        <td class="color003366" width="10">»</td>
                        <td width="140"><html:link action="/setUpReportForm">new
                        report</html:link></td>
                    </tr>

create_new_report.jsp:

<html:form action="/CReport"
                               name="newReportForm"
                               type="NewReportForm" >

...other form objects...

                        <td>
                         <html:select name="newReportForm" property="activities" size="4">
                         <html:options collection="setUpReport" property="id"/>
                         </html:select>                        
                         <a HREF="create_new_activity.jsp">
                         <html:img src="/wrs/images/add_button.gif" border="0" style="width:45; height=:20" />
                         </a>
                        </td>
</html:form>

struts-config.xml:

        <action

            path="/setUpReportForm"
           
            name="newReportForm"

            type="setUpReport"
           
            validate="false"
           
            input="/list_reports.jsp"/>

        <forward

            name="continue"

            path="/create_new_report.jsp"/>

setUpReport.java: (Action)

public final class setUpReport extends Action {
   
    ActionErrors errors = new ActionErrors();
    ArrayList list = new ArrayList();
       
    protected Collection getActivities()
             throws Exception {

             Connection conn = null;
             Statement stmt = null;
             ResultSet rs = null;

             ServletContext context = servlet.getServletContext();
             DataSource dataSource = (DataSource)context.getAttribute(Action.DATA_SOURCE_KEY);
         
             try {
   
                 conn = dataSource.getConnection();
                 stmt = conn.createStatement();
             
                 rs = stmt.executeQuery("SELECT Activity_Name FROM T006_Activities");
                 while ( rs.next() ) {
                     list.add( new activityBean (rs.getString("Activity_Name"), rs.getString("Activity_Name")) );
                 }
             
             } //end of try
       
             catch (SQLException e) {
                 errors.add(ActionErrors.GLOBAL_ERROR, new ActionError( "sql.exceptions", e.getMessage() ) );
                 System.err.println(e.getMessage());
             } //end of catch
           
             finally {
                 if (rs != null) {
                     try {
                         rs.close();
                     }
               
                     catch (SQLException sqle) {
                         errors.add(ActionErrors.GLOBAL_ERROR, new ActionError( "sql.exceptions", sqle.getMessage() ) );
                         System.err.println(sqle.getMessage());
                     }
               
                     rs = null;
                 } //end of if (rs !=null)
           
                 if (stmt != null) {
                     try {
                         stmt.close();
                     }
                     catch (SQLException sqle) {
                         errors.add(ActionErrors.GLOBAL_ERROR, new ActionError( "sql.exceptions", sqle.getMessage() ) );
                         System.err.println(sqle.getMessage());
                     }
                     stmt = null;
                 } //if (stmt != null)
           
                 if (conn != null) {
                     try {
                         conn.close();
                     }
                     catch (SQLException sqle) {
                         errors.add(ActionErrors.GLOBAL_ERROR, new ActionError( "sql.exceptions", sqle.getMessage() ) );
                         System.err.println(sqle.getMessage());
                     }
                     conn = null;
                 } //end of if (conn != null)
             } //end of finally
             return list;
         } //end for method setActivities()
     
//  ******************EXECUTE****************//
   
      public ActionForward execute(ActionMapping mapping,
              ActionForm form,
              HttpServletRequest request,
              HttpServletResponse response)
              throws IOException, ServletException, Exception {
                 
                  Collection activities = getActivities();
                  HttpSession session = request.getSession();
                  session.setAttribute( "activities", activities);
                  NewReportForm newReportForm = (NewReportForm)form;
                  request.setAttribute(mapping.getAttribute(), newReportForm);  
                  return (mapping.findForward("continue"));  
                         
      } //end for execute method
} //end for setUpReport class

activityBean.java:

public class activityBean {
   
    private String activity;
    private String description;
   
    public activityBean() {
       
    }
   
    public activityBean( String activity, String description ) {
        this.activity = activity;
        this.description = description;
    }
   
    public String getActivity() {
        return activity;
    }
   
    public void setActivity(String activity) {
        this.activity = activity;
    }
   
    public String getDescription() {
        return description;
    }
   
    public void setDescription(String description) {
        this.description = description;
    }
}Start Free Trial
[+][-]10.02.2003 at 12:20AM PDT, ID: 9472504

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.02.2003 at 11:54PM PDT, ID: 9482864

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10.05.2003 at 11:28PM PDT, ID: 9496537

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.06.2003 at 12:30AM PDT, ID: 9496765

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10.06.2003 at 11:49PM PDT, ID: 9503682

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.07.2003 at 12:48AM PDT, ID: 9503902

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 7-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]10.08.2003 at 02:41AM PDT, ID: 9512183

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 7-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]10.20.2003 at 12:14AM PDT, ID: 9581577

View this solution now by starting your 7-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

 

About this solution

Zone: Java Server Pages (JSP)
Tags: struts, fill, using, jsp, list
Sign Up Now!
Solution Provided By: rusco
Participating Experts: 1
Solution Grade: A
 
 
[+][-]10.20.2003 at 04:08AM PDT, ID: 9582549

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 7-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20080716-EE-VQP-32