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 ...
随机推荐
- 展开、收起div的jQuery代码
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Cdnbes负载均衡的权重用法解释
(1)相同域名添加两条记录,解析不同的ip,可以设置权重,比如权重2,就意思占百分之20 ,数字越大,优先级越大 (2)这个hash 如果用户访问的源是挂掉的.会去第二个源
- ubuntu 安装mysql-python和 python-ldap,navicate 问题
1.Ubuntu 下 pip install mysql-python 报错 EnvironmentError: mysql_config not found 原因是缺少mysqlclient 包,执 ...
- java实现简单的验证码(待增强)
package com.xxx; import java.awt.Color;import java.awt.Font;import java.awt.Graphics2D;import java.a ...
- mac系统,git上刚刚checkout出来的文件,一检查,发现已经被修改过了,怎么破???
如下图中所示: 事实上,checkout之后什么都还没做,这些文件为何就被修改? 检查一下别的电脑上所存放的同一套源码,原来出问题的文件都是同名文件,只不过是有大小写区分而已!!! linux系统可以 ...
- CocosCreator反射在Android中的使用
CocosCreator反射在Android中的使用 新建一个CocosCreator项目,然后点击构建 构建完成之后,即可用AndroidStudio打开构建的项目 使用AndroidStudio打 ...
- nginx支持pathinfo并且隐藏index.php
How To Set Nginx Support PATHINFO URL Model And Hide The /index.php/ 就像这样 The URL before setting lik ...
- POJ2186
poj2186 popular cows Every cow's dream is to become the most popular cow in the herd. In a herd of ...
- dojo创建tree
今天介绍dojo目录树的效果,效果如下图: HTML代码如下: <body class="claro"> <div id="rootlessTree&q ...
- 16.10.16学到的JAVA知识
1. 每个字节就是八位,所以每个字节的取值范围是 -128~127,它可以保存一个英文字符,包括字母,数字和英文标点.而汉字的的数量很多,一个字节没法把所有的汉字表达出来,所以汉字就是用两个字节来存 ...