首先导入包
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的四个包。)
<wbr></wbr>
Entity
public class Student
{
<wbr><wbr> private String name;</wbr></wbr>
<wbr><wbr> private String num;</wbr></wbr>
<wbr><wbr> ...................</wbr></wbr>
}
<wbr></wbr>
JSP——传一个对象去action,和表单传对象一样
<script type="text/javascript">
<wbr>$(function() {<br><wbr><wbr>$(".btnSave").click(function() {</wbr></wbr></wbr>
<wbr><wbr><wbr>var<wbr>name = $("input[name=stu.name]").val();<br><wbr><wbr><wbr>var<wbr>num = $("input[name=stu.name]").val();<br><wbr><wbr><wbr><wbr>$.get("stu/Stu_tosave.action", {<br><wbr><wbr><wbr><wbr>"<strong>stu.name</strong>" : name,<br><wbr><wbr><wbr><wbr>"<strong>stu.num</strong>" : num,<br><wbr><wbr><wbr><wbr>}, function(data, status) {<br><wbr><wbr><wbr><wbr>if (status == "success") {<br><wbr><wbr><wbr><wbr><wbr>$('.hint').html("更新成功!");<br><wbr><wbr><wbr><wbr>}<br><wbr><wbr><wbr>});</wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr></wbr>
<wbr><wbr>});<br><wbr>});</wbr></wbr></wbr>
<wbr></wbr>
<input type="button" class="btnSave" value="保存" /> 注意哦,是button类型,不是submit
Action
private Student stu;
public String tosave()
{
<wbr><wbr> .............</wbr></wbr>
<wbr><wbr><wbr>stuService.save(stu);</wbr></wbr></wbr>
<wbr><wbr> .............</wbr></wbr>
}