This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes JSP and the fly likes could not load the select box during pageload. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » JSP
Reply locked New topic
Author

could not load the select box during pageload.

rinki gose
Greenhorn

Joined: Sep 25, 2008
Posts: 4
Hi readers,

i tried to load a combo box using the htmlptions collection during the page load in the jsp page from a database. i prepared form bean,action,configured the xml files, kept the necessary tag libraries. i am able to get the values from the database into the arraylist that i am using if i make the code containing the database operation as a dao and test it. but unfortunately if i make it in the same action class by calling it, i couldnt fire the action itself

please see the code below and make me know where lies the error

jsp page:

<%@ page import="com.personalinfo.beans.*" %>
<%@ page import="com.personalinfo.form.*" %>
<bean:define id="genderListOptions" name="PersonalInformationForm" property="genderListOptions" type="java.util.ArrayList"/>
.
<html:form action="/PersonalInformationAction">
.
.
<html:select property="selGender" styleClass="txtwidh">
<htmlptions collection="genderListOptions" property="value" labelProperty="label"/>
</html:select>
.
</html:form>


model class:

package com.personalinfo.model;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;

import com.personalinfo.form.PersonalInformationForm;

public class PersonalInformationModel
{
/*public static void main(String args[])
{
PersonalInformationModel personalInformationModel=new PersonalInformationModel();
ArrayList arr=personalInformationModel.fillGender();
}*/
public ArrayList fillGender()
{
ArrayList<String> listOptions=new ArrayList();
boolean valid = false;
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
//PersonalInformationForm personalinformationForm = null;
try
{
PersonalInformationForm personalInformationForm=new PersonalInformationForm();
Class.forName("com.mysql.jdbc.Driver");
con = DriverManager.getConnection("jdbc:mysql://localhost/personalinformation", "", "");
stmt = con.createStatement();
rs = stmt.executeQuery("SELECT gender FROM person");
while(rs.next())
{
String gen=rs.getString("gender");
listOptions.add(gen);
System.out.println(gen);
}
personalInformationForm.setGenderListOptions(listOptions);



}
catch (ClassNotFoundException e)
{
e.printStackTrace();
}
catch (SQLException e)
{
e.printStackTrace();
}
finally
{
try { if (rs != null) rs.close(); } catch (SQLException e) {};
try { if (stmt != null) stmt.close(); } catch (SQLException e) {};
try { if (con != null) con.close(); } catch (SQLException e) {};
}
return listOptions;
}
}

action class:

public class PersonalInformationAction extends Action {
public ActionForward execute(
ActionMapping mapping,
ActionForm form,
HttpServletRequest request,
HttpServletResponse response) throws Exception{
System.out.println("hii action");
PersonalInformationForm personalInformationForm = (PersonalInformationForm)form;
PersonalInformationModel perInformationModel=new PersonalInformationModel();
ArrayList arr=perInformationModel.fillGender();

return mapping.findForward("success");
}
}

form class:

public class PersonalInformationForm extends ActionForm {

private ArrayList genderListOptions = new ArrayList();
private String selGender;

public void setSelGender(String string)
{
this.selGender = string;
}
public String getSelGender()
{
return this.selGender;
}

public ArrayList getGenderListOptions() {
return genderListOptions;
}
public void setGenderListOptions(ArrayList genderListOptions) {
this.genderListOptions = genderListOptions;
}

public void reset(ActionMapping mapping,HttpServletRequest request)
{
PersonalInformationModel personalInformationModel=new PersonalInformationModel();
ArrayList arr=personalInformationModel.fillGender();
request.setAttribute( "genderListOptions", arr);

}

struts-config.xml:

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="PersonalInformationForm" type="com.personalinfo.form.PersonalInformationForm"/>
</form-beans>
<action-mappings>
<action path="/PersonalInformationAction" type="com.personalinfo.action.PersonalInformationAction" name="PersonalInformationForm" scope="session">
<forward name="failure" path="/jsp/personal.jsp"/>
<forward name="success" path="/jsp/x.jsp"/>
</action>
</action-mappings>
</struts-config>

web.xml:

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE web-app PUBLIC "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN" "http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app >
<servlet>
<servlet-name>action</servlet-name>
<servlet-class>org.apache.struts.action.ActionServlet</servlet-class>
<init-param>
<param-name>config</param-name>
<param-value>/WEB-INF/struts-config.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>/jsp/personal.jsp</welcome-file>
</welcome-file-list>


<!-- Struts Tag Library Descriptor -->
<taglib>
<taglib-uri>struts-bean.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-bean.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>struts-html.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-html.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>c.tld</taglib-uri>
<taglib-location>/WEB-INF/c.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>struts-tiles.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-tiles.tld</taglib-location>
</taglib>


</web-app>


please let me know the solution ASAP.
Kuladip Yadav
Ranch Hand

Joined: Jul 30, 2008
Posts: 162

Hi,

In the action you are not setting arraylist to the form
So your Action class will be like that :




Thanks
rinki gose
Greenhorn

Joined: Sep 25, 2008
Posts: 4
Hi kuldeep,

thanks for your suggestion.

i have already included that line of code in the model class itself. eventhough i included it in the action class i couldnt load the select box.

in this case, one interesting thing is happening.

first of all, during the pageload the select box is not getting populated eventhough i am able to get the values from the database (tested the model class by including the main method in it).

second, if i submit the form, the action is mapping to the success page view ie the page containing the select box(but not getting populated). with in seconds, it is being redirected to the failure view page.

kindly assist me if there are any errors in the code. i need to know how to load the select box during pageload or at the time of submitting the form. take these as two cases!
rinki gose
Greenhorn

Joined: Sep 25, 2008
Posts: 4
Hi kuldeep,

a small correction

take this as the struts-config.xml for the application,

<?xml version="1.0" encoding="iso-8859-1"?>
<!DOCTYPE struts-config PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 1.1//EN" "http://jakarta.apache.org/struts/dtds/struts-config_1_1.dtd">
<struts-config>
<form-beans>
<form-bean name="PersonalInformationForm" type="com.personalinfo.form.PersonalInformationForm"/>
</form-beans>
<action-mappings>
<action path="/PersonalInformationAction" type="com.personalinfo.action.PersonalInformationAction" name="PersonalInformationForm" scope="session">
<forward name="success" path="/jsp/personal.jsp"/>
<forward name="failure" path="/jsp/x.jsp"/>
</action>
</action-mappings>
</struts-config>
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 60785
    
  65

Please do not post the same question more than once.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: could not load the select box during pageload.
 
Similar Threads
Servlet Action is not available(Very Urgent)
Action is not firing from the jsp page
Problem with my first struts application
Problem with my first struts application
struts forward link?