版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
1. new.html: 编辑页面:
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>编辑</title>
</head>
<body>
<form id="saveForm">
名字:<input type="text" name="name"><br>
排名:<input type="text" name="seq"><br>
分数:<input type="text" name="score"><br>
级别:<input type="text" name="level"><br>
备注:<input type="text" name="remark"><br>
<button type="submit" id="btn_save">保存</button>
</form>
<th:block th:replace="common/include"></th:block>
<th:block th:replace="common/link"></th:block>
<th:block th:replace="js/save_js"></th:block>
</body>
</html>
2.save_js
<script type="text/javascript">
var Save = function(){
return {
init: function () {
alert("in.....")
Save.initSubmit();
},
initSubmit: function () {
var defaultParam = {
formId: "saveForm"
}
// var param = $.extends({},defaultParam);
var param = $.extend({},defaultParam);
// debugger;
alert("...." + defaultParam.formId)
$('#' + defaultParam.formId).submit(function () {
alert('.....');
Save.getSaveData(defaultParam);
});
}
,
/**
* 从form中获取数据
*/
getSaveData: function (curParam) {
// var info = $('#' + curParam.formId).serializeArray();
// //info:两个Object
// var d = {};
// //循环
// $.each(info, function () {
// d[this.name] = this.value;//this代表当前循环的object 第一次是[username:ddd]
// })
var info = $('#' + curParam.formId).serializeObject();
Save.sendDataToController(info);
},
/**
* 用Ajax
* 将数据(param)发送到Controller
* @param param : 需要提交的数据
*/
sendDataToController: function (param) {
alert('......sendDataToController');
$.ajax({
url:"/save.json",
contentType: "application/json;charset=UTF-8",
type: "post",
data: JSON.stringify(param),
success: function (resp) {
console.log('....resp:'+ JSON.stringify(resp) + 'code:' + resp.resultCode);
if(resp.success) {
alert(".....提交成功" + resp.resultCode);
location.href="/school.html";
// resp.result;
} else {
alert("密码错误!!")
}
}
});
}
}
}();
/**
* 页面加载完成后执行
*/
$(document).ready(function () {
Save.init();
});
/**
* 封装从this中取数据的功能
* this:代表调用此方法的那个标签(newForm)
*/
$.fn.serializeObject = function () {
var o = {};//放数据用的
var a = this.serializeArray();
$.each(a,function () {
if(o[this.name]) {
if(!o[this.name].push) {
o[this.name] = this.value;
}
o[this.name].push(this.value || '')
} else {
o[this.name] = (this.value || '')
}
});
return o;
}
</script>
3.Controller:
package com.zpark.neimin.first.controller;
import com.zpark.neimin.first.common.BaseModelExample;
import com.zpark.neimin.first.common.BootstrapTable;
import com.zpark.neimin.first.common.QueryParam;
import com.zpark.neimin.first.model.School;
import com.zpark.neimin.first.service.SchoolService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import java.util.List;
@Controller
public class SchoolController {
private int myId;
@Autowired
SchoolService schoolService;
@RequestMapping(value = "/getSchoolData")
@ResponseBody
public BootstrapTable getSchoolData(@RequestBody(required = false) QueryParam queryParam) {
System.out.println("....queryParam:" + queryParam);
int index = queryParam.getPageNumber();
int pageSize = queryParam.getPageSize();
BaseModelExample baseModelExample = new BaseModelExample();
baseModelExample.setRowIndex(index);
baseModelExample.setPageSize(pageSize);
List<School> schoolData = schoolService.getSchoolData(baseModelExample);
return new BootstrapTable(60, schoolData);
}
@RequestMapping("/school")
public String school() {
return "school";
}
@RequestMapping("/{id}/new") //{id} 占位符
public String newEidt(@PathVariable int id) {
System.out.println(".....id:" + id);
myId = id;
return "new";
}
@RequestMapping("/save.json")
@ResponseBody
public School save(@RequestBody School schoolInfo) {
System.out.println(".....scInfo:" + schoolInfo);
schoolInfo.setId(myId);
schoolService.updateSchool(schoolInfo);
return null;
}
}
4.SchoolService:
public void updateSchool(School schoolInfo) {
schoolMapper.updateSchool(schoolInfo);
}
5.SchoolMapper.java
void updateSchool(School schoolInfo);
6. SchoolMapper.xml
<update id="updateSchool" parameterType="com.zpark.neimin.first.model.School" >
update school set school.name=#{name}, seq = #{seq}, score = #{score} where school.id = #{id};
</update>