1、springmv依赖jackson的pom如下
<!-- json -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-lgpl</artifactId>
<version>1.9.2</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-lgpl</artifactId>
<version>1.9.2</version>
</dependency>
2、js
function doTestJson(ctx){
$.ajax({
url:ctx+'/project/ajax/save_project',
type: 'POST',
data: $("#proForm").serialize(),
dataType: 'json',
contentType:'application/json;charset=UTF-8',
success: function(result) {
alert(result.result);
}
});
}
@RequestMapping(value= "/project/ajax/save_project")
public @ResponseBody JsonResult saveProject(HttpServletRequest req,@ModelAttribute("pro")Project pro){
/*pro.setCreator((User) SecurityUtils.getSubject().getSession().getAttribute(Constants.CURRENT_USER));
* */
//判断必填
String name = req.getParameter("projectType");
projectService.add(pro);
System.out.println(pro);
return new JsonResult(false,"失败");
}
4、对象属性
project对象中存在日期类型属性,前段用form提交到后台后,会提示400错误(请求异常),springmvc对string转换成Date类型可能需要自己实现。把Date字段改成String类型就可以正常接收参数了。