您的位置:首页 > 编程语言 > Java开发

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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  spring mvc ajax json