Very early to study Ajax through the JSON way to go back to the list to the foreground display, today finally made out, put the code to share to everyone
-
The Ajax JSON method requires a jar package, which is shared from the attachment to everyone.
-
First I used the development framework of SSI2, and then I put the code to everyone.
(1) front page jhxdxukezheng.jsp
<script type = "text / javascript">
function plannochange () {
var planno = $ ("# planno"). val ();
$ .ajax ({
type: ‘post’,
url: ‘SampleAction_selectProducttypeByPlanno’,
dataType: ‘json’,
data: {
planno: planno,
Hell
},
success: function (data) {
// alert (data);
$ ("# producttype"). empty ();
// $ ("# producttype"). append ("<option> Please choose </ option>");
$ .each (data, function (i, item) {
// alert (item.name + "," + item.value);
$ ("# producttype"). append ("<option value =‘ "+ item.name +" ‘>" + item.value + "</ option>");
Ranch
});
Hell
},
error: function () {
alert ("Error");
}
});
};
</ script>
<b> Plan ID: </ b>
<s: select list = "# request.plannolist" listKey = "planno"
listValue = "planno" id = "planno" onchange = "plannochange ()"> </ s: select>
<b> Product type: </ b>
<s: select list = "jsonarry" listKey = "name" listValue = "value" headerKey = "1" headerValue = "Please select" id = "producttype"> </ s: select>
(2) action class
public String selectProducttypeByPlanno () {
response.setContentType ("text / html; charset = UTF-8");
String planno = request.getParameter ("planno");
List <Producttype> producttypelist = sampleService
.selectProducttypeByPlanno (planno);
JSONArray jsonarry = JSONArray.fromObject (producttypelist);
try {
PrintWriter out = response.getWriter ();
System.out.println (jsonarry.toString ());
out.print (jsonarry.toString ());
out.flush ();
out.close ();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace ();
}
return null;
}
Note: 1. The data transmission method here is: json method {"1": "aa", "2": "bb"}
2. $ ("# producttype"). Empty () means empty the drop-down list alert (item.name + "," + item.value);
$ ("# producttype"). append ("<option value =‘ "+ item.name +" ‘>" + item.value + "</ option>");
3.JSONArray jsonarry = JSONArray.fromObject (producttypelist) represents converting the list collection into jsona rry format, and then using PrintWriter stream output parameter jsonarry.toString () to return to the front page
This article is from the "Big Talk Program" blog, please be sure to keep this source http://houqida.blog.51cto.com/8877896/1580003
Strus2+mybatis+spring Ajax+json returns list to cascade for select