I have two selections. The second selection values depend on the first ones.
When the user select a value in the first select, an Action is called to fill the second select.
It works only if the user submits the form and there are validation errors.
how could I reload the div when the user selects a value in the first select?
My jquery is:
$('select[name=select1]').change(function () {
var cod=$(this).val();
if (cod>0) {
$.ajax({
url:"Load.do",
data: "cod="+cod,
});
}
});
In the struts-config.xml, I have:
<action path="/Load" scope="request" type="mypackage.Load">
<forward name="Success" path="/jsp/myForm.jsp"/>
</action>
The class Load is an Action:
public class Load extends Action {
public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response)
{
// TODO Auto-generated method stub
Connection con = null;
Statement stmt = null;
ResultSet rs = null;
try {
// get the connection
stmt = (Statement) con.createStatement();
String cod = request.getParameter("cod");
String select = new String("Select firstCode, name from mytable where secondCode = '");
select = select.concat(cod);
select = select.concat("';");
rs = stmt.executeQuery(select);
ArrayList<LabelValueBean> arr = new ArrayList<LabelValueBean>();
while (rs.next()) {
arr.add(new LabelValueBean(rs.getString("name"),rs.getString("firstCode")));
}
request.getSession().setAttribute("arr", arr);
return mapping.findForward("Success");
} catch (NamingException e) {
e.printStackTrace();
} catch (SQLException e) {
e.printStackTrace();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (rs != null) {
try { rs.close(); } catch (SQLException e) {}
rs = null;
}
if (stmt != null) {
try { stmt.close(); } catch (SQLException e) {}
stmt = null;
}
if (con != null) {
try { con.close(); } catch (SQLException e) {}
con = null;
}
}
return mapping.findForward("Failure");
}
}
In my myForm.jsp page, the select which I want fill is:
<html:select property="prop1">
<html:option value="0">Choose:</html:option>
<% if (request.getSession().getAttribute("arr") != null) { %>
<html:optionsCollection name="arr" label="label" value="value"/>
<% } %>
</html:select>
If the response data contains exactly the html you want, i.e. the correct
<select>with all the options you can simply replace the 2nd select with the response data: