首页 > IT职业 > IT职场 > Ajax与SpringMVC交互详解
Ajax的使用是很经常的,最近在写自己的博客系统的时候也用到了,在这总结一下用法,有完整例子
本文章统一用index.jsp页面

java.lang.NoSuchMethodError: com.fasterxml异常,说明Spring版本与json版本不兼容,解决方案:提高json版本。 新建index.jsp(在文章开头)
UserModel
package com.entity;
public class UserModel {
private String username;
private int age;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
package com.controller;
import java.util.*;
import java.util.HashMap;
import java.util.Map;
import org.hibernate.annotations.common.util.impl.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.entity.UserModel;
@Controller
@RequestMapping("/user")
public class UserController {
private org.jboss.logging.Logger logger = LoggerFactory.logger(UserController.class);
@RequestMapping(value = "/list", method = RequestMethod.GET)
@ResponseBody //必须要有
public Map getUserList() {
logger.info("获取列表成功");
List list = new ArrayList();
UserModel um = new UserModel();
um.setUsername("hope");
um.setAge(20);
list.add(um);
Map modelMap = new HashMap(3);
modelMap.put("total", "1");
modelMap.put("data", list);
modelMap.put("success", "true");
return modelMap;
}
@RequestMapping(value = "/add", method = RequestMethod.POST)
@ResponseBody //必须要有
public Map addUser(@RequestBody UserModel model) {
logger.info("新增model");
logger.info("捕获到前台Model:" + model.getUsername());
Map map = new HashMap(1);
map.put("success", "true");
return map;
}
}
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/2976/viewspace-2798733/ ,如需转载,请注明出处,否则将追究法律责任。