zoukankan      html  css  js  c++  java
  • Spring3.0实现REST实例

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

    spring rest

          这是一个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.   
    9.   <description>Spring公共配置</description>  
    10.   
    11.   <!--检测注解-->  
    12.   <context:component-scan base-package="com.liqiu" />  
    13.   <bean   class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping" />  
    14.   <bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter" />  
    15. <!-- 注册视图解析器,说白了就是根据返回值指定到某个页面 -->      
    16.   <bean id="viewResolver"   class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
    17.     <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" />  
    18.     <property name="prefix" value="/"></property<!--页面文件的路径,在根目录下-->  
    19.    </bean>  
    20. </beans>  

     第三步:具体实现类

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

       index.jsp页面:

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

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

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

    spring rest 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.     
    25.   <body>  
    26.     ${message}  
    27.     <br>  
    28.     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)">  
    29.     <div id="text"></div>  
    30.   </body>  
    31. </html>  

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

    spring rest ajax

  • 相关阅读:
    Rainmeter 雨滴桌面 主题分享
    行人检測之HOG特征(Histograms of Oriented Gradients)
    const和readonly差别
    ADB命令解析
    Java实现 蓝桥杯VIP 算法训练 接水问题
    Java实现 蓝桥杯VIP 算法训练 星际交流
    Java实现 蓝桥杯VIP 算法训练 星际交流
    Java实现 蓝桥杯VIP 算法训练 星际交流
    Java实现 蓝桥杯VIP 算法训练 星际交流
    Java实现 蓝桥杯VIP 算法训练 星际交流
  • 原文地址:https://www.cnblogs.com/atomgame/p/3959239.html
Copyright © 2011-2022 走看看