关于REST是什么东西,在这里我就不再多说,大家可以去http://blog.csdn.net/pilou5400/archive/2010/12/24/6096861.aspx看看介绍,直接切入主题:

这是一个rest风格的访问,Spring从3.0开始将全面支持rest。不得不感叹Spring的强悍。

项目结构:

第一步永远是配置,使用框架永远都是先有配置,在web.xml中的配置:

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="3.0"
  3. xmlns="http://java.sun.com/xml/ns/javaee"
  4. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  5. xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
  6. http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
  7. <display-name></display-name>
  8. <context-param>
  9. <!--rest配置文件的路径,貌似不配置也是加载这个地址,这个地方有点疑问,大家指点指点-->
  10. <param-name>contextConfigLocation</param-name>
  11. <param-value>/WEB-INF/rest-servlet.xml</param-value>
  12. </context-param>
  13. <listener>
  14. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  15. </listener>
  16. <servlet>
  17. <!-- 配置一个Servlet,有这个Servlet统一调度页面的请求 -->
  18. <servlet-name>rest</servlet-name>
  19. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  20. <load-on-startup>2</load-on-startup>
  21. </servlet>
  22. <servlet-mapping>
  23. <!-- 映射路径,不要写成了/*那样会拦截所有的访问,连JSP页面都访问不了 -->
  24. <servlet-name>rest</servlet-name>
  25. <url-pattern>/</url-pattern>
  26. </servlet-mapping>
  27. <welcome-file-list>
  28. <welcome-file>/index.jsp</welcome-file>
  29. </welcome-file-list>
  30. </web-app>

第二步:配置rest-servlet.xml这个文件

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:jee="http://www.springframework.org/schema/jee"
  4. xmlns:tx="http://www.springframework.org/schema/tx" xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-2.5.xsd http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-2.5.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-2.5.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd"
  7. default-lazy-init="true">
  8. <description>Spring公共配置</description>
  9. <!--检测注解-->
  10. <context:component-scan base-package="com.liqiu" />
  11. <bean   class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />
  12. <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />
  13. <!-- 注册视图解析器,说白了就是根据返回值指定到某个页面 -->
  14. <bean id="viewResolver"   class="org.springframework.web.servlet.view.InternalResourceViewResolver">
  15. <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />
  16. <property name="prefix" value="/"></property> <!--页面文件的路径,在根目录下-->
  17. </bean>
  18. </beans>

第三步:具体实现类

  1. package com.liqiu.controller;
  2. import java.io.IOException;
  3. import javax.servlet.http.HttpServletRequest;
  4. import javax.servlet.http.HttpServletResponse;
  5. import org.springframework.stereotype.Controller;
  6. import org.springframework.web.bind.annotation.PathVariable;
  7. import org.springframework.web.bind.annotation.RequestMapping;
  8. import org.springframework.web.bind.annotation.RequestMethod;
  9. @Controller
  10. @RequestMapping("/simple")
  11. public class SimpleController {
  12. //映射路径/simple/index当访问这个路径时,执行这个方法
  13. @RequestMapping("/index")
  14. public String index(HttpServletRequest request ,HttpServletResponse response){
  15. //response,request会自动传进来
  16. request.setAttribute("message", "Hello,This is a example of Spring3 RESTful!");
  17. return "index.jsp";
  18. }
  19. //根据ID获取不同的内容,通过@PathVariable 获得属性
  20. @RequestMapping(value="/{id}",method=RequestMethod.GET)
  21. public String get(@PathVariable String id,HttpServletRequest request ,HttpServletResponse response) throws IOException{
  22. request.setAttribute("message", "Hello,This is a example of Spring3 RESTful!<br/>ID:"+id+"");
  23. //response.getWriter().write("You put id is : "+id);
  24. return "index.jsp";
  25. //return null;
  26. }
  27. }

index.jsp页面:

  1. <%@ page language="java" pageEncoding="UTF-8"%>
  2. <html>
  3. <head>
  4. <title>Spring3 RESTful</title>
  5. </head>
  6. <body>
  7. ${message}
  8. </body>
  9. </html>

在浏览器中输入:http://localhost:8080/SpringREST/simple/index/,就可以看到效果。

也可以在页面输入不同的参数,获得不同的内容,输入地址:http://localhost:8080/SpringREST/simple/88888,这次执行的就是get方法,通过注解获取ID值,效果:

关于Spring rest 对于Ajax的支持,其实响应Ajax就是通过response返回一个字符串到页面,既然能获得response对象,那问题就迎刃而解了,我们改造下get方法:

  1. @RequestMapping(value="/{id}",method=RequestMethod.GET)
  2. public String get(@PathVariable String id,HttpServletRequest request ,HttpServletResponse response) throws IOException{
  3. //request.setAttribute("message", "Hello,This is a example of Spring3 RESTful!<br/>ID:"+id+"");
  4. response.getWriter().write("You put id is : "+id);
  5. //return "index.jsp";
  6. return null;
  7. }

改造index.jsp页面:

  1. <%@ page language="java" pageEncoding="UTF-8"%>
  2. <html>
  3. <head>
  4. <title>Spring3 RESTful</title>
  5. <SCRIPT TYPE="text/javascript">
  6. function go(value){
  7. var url = "/SpringREST/simple/"+value+"/";
  8. var request =  new XMLHttpRequest();
  9. request.open("GET", url, true);
  10. request.setRequestHeader("Content-Type","application/x-javascript;");
  11. request.onreadystatechange = function() {
  12. if (request.readyState == 4) {
  13. if (request.status == 200){
  14. if (request.responseText) {
  15. document.getElementById("text").innerHTML = request.responseText;
  16. }
  17. }
  18. }
  19. };
  20. request.send(null);
  21. }
  22. </SCRIPT>
  23. </head>
  24. <body>
  25. ${message}
  26. <br>
  27. Input the id of you will access object:<input id="id" type="text" size="7"><input type="button" value="Go" onclick="go(document.getElementById('id').value)">
  28. <div id="text"></div>
  29. </body>
  30. </html>

访问http://localhost:8080/SpringREST/simple/index/,在页面里的输入框中输入值,可以看到返回的数据:

Spring3.0实现REST实例的更多相关文章

  1. Spring3.0 与 MyBatis框架 整合小实例

    本文将在Eclipse开发环境下,采用Spring MVC + Spring + MyBatis + Maven + Log4J 框架搭建一个Java web 项目. 1. 环境准备: 1.1 创建数 ...

  2. Jbpm4.4+hibernate3.5.4+spring3.0.4+struts2.1.8整合例子(附完整的请假流程例子,jbpm基础,常见问题解决)

    Jbpm4.4+hibernate3.5.4+spring3.0.4+struts2.1.8 整合例子(附完整的请假流程例子). 1.jbpm4.4 测试环境搭建 2.Jbpm4.4+hibernat ...

  3. 开发基础框架:mybatis-3.2.8 +hibernate4.0+spring3.0+struts2.3

    一:项目下载地址(点击 Source code(zip)) https://github.com/fzxblgong/frame_2014-12-15/releases 版本:v1.2大小:20M 二 ...

  4. 《Spring3.0就这么简单》

    第一章 认识Spring 1.Spring提供的IOC容器,是Spring大杀器之一.容器将对象之间的依赖关系交给Spring进行控制,采用配制的方式对依赖关系进行描述,由Ioc容器负责依赖类之间的创 ...

  5. Jbpm4.4+hibernate3.5.4+spring3.0.4+struts2.1.8 整合例子

    转自:http://www.blogjava.net/wangxinsh55/archive/2011/07/24/354925.html   Jbpm4.4+hibernate3.5.4+sprin ...

  6. Oracle 11.2.0.4单实例打PSU,OJVM PSU补丁快速参考

    写在前面: 1.Oracel打每个补丁的操作有时存在差异,所以不管多熟悉,都应该在打任何补丁之前阅读新补丁中附带的readme. 2.Oracle每季度都会更新一个最新的PSU,本文最新指的是当前最新 ...

  7. 模块化之Spring3.0 web fragment和gradle构建项目

      1.背景 模块化开发很久以前就开始普及的概念.但是到了企业实际情况中,真正把模块化作为系统架构的核心的不多.或者说对模块化有这个意识,但是具体到底该如何实现,有些模糊,同时也许因为项目紧.任务中. ...

  8. spring3.0使用annotation完全代替XML(三)

    很久之前写过两篇博客: spring3.0使用annotation完全代替XML spring3.0使用annotation完全代替XML(续) 用java config来代替XML,当时还遗留下一些 ...

  9. spring3.0使用annotation完全代替XML

    @Service与@Component有什么不同?那天被问到这个问题,一时之间却想不起来,就利用这篇文章来纪录spring3.0中常用的annotation. 从spring2.5开始,annotat ...

随机推荐

  1. Python基础 第一天

    编码 #coding=utf-8 函数入口 if __name__== "__main__": 内置方法 type:a=1 type(a) help:import time hel ...

  2. Linux之uboot分析与移植20160601

    说一下uboot分析与移植: 1.下载.建立source insight工程.编译.烧写.如果无运行分析原因 tar xjf u-boot-2012.04.01.tar.bz2 cd u-boot-2 ...

  3. linux网络子系统内核分析

    1.选择路由 若要将数据包发至PC2,则linux系统通过查询路由表可知168.1.1.10(目的地址)的网关地址为192.168.1.1,此时linux系统选择网卡1发送数据包. 2.邻居子系统(通 ...

  4. 命令行工具cmder

    1.下载地址 http://bliker.github.io/cmder/ 分为两个版本:mini版和Full版 2.快捷命令配置: 比如,快速启动canssandra/redis数据库服务和查询工具 ...

  5. About Inside the Azure Storage Outage of November 18th

    Channel 9的官方解释 http://channel9.msdn.com/posts/Inside-the-Azure-Storage-Outage-of-November-18th 曾经在自己 ...

  6. 整齐地输出n的平方,立方

    初学C语言,有许多搞不明白的地方.编程,最重要的就是实践.今天,我偶然间看到书上的练习,做了一个能整齐地输出n,n的平方,n的立方的小程序.首先,我先用伪代码设计程序: 提示用户输入表格上限,下限或退 ...

  7. VC中实现GCC的2个比较常用的位运算函数

    在GCC中内嵌了两个位运算的函数,但在VC中并没有这两个函数(有相似函数). //返回前导的0的个数. int __builtin_clz (unsigned int x) //返回后面的0个个数,和 ...

  8. eth0: error fetching interface information: Device not found

    转载,原文出处:http://zh888.blog.51cto.com/1684752/775447 亲测有效,感谢作者!!! ----------------------------分割线----- ...

  9. Android JNI如何调用第三方库

    http://www.2cto.com/kf/201504/388764.html Android JNI找不到第三方库的解决方案 cannot load library 最近做一个jni项目,拿到的 ...

  10. aliyun install php apache mysql nginx

    yum install httpd -y yum install mysql mysql-server -y yum install php-mysql php-pgsql php-pecl-mong ...