Hai I wrote a
Struts Code which populates States list on choosing a Selected Country Using AJAX and It is showing me the Error as
javax.servlet.jsp.JspException: Cannot find bean: "ListForm" in any scope
My Entire Code is as below
INDEX.jsp
<%@ page language="java" import="java.util.*,example.*" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ taglib uri="/WEB-INF/struts-html.tld" prefix="html" %>
<%@ taglib uri="/WEB-INF/struts-bean.tld" prefix="bean" %>
<%@ taglib uri="/WEB-INF/struts-logic.tld" prefix="logic" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>
<script type="text/javascript">
function getStates(str)
{
var xmlhttp;
if(str=="")
{
document.getElementById("result").innerHTML="Please Choose a Country";
return;
}
if(window.XMLHttpRequest)
{
xmlhttp=new XMLHttpRequest();
}
else
{
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.open("GET","getList.do?country="+str,true);
xmlhttp.onreadystatechange=function()
{
if(xmlhttp.readyState==4||xmlhttp.status==200)
{
document.getElementById("statesList").innerHTML=xmlhttp.responseText;
}
}
xmlhttp.send(null);
}
</script>
</head>
<body>
<html:form action="getList.do">
<html:select property="country" onchange="getStates(this.value)">
<html:option value="select">Select</html:option>
<html:option value="India">India</html:option>
<html:option value="America">America</html:option>
<html:option value="Australia">Australia</html:option>
</html:select>
<div id="result"></div>
<bean:define id="statesList" name="ListForm" property="statesList"></bean:define>
<html:select property="states">
<html:options collection="statesList"/>
</html:select>
</html:form>
</body>
</html>
FormBean(ListForm)
package example;
import org.apache.struts.action.ActionForm;
import java.util.ArrayList;
public class ListForm extends ActionForm
{
private ArrayList statesList;
private
String country;
private String states;
public void setStates(String states)
{
this.states=states;
}
public void setCountry(String country)
{
this.country=country;
}
public String getCountry()
{
return country;
}
public String getStates()
{
return states;
}
public void setStatesList(ArrayList statesList)
{
this.statesList=statesList;
}
public ArrayList getStatesList()
{
return statesList;
}
}
ACTIONCLASS(ListAction)
package example;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.ArrayList;
import java.util.Iterator;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
public class ListAction extends Action
{
public ActionForward execute(ActionMapping mapping,ActionForm form,HttpServletRequest request,HttpServletResponse response) throws Exception
{
ListForm myform=(ListForm)form;
String country=myform.getCountry();
ArrayList states=displayStates(country);
request.getSession().setAttribute("statesList",states);
return null;
}
public ArrayList displayStates(String country)
{
ArrayList statesList=new ArrayList();
if(country.equals("India"))
{
statesList.add("India1");
statesList.add("India2");
statesList.add("India3");
statesList.add("India4");
}
else if(country.equals("America"))
{
statesList.add("America1");
statesList.add("America2");
statesList.add("America3");
statesList.add("America4");
}
else if(country.equals("Australia"))
{
statesList.add("Australia1");
statesList.add("Australia2");
statesList.add("Australia3");
}
return statesList;
}
}
Struts-Config.XML
<?xml version="1.0" encoding="ISO-8859-1" ?>
<!DOCTYPE struts-config PUBLIC
"-//Apache Software Foundation//DTD Struts Configuration 1.2//EN"
"http://struts.apache.org/dtds/struts-config_1_2.dtd">
<struts-config>
<data-sources>
</data-sources>
<form-beans>
<form-bean name="myform" type="example.ListForm"></form-bean>
</form-beans>
<global-forwards>
</global-forwards>
<action-mappings>
<action path="/getList" name="myform" scope="request" type="example.ListAction" input="/index.jsp">
</action>
</action-mappings>
<controller>
</controller>
</struts-config>
Please get me out from that Error.