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_butto
n.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.j
sp"/>
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.getAtt
ribute(Act
ion.DATA_S
OURCE_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_Na
me"), rs.getString("Activity_Nam
e")) );
}
} //end of try
catch (SQLException e) {
errors.add(ActionErrors.GL
OBAL_ERROR
, new ActionError( "sql.exceptions", e.getMessage() ) );
System.err.println(e.getMe
ssage());
} //end of catch
finally {
if (rs != null) {
try {
rs.close();
}
catch (SQLException sqle) {
errors.add(ActionErrors.GL
OBAL_ERROR
, new ActionError( "sql.exceptions", sqle.getMessage() ) );
System.err.println(sqle.ge
tMessage()
);
}
rs = null;
} //end of if (rs !=null)
if (stmt != null) {
try {
stmt.close();
}
catch (SQLException sqle) {
errors.add(ActionErrors.GL
OBAL_ERROR
, new ActionError( "sql.exceptions", sqle.getMessage() ) );
System.err.println(sqle.ge
tMessage()
);
}
stmt = null;
} //if (stmt != null)
if (conn != null) {
try {
conn.close();
}
catch (SQLException sqle) {
errors.add(ActionErrors.GL
OBAL_ERROR
, new ActionError( "sql.exceptions", sqle.getMessage() ) );
System.err.println(sqle.ge
tMessage()
);
}
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(mappi
ng.getAttr
ibute(), newReportForm);
return (mapping.findForward("cont
inue"));
} //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