我的開發環境
框架:        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的更多相关文章

  1. SpringMVC常用注解實例詳解2:@ModelAttribute

    我的開發環境框架:        springmvc+spring+freemarker開發工具: springsource-tool-suite-2.9.0JDK版本: 1.6.0_29tomcat ...

  2. SpringMVC常用注解實例詳解1:@Controller,@RequestMapping,@RequestParam,@PathVariable

    我的開發環境 框架:        springmvc+spring+freemarker 開發工具: springsource-tool-suite-2.9.0 JDK版本: 1.6.0_29 to ...

  3. SpringMVC 常用注解 详解

    SpringMVC 常用注解 详解 SpringMVC 常用注解 1.@RequestMapping                                      路径映射 2.@Requ ...

  4. 转:springmvc常用注解标签详解

    Spring5:@Autowired注解.@Resource注解和@Service注解 - IT·达人 - 博客园--这篇顺序渐进,讲得超级好--此人博客很不错http://www.cnblogs.c ...

  5. springmvc常用注解与类型转换

    springmvc常用注解与类型转换 一:前置 spring -servlet.xml 注入 <!-- 启用spring mvc 注解 --> <context:annotation ...

  6. SpringMVC常用注解@Controller,@Service,@repository,@Component

    SpringMVC常用注解@Controller,@Service,@repository,@Component controller层使用@controller注解 @Controller 用于标记 ...

  7. 一 : springmvc常用注解

    springmvc常用注解详解1.@Controller在SpringMVC 中,控制器Controller 负责处理由DispatcherServlet 分发的请求,它把用户请求的数据经过业务处理层 ...

  8. Spring 和 SpringMVC 常用注解和配置(@Autowired、@Resource、@Component、@Repository、@Service、@Controller的区别)

    Spring 常用注解 总结内容 一.Spring部分 1.声明bean的注解 2.注入bean的注解 3.java配置类相关注解 4.切面(AOP)相关注解 5.事务注解 6.@Bean的属性支持 ...

  9. SpringMVC常用注解,返回方式,路径匹配形式,验证

    常用注解元素 @Controller 标注在Bean的类定义处 @RequestMapping 真正让Bean具备 Spring MVC Controller 功能的是 @RequestMapping ...

随机推荐

  1. 邮箱验证 各种邮箱的smtp

    常见邮箱的SMTP设置 QQ 邮箱举例:(地址test@qq.com)(账号test)(密码***)(SMTP服务smtp.qq.com)(端口25)(注意:请手动开通SMTP功能,通过网页登录qq邮 ...

  2. NLua - 基于Lua的C#脚本引擎

    Nlua NLua is the bind between Lua world and the .NET world. NLua is a fork of project LuaInterface ( ...

  3. 对dijkstra算法的自我理解,c#例子

    dijkstra该算法主要应用在求解最短路径,从最近点开始,广度搜索. 假设有向图中有10个顶点,求其中某个顶点a到其它顶点的最短路径..满足贪心算法的2个标准.时间复杂度为O(N2) 此问题可以进行 ...

  4. Spring+quartz 实现定时任务job集群配置

    为什么要有集群定时任务? 因为如果多server都触发相同任务,又同时执行,那在99%的场景都是不适合的.比如银行每晚24:00都要汇总营业额.像下面3台server同时进行汇总,最终计算结果可能是真 ...

  5. Mediator(中介者)-对象行为型模式

    1.意图 用一个中介对象来封装一系列的对象交互.中介者使各个对象不需要显示地相互引用,从而使其耦合松散,而且可以独立地改变它们之间的交互. 2.动机 通过将集体行为封装在一个单独的中介者对象中,中介者 ...

  6. Appium移动自动化测试之Eclipse

    下载eclipse,这个下载方式比较多,eclipse官网,CSDN都有的下,版本根据自己操作系统选择,切记eclipse版本一定要与JDK版本一至,不然eclipse无法启动.现在我们来搭建Andr ...

  7. chVsprintf

    #if LINUX_SYSTEM int chVsprintf(LPSTR buffer, int nCount, LPCSTR format, va_list argptr) { return vs ...

  8. 使用merge同时执行insert和update操作

    SQL点滴18—SqlServer中的merge操作,相当地风骚   今天在一个存储过程中看见了merge这个关键字,第一个想法是,这个是配置管理中的概念吗,把相邻两次的更改合并到一起.后来在tech ...

  9. 记录容易忘记的知识点(html 内容)

    <xx 表文件名> 导入外部样式表 <link type="text/css" rel="stylesheet" href="xx. ...

  10. new(C# 参考)

    在 C# 中,new 关键字可用作运算符.修饰符或约束. new 运算符 用于创建对象和调用构造函数. new 修饰符 用于隐藏基类中被继承的成员. new 约束 用于在泛型声明中约束可能用作类型参数 ...