SpringMVC:ajax请求处理
2017-06-21 11:56
447 查看
相关jar包(jackson和fastjson)下载
返回类型为字符串时
Controller请求参数不使用@RequestBody注释
public class Comment implements Serializable { private Long id; private String details; //...省略getter和setter方法 }
js代码段:
$.ajax({ url:"add.html", data:{"details":"内容"}, type:"POST", dataType:"json", success:function(data){ alert(data); } });
Controller代码段:
//添加ResponseBody注释 @RequestMapping("/add") @ResponseBody public String addComment(Comment comment){ System.out.println(comment.getDetails()); comment.setUser(new User(1L)); commentMapper.addComment(comment); return comment.getId().toString(); }
请求参数添加@RequestBody注释
所需jar包
jackson-annotations-xxx.jar、jackson-core-xxx.jar、jackson-databind-xxx.jar
js代码段:
$.ajax({ url:"add.html", data:JSON.stringify({"details":$(".comment-textarea").val()}), type:"POST", dataType:"json", contentType:"application/json", success:function(data){ alert(JSON.stringify(data)); } });
Controller代码段:
@RequestMapping(value="/add", method = RequestMethod.POST) public @ResponseBody String addComment(@RequestBody Comment comment){ comment.setUser(new User(1L)); commentMapper.addComment(comment); return comment.getId().toString(); }
spring-servlet.xml配置
<!--头部--> <?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:p="http://www.springframework.org/schema/p" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-4.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-4.2.xsd"> <!--注意加入--> <mvc:annotation-driven></mvc:annotation-driven> <!--省略其他-->
返回类型为对象时
请求参数不使用@RequestBody注释
所需jar包
jackson-annotations-xxx.jar、jackson-core-xxx.jar、jackson-databind-xxx.jar
js代码段:
$.ajax({ url:"add.html", data:{"details":"内容"}, type:"POST", dataType:"json", success:function(data){ alert(JSON.stringify(data)); } });
Controller代码段:
@RequestMapping(value="/add", method = RequestMethod.POST) public @ResponseBody Comment addComment( Comment comment){ comment.setUser(new User(1L)); commentMapper.addComment(comment); return comment; }
springmvc-servlet.xml配置:
<mvc:annotation-driven> <mvc:message-converters register-defaults="false"> <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
请求参数添加@RequestBody注释
添加jar包:
fastjson-xxx.jar
js代码片段:
$.ajax({ url:"add.html", data:JSON.stringify({"details":"内容"}), type:"POST", dataType:"json", contentType:"application/json", success:function(data){ alert(JSON.stringify(data)); } });
Controller代码片段:
@RequestMapping(value="/add", method = RequestMethod.POST) public @ResponseBody Comment addComment(@RequestBody Comment comment){ comment.setUser(new User(1L)); commentMapper.addComment(comment); return comment; }
springmvc-servlet.xml修改为:
<mvc:annotation-driven> <mvc:message-converters register-defaults="false"> <bean id="fastJsonHttpMessageConverter" class="com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter"> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean> </mvc:message-converters> </mvc:annotation-driven>
相关文章推荐
- SpringMVC3.2 处理Ajax请求时返回json字符串的中文乱码问题
- Ajax响应中文乱码 [SpringMVC使用@ResponseBody处理Ajax请求]
- springMVC处理ajax请求时的404
- springmvc处理ajax请求
- springmvc拦截器针对ajax()请求的带特殊请求头的处理
- Ajax响应中文乱码 [SpringMVC使用@ResponseBody处理Ajax请求]
- SpringMVC处理Ajax请求(请求数据为 json 格式字符串)
- springmvc处理ajax请求
- springmvc处理ajax请求
- springMVC拦截器处理ajax请求及数据返回
- SpringMVC处理ajax请求
- SpringMVC处理AJAX请求
- springMVC处理Ajax请求的一个错误
- springmvc处理ajax请求
- Ajax响应中文乱码 [SpringMVC使用@ResponseBody处理Ajax请求]
- SpringMVC对异常进行全局处理,并区分对待ajax和普通请求
- Ajax响应中文乱码 [SpringMVC使用@ResponseBody处理Ajax请求]
- SpringMVC经典系列-13使用SpringMVC处理Ajax请求---【LinusZhu】
- Problem: springmvc 处理Ajax请求的controller中跳转语句无效
- SpringMVC处理普通请求异常以及,ajax异常