js code
$("#saveFamilySituationBtn").click(function(){
var isSuccess = false;
//创建guardian数组
var guardianList = [];
//遍历guardian div,找出非完全的guardian div
$("div[name='guardian']").each(function(index){
//设立非空guardian div flag
var flag = false;
$(this).find("input").each(function(){
if($(this).attr("type") == "checkbox"){
if($(this).attr("checked")){
flag = true;
return false;
}
}else{
if($(this).val().trim() != ""){
flag = true;
return false;
}
}
});
if(flag){
//创建guardian对象
var guardian = {};
index += 1;
guardian.name = $("#guardianName"+index).val().trim();
guardian.relationship = $("#guardianRelationship"+index).val().trim();
if($("#liveWith"+index).attr("checked")){
guardian.liveWith = true;
}else{
guardian.liveWith = false;
}
guardian.id = $("#guardianId"+index).val().trim();
guardian.address = $("#address"+index).val().trim();
guardian.apartment = $("#apartment"+index).val().trim();
guardian.postalCode = $("#postCode"+index).val().trim();
guardian.homePhone = $("#homePhone"+index).val().trim();
guardian.mobilePhone = $("#mobilePhone"+index).val().trim();
guardian.workPhone = $("#workPhone"+index).val().trim();
guardian.email = $("#email"+index).val().trim();
if(guardian.name == null || guardian.name==""){
common.showMessage("Please type guardian name first!");
return false;
}
if(guardian.mobilePhone == null || guardian.mobilePhone==""){
common.showMessage("Please type mobile phone first!");
return false;
}
guardianList.push(guardian);
}
});
//若guardian 数组有值,则提交ajax请求
if(guardianList.length != 0){
$.ajax({
type: "post",
dataType: "json",
traditional:true,
async : false,
data: JSON.stringify(guardianList),
url: "${maintainGuardianURL}"+$("#id").val(),
headers : {
'Accept' : 'application/json',
'Content-Type' : 'application/json'
},
success: function (data, textStatus) {
if (!data.result) {
isSuccess = false;
}else{
isSuccess = true;
}
},
error: function (e) {
isSuccess = false;
}
});
}
var siblingList = [];
$("div[name='sibling']").each(function(index){
var flag = false;
$(this).find("input").each(function(){
if($(this).attr("type") != "checkbox"){
if($(this).val().trim() != ""){
flag = true;
return false;
}
}
});
if(flag){
var sibling = {};
index += 1;
sibling.kidId = $("#kidId"+index).val().trim();
sibling.kidName = $("#siblingName"+index).val().trim();
sibling.relationship = $("#relationship"+index).val().trim();
if($("#enrolled"+index).attr("checked")){
sibling.enrolled = true;
}else{
sibling.enrolled = false;
}
siblingList.push(sibling);
}
});
if(siblingList.length != 0){
$.ajax({
type: "post",
dataType: "json",
traditional:true,
async : false,
data: JSON.stringify(siblingList),
url: "${maintainSiblingURL}"+$("#id").val(),
headers : {
'Accept' : 'application/json',
'Content-Type' : 'application/json'
},
success: function (data, textStatus) {
if (!data.result) {
isSuccess = false;
}else{
isSuccess = true;
}
},
error: function (e) {
isSuccess = false;
}
});
}
if(isSuccess){
common.showMessage("save success!");
}else{
common.showMessage("save failed, please try again!");
}
});
server code
1.list
@RequestMapping(value = "/familySituation/guardian/maintain/{kidId}", produces = "application/json;charset=UTF-8")
@ResponseBody
public ResultResponse maintainGuardianBatch(@PathVariable("kidId") Long kidId,
@RequestBody List<AppGuardianVO> guardianList) {
ResultResponse result = new ResultResponse();
if (null != guardianList) {
try {
kidEditSerivce.maintainGuardianBatch(guardianList, kidId);
result.setResult(true);
} catch (Exception e) {
result.setResult(false);
result.setMessage("修改出异常,请重试!");
}
}
return result;
}
2.array
@RequestMapping(value = "/familySituation/sibling/maintain/{kidId}", produces = "application/json;charset=UTF-8")
@ResponseBody
public ResultResponse maintainSiblingBatch(@PathVariable("kidId") Long kidId,
@RequestBody AppSiblingVO[] siblingArray) {
ResultResponse result = new ResultResponse();
List<AppSiblingVO> siblingVOList = null;
if (null != siblingArray) {
siblingVOList = Arrays.asList(siblingArray);
try {
kidEditSerivce.maintainSiblingBatch(siblingVOList, kidId);
result.setResult(true);
} catch (Exception e) {
result.setResult(false);
result.setMessage("修改出异常,请重试!");
}
}
return result;
}
- 描述: UI IMAGE
- 大小: 767.1 KB
分享到:
相关推荐
通过正确配置Ajax请求和服务器接口,你可以轻松地传递和处理List、Map等复杂数据结构,提升Web应用的交互体验。在实际项目中,确保对错误处理和安全性有充分考虑,这将有助于构建健壮和安全的Web应用。
然而,实际应用中,XML已经不是数据交换的主要格式,JSON因其简洁、易于解析的特性,已经成为Ajax请求和响应中的首选数据格式。 JSON是一种轻量级的数据交换格式,它允许我们用接近自然语言的方式来表示数据。在...
It allows you to locate individual items in a space-separated attribute (such as a class or rel attribute).<br><br>$("a[@rel~=test]")Animation Improvements<br><br>参数: <br>options <br>返回值: <br>...
items = gson.fromJson(jsonString, new TypeToken<List<MyObject>>(){}.getType()); // ...其他业务逻辑... return SUCCESS; } } ``` 在这里,`MyObject`是你定义的一个Java类,其属性应与JSON对象的键相对应...
首先定义一个 VO (Value Object) 类,其中包含 `List<String>` 类型的属性。 **VO 定义示例**: ```java public class ParamVo { private List<String> areaList; public List<String> getAreaList() { return ...
- AJAX(Asynchronous JavaScript and XML)异步请求,更新部分页面内容无需刷新整个页面。 通过这个“仿淘宝首页html”项目,你可以学习到如何构建一个具有专业外观的网页,并掌握一系列前端开发的基础知识。无论...
List<Map<String, Object>> list = new ArrayList<>(); // ... (省略具体实现) Gson gson = new Gson(); return gson.toJson(list); } ``` #### 四、总结 本教程通过具体的代码实例详细介绍了如何使用 ...
List<User> users = new ArrayList<>(); User user1 = new User(1, "张三"); User user2 = new User(2, "李四"); users.add(user1); users.add(user2); return users; } } class User { private int id; ...
List<Person> personList = new ArrayList<>(); try { personList = JSONUtil.jsonToObject(jsonPersons, new TypeReference<List<Person>>(){}); } catch (Exception e) { e.printStackTrace(); } // 进行...
public Map<String, Object> handleAjaxRequest(@RequestParam Map<String, String> params) { // 处理业务逻辑 List<Movie> movies = movieDao.getMovies(params); return Collections.singletonMap("movies", ...
<list> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" /> </list> </property> </bean> ``` 3. **创建Controller**: 创建一个Spring MVC Controller,定义...
items = new ArrayList<>(); items.add("Item 1"); items.add("Item 2"); items.add("Item 3"); return "success"; } } ``` 然后,在Struts配置文件中定义这个Action,指定返回的视图是JSON类型: ```xml ...
List<DataObject> dataList = new ArrayList<>(); // 数据库连接、查询、获取结果集等操作 // ... // 将查询结果转换为List // ... // 设置响应内容类型为JSON response.setContentType("application/json");...
List<Map<String, Object>> data = new ArrayList<>(); Map<String, Object> item1 = new HashMap<>(); item1.put("name", "类别1"); item1.put("value", 50); data.add(item1); Map<String, Object> item2 =...
public List<Object> getListOfData() { // 查询数据库并返回list } } ``` 在前端,我们可以使用AJAX来异步调用这个Java方法,获取数据,然后利用DOM操作动态更新表格。例如,使用jQuery库可以这样实现: ```...
List<String> dataList = new ArrayList<>(); // 添加数据到list... // 转换为JSON格式 JSONObject jsonObject = new JSONObject(); jsonObject.put("data", dataList); %> <%=jsonObject.toString()%> ``` ...
List<MyObject> dataList = myService.getData(); return dataList; } } ``` 这里的`MyObject`是自定义的Java类,它代表了下拉框中的每一项数据。确保这个类有合适的getter和setter方法,以便JSON序列化。 在...
public ResponseEntity<List<MyObject>> getData(@RequestParam Optional<Integer> pageSize, Optional<Integer> pageNumber) { // 查询数据库,分页获取数据 List<MyObject> dataList = repository.getData...
List<string[]> data = new List<string[]> { ... }; // 假设这是从数据库获取的数据 string tableHtml = "<table>"; foreach (string[] row in data) { tableHtml += "<tr>"; foreach (string cell in row) ...