Ajax写成绩批量录入
1.jsp,ajax的循环调用,必须要递归,否则会出错。
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <%@ taglib prefix="s" uri="/struts-tags"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %> <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"> <html> <head> <base href="<%=basePath%>"> <title>My JSP 'Scoreinsert.jsp' starting page</title> <meta http-equiv="pragma" content="no-cache"> <meta http-equiv="cache-control" content="no-cache"> <meta http-equiv="expires" content="0"> <meta http-equiv="keywords" content="keyword1,keyword2,keyword3"> <meta http-equiv="description" content="This is my page"> <script type="text/javascript" src="js/jquery.js"></script> <!-- <link rel="stylesheet" type="text/css" href="styles.css"> --> </head> <script type="text/javascript"> function sub(i) { var a = $("table tr").eq(i).children("td").eq(0).children().val(); var b = $("table tr").eq(i).children("td").eq(0).children().eq(1).val(); var c = $("table tr").eq(i).children("td").eq(1).children().val(); var d = $("table tr").eq(i).children("td").eq(2).children().val(); var e = $("table tr").eq(i).children("td").eq(3).children().val(); aja2(a,b,c,d,e,i); } function aja2(a,b,c,d,e,i){ $.ajax({ url:'BatchScoreSave.action', type:'POST', //GET async:true, //或false,是否异步 data:{ classs:a, course:b, number:c, name:d, scores:e },//提交表单数据,也可以直接用data:$('#form').serialize(), timeout:5000, //超时时间 dataType:'json', //返回的数据格式:json/xml/html/script/jsonp/text success:function(data){ //console.log(data); console.log('成功'); if(i<$("table tr").length-1){ i++; sub(i); } }, error:function(xhr,textStatus){ console.log('错误'); }, }); } </script> <body> <s:property value="classs"/>班<s:property value="course"/>成绩录入 <table cellpadding="10" border="1"> <tr> <th>班级</th> <th>学号</th> <th>姓名</th> <th>成绩</th> <th>操作</th> </tr> <s:iterator value="scorelist"> <tr> <form action="ScoreSave.action"> <td><input type="text" name="classs" value='<s:property value="id.classs"/>' size="4" readonly="readonly" style="border: 0"> <input type="text" name="score.id.course" value='<s:property value="id.course"/>' style="display:none;"></td> <td><input type="text" name="score.id.number" value='<s:property value="id.number"/>' size="4" readonly="readonly" style="border: 0"></td> <td><input type="text" name="score.id.name" value='<s:property value="id.name"/>' size="4" readonly="readonly"style="border: 0"></td> <td><input type="text" name="score.id.score" value='<s:property value="id.score"/>' size="4"style="border: 0" onkeyup="this.value=this.value.replace(/\D/g,'')"></td> <td><input type="submit" value="保存"></td> </form> </tr> </s:iterator> </table> <input type="button" value="提交" onclick="sub(1)"> </body> </html>
2.action,就是简单的一条数据一条数据的保存,一定有get和set,注意Spring注入不能写get,会报错。
public String BatchScoreSave() throws Exception{ score = new Score(); score.setId(new ScoreId()); score.getId().setClasss(classs); score.getId().setCourse(course); score.getId().setNumber(number); score.getId().setName(name); score.getId().setScore(scores); mgr.Scoresave(score); return SUCCESS; }
3.struts.xml
<package name="aaa" extends="json-default">
<action name="BatchScoreSave" class="ScoreAction" method="BatchScoreSave"> <result name="success" type="json"> <param name="root"></param> </result></action>
4.结果截图
data:$('#form').serialize(),