My jquery is:
$('select[name=select1]').change(function () {
var cod=$(this).val();
if (cod>0) {
$.ajax({
url:"Load.do",
data: "cod="+cod,
success: function (response) {
$('#info').html(response); }
});
}
});
In the struts-config.xml, I have:
<action path="/Load" scope="request" type="mypackage.Load"></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);
PrintWriter out = response.getWriter();
while (rs.next()) {
out.print("<html:option value='");
out.print(rs.getString("firstCode"));
out.print("'>");
out.print(rs.getString("name"));
out.println("</html:option>");
out.flush();
}
return null;
} 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 null;
}
}
The data are retrieved, but they aren't printed in the info div.
In my .jsp page, the select which I want fill is:
<html:select property="prop1">
<html:option value="0">Choose:</html:option>
<div id="info"></div>
</html:select>
The JSP code is not really relevant. What matters is the generated HTML code. Use Firebug or a similar tool to inspect the contents of your page, and see what the problem is. If the Struts action returned the appropriate HTML, like the following:
<option value="2">some label</option>
<option value="3">some other label</option>
then the above Javascript would replace the contents of the div identified by info
with the above HTML. So the end result would be
<select name="prop1">
<option value="0">Choose:</option>
<div id="info">
<option value="2">some label</option>
<option value="3">some other label</option>
</div>
</select>
And the above is invalid HTML. The options shouldn't be inside a div, but directly under the select element.
Give the select element an ID, and replace the contents of the select element by the response of your AJAX request.
But the other problem is that your action doesn't generate HTML code, but JSP code containing Struts tags. This makes no sense. Struts tags are interpreted by a JSP, at server-side. But you're sending them directly to the browser. The browser only understands HTML. It doesn't know about Struts tags.
Why aren't you using the recommended MVC approach? The action should generate a list of beans, store it in the request, and forward to a JSP which would generate the HTML markup using tags.
Finally, you should