首先导入包
json-lib-2.1.jar
struts2-json-plugin-2.1.8.1.jar(这两个是JSON所必须的包)
ezmorph-1.0.3.jar, commons-collections-3.2.jar
commons-beanutils-1.7.0.jar
commons-logging-1.0.4.jar
commons-lang-2.3.jar (因为json大量引用了Apache commons的包,所以需要引入commons的四个包。)
Entity
public class Student
{
private String name;
private String num;
...................
}
JSP——传一个对象去action,和表单传对象一样
<script type="text/javascript">
$(function() {
$(".btnSave").click(function() {
var name = $("input[name=stu.name]").val();
var num = $("input[name=stu.name]").val();
$.get("stu/Stu_tosave.action", {
"stu.name" : name,
"stu.num" : num,
}, function(data, status) {
if (status == "success") {
$('.hint').html("更新成功!");
}
});
});
});
<input type="button" class="btnSave" value="保存" /> 注意哦,是button类型,不是submit
Action
private Student stu;
public String tosave()
{
.............
stuService.save(stu);
.............
}