SpringMVC常用注解實例詳解3:@ResponseBody
我的開發環境
框架: springmvc+spring+freemarker
開發工具: springsource-tool-suite-2.9.0
JDK版本: 1.6.0_29
tomcat版本:apache-tomcat-7.0.26
前置文章-SpirngMVC配置入門 http://www.cnblogs.com/sunang/p/3419544.html
Spring整合Freemarker http://www.cnblogs.com/sunang/p/3419676.html
@ResponseBody用于在controller方法中直接返回一個數據對象,常用于Ajax交互中,本文用Ajax交互的例子來演示下該註釋的用法。
step1.由於Ajax傳輸數據用到JSON,所以要先添加JSON依賴如下:

Maven代碼如下:
<!-- JSON -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-core-asl</artifactId>
<version>1.8.4</version>
</dependency>
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-mapper-asl</artifactId>
<version>1.8.4</version>
</dependency>
在spring配置文件中加入JSON所需配置,此處以spring-servlet.xml為例,代碼如下:
<!-- JSON所需配置 -->
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="mappingJacksonHttpMessageConverter" />
</list>
</property>
</bean>
<bean id="mappingJacksonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>
step2.編寫頁面ajaxGetMsg.ftl,代碼如下:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<!-- 引入jquery文件-->
<script type="text/javascript" src="../js/jquery.js"></script>
<title>Insert title here</title>
</head>
<body>
<span id="content">
<!-- 點擊按鈕后調用getMsg()方法-->
<button type="button" onclick="javascript:getMsg();">@ResponseBody結合Ajax例子演示</button>
</span>
</body>
</html>
<script type="text/javascript">
function getMsg(){
var content = "";
//ajax訪問controller方法,利用@ResponseBody返回數據對象
$.ajax({
async:false,
cache : false,
type : 'POST',
dataType : "json",
url:"ajaxGetMsg.htm",
success:function(data){
$.each(data, function(i,obj){
content=content+obj;
});
$("#content").html(content);
},
error:function(){
alert("加载失败");
return;
}
});
}
</script>
step3.編寫controller方法,代碼如下:
package www.asuan.com.controller; import java.util.ArrayList;
import java.util.List; import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; @Controller
@RequestMapping("/learnMVC")
public class LearnMVCController {
//前往初始頁面
@RequestMapping("/indexPage")
public String indexPage(){
return "ajaxGetMsg.ftl";
}
//Ajax交互方法
@RequestMapping("/ajaxGetMsg")
@ResponseBody
public List<String> ajaxGetMsg() {
List<String> strList = new ArrayList<String>();
strList.add("學");
strList.add("習");
strList.add("Spring");
strList.add("M");
strList.add("V");
strList.add("C");
return strList;
}
}
indexPage()方法用於訪問初始頁面,ajaxGetMsg()方法上加了@ResponseBody註釋,所以該方法可以直接向頁面返回數據對象,該方法的返回數據類型為List<String>.
step4.運行調試
部署運行項目,瀏覽器訪問:http://localhost:8080/你的工程名/learnMVC/indexPage.htm
運行結果如下:

點擊按鈕,ajax加載數據得到如下結果:

complete!
系列文章鏈接:
SpringMVC常用注解實例詳解:@Controller,@RequestMapping,@RequestParam,@PathVariable http://www.cnblogs.com/sunang/p/3421707.html
SpringMVC常用注解實例詳解:@ModelAttribute http://www.cnblogs.com/sunang/p/3423227.html
SpringMVC常用注解實例詳解3:@ResponseBody的更多相关文章
- SpringMVC常用注解實例詳解2:@ModelAttribute
我的開發環境框架: springmvc+spring+freemarker開發工具: springsource-tool-suite-2.9.0JDK版本: 1.6.0_29tomcat ...
- SpringMVC常用注解實例詳解1:@Controller,@RequestMapping,@RequestParam,@PathVariable
我的開發環境 框架: springmvc+spring+freemarker 開發工具: springsource-tool-suite-2.9.0 JDK版本: 1.6.0_29 to ...
- SpringMVC 常用注解 详解
SpringMVC 常用注解 详解 SpringMVC 常用注解 1.@RequestMapping 路径映射 2.@Requ ...
- 转:springmvc常用注解标签详解
Spring5:@Autowired注解.@Resource注解和@Service注解 - IT·达人 - 博客园--这篇顺序渐进,讲得超级好--此人博客很不错http://www.cnblogs.c ...
- springmvc常用注解与类型转换
springmvc常用注解与类型转换 一:前置 spring -servlet.xml 注入 <!-- 启用spring mvc 注解 --> <context:annotation ...
- SpringMVC常用注解@Controller,@Service,@repository,@Component
SpringMVC常用注解@Controller,@Service,@repository,@Component controller层使用@controller注解 @Controller 用于标记 ...
- 一 : springmvc常用注解
springmvc常用注解详解1.@Controller在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层 ...
- Spring 和 SpringMVC 常用注解和配置(@Autowired、@Resource、@Component、@Repository、@Service、@Controller的区别)
Spring 常用注解 总结内容 一.Spring部分 1.声明bean的注解 2.注入bean的注解 3.java配置类相关注解 4.切面(AOP)相关注解 5.事务注解 6.@Bean的属性支持 ...
- SpringMVC常用注解,返回方式,路径匹配形式,验证
常用注解元素 @Controller 标注在Bean的类定义处 @RequestMapping 真正让Bean具备 Spring MVC Controller 功能的是 @RequestMapping ...
随机推荐
- Generics and Collection (2)
Integer is a subtype of NumberDouble is a subtype of NumberArrayList<E> is a subtype of List&l ...
- iOS支持Https
http://oncenote.com/2014/10/21/Security-1-HTTPS/?hmsr=toutiao.io&utm_medium=toutiao.io&utm_s ...
- Exception Type & Exception Code
1.Exception Type 1)EXC_BAD_ACCESS 此类型的Excpetion是我们最长碰到的Crash,通常用于访问了不改访问的内存导致.一般EXC_BAD_ACCESS后面的&qu ...
- ibatis map
<select id="selectBank2" parameterClass="java.util.Map" resultClass="jav ...
- Promise.race
[Promise.race] 返回最先完成的promise var p1 = new Promise(function(resolve, reject) { setTimeout(resolve, 5 ...
- vim的共享系统剪贴板以及缩进相关问题
http://www.cnblogs.com/end/archive/2012/06/01/2531147.html:reg 可以显示可用的寄存器,其中注意两个特殊的寄存器:"* 和 &qu ...
- js常用正则
var sTest="xxxkdsj234dogdog1234xx"var reTest1=/(dog){2}/var reTest2 = /(?:dog){2}/;console ...
- [转载] python 计算字符串长度
本文转载自: http://www.sharejs.com/codes/python/4843 python 计算字符串长度,一个中文算两个字符,先转换成utf8,然后通过计算utf8的长度和len函 ...
- Android开发学习---android下的数据持久化,保存数据到rom文件,android_data目录下文件访问的权限控制
一.需求 做一个类似QQ登录似的app,将数据写到ROM文件里,并对数据进行回显. 二.截图 登录界面: 文件浏览器,查看文件的保存路径:/data/data/com.amos.datasave/fi ...
- JS产生随机数
<script> function GetRandomNum(Min,Max){ var Range = Max - Min; var Rand = Math.random() ...