About rest is what thing, here I no longer say, you can go to http://blog.csdn.net/pilou5400/archive/2010/12/24/6096861.aspx to see the introduction, directly into the topic:
This is a restful visit, and spring will fully support rest from 3.0 onwards. Had to lament Spring's toughness.
Project structure:
The first step is always configuration, the use of the framework is always configured first, the configuration in Web. xml:
<?xml version= "1.0" encoding= "UTF-8"?> <web-app version= "3.0" xmlns= "Http://java.sun.com/xml/ns/javaee" Xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http ://java.sun.com/xml/ns/javaee/web-app_3_0.xsd "> <display-name></display-name> <context-param > <!--the path of the rest profile, seemingly not configured is also loading this address, this place a bit of doubt, everyone pointing to <param-name>contextconfiglocation</ Param-name> <param-value>/WEB-INF/rest-servlet.xml</param-value> </context-param> < Listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <!--Configure a servlet that has this servlet Unified dispatch page request--<servlet-name>rest</ Servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>2</load-on-startup> </servlet> <servlet-mapping> <!--mapping path, do not write AS/* That would intercept all the visits, evenJSP pages are not accessible-<servlet-name>rest</servlet-name> <url-pattern>/</url-pattern> </ servlet-mapping> <welcome-file-list> <welcome-file>/index.jsp</welcome-file> </ Welcome-file-list> </web-app>
Step Two: Configure Rest-servlet.xml this file
<?xml version= "1.0" encoding= "UTF-8"?> <beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:jee= "Http://www.springframework.org/schema/jee" xmlns:tx= " Http://www.springframework.org/schema/tx "xmlns:context=" Http://www.springframework.org/schema/context "xmlns: aop= "HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP" 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 "default-lazy-init=" true "> <description> Spring Public Configuration </desCription> <!--detection notes--<context:component-scan base-package= "Com.liqiu"/> <bean class= " Org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping "/> <bean class=" Org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter "/> <!--registered View parser, To be blunt is to specify to a page based on the return value--<bean id= "Viewresolver" class= " Org.springframework.web.servlet.view.InternalResourceViewResolver "> <property name=" viewclass "value=" Org.springframework.web.servlet.view.JstlView "/> <property name=" prefix "value="/"></property> < !--the path to the paging file, in the root directory-</bean> </beans>
Step Three: Concrete implementation class
Package Com.liqiu.controller; Import java.io.IOException; Import Javax.servlet.http.HttpServletRequest; Import Javax.servlet.http.HttpServletResponse; Import Org.springframework.stereotype.Controller; Import org.springframework.web.bind.annotation.PathVariable; Import org.springframework.web.bind.annotation.RequestMapping; Import Org.springframework.web.bind.annotation.RequestMethod; @Controller @RequestMapping ("/simple") public class Simplecontroller {//map path/simple/index When accessing this path, execute this method @ Requestmapping ("/index") public String Index (httpservletrequest request, httpservletresponse response) {//response, The request is automatically passed in Request.setattribute ("message", "Hello,this is a example of Spring3 restful!"); return "index.jsp"; }//Get different content based on ID, get property @RequestMapping (value= "/{id}", method=requestmethod.get) public String Get by @pathvariable (@ Pathvariable String id,httpservletrequest request, HttpServletResponse response) throws ioexception{ Request.setattribute ("message", "Hello,this is a example of SPRINg3 restful!<br/>id: "+id+" "); Response.getwriter (). Write ("Put ID is:" +id); return "index.jsp"; return null; } }
index.jsp page:
<%@ page language= "java" pageencoding= "UTF-8"%>
In the browser, enter: http://localhost:8080/SpringREST/simple/index/, you can see the effect.
You can also enter different parameters in the page, get different content, enter the address: http://localhost:8080/SpringREST/simple/88888, this time the implementation is the Get method, through the annotations to get the ID value, the effect:
About spring Rest's support for Ajax, in fact, in response to Ajax is to return a string to the page through response, since the response object can be obtained, the problem is solved, we modified the Get method:
@RequestMapping (value= "/{id}", Method=requestmethod.get) public string GET (@PathVariable string ID, HttpServletRequest request, HttpServletResponse response) throws ioexception{//request.setattribute ("message", " Hello,this is a example of Spring3 restful!<br/>id: "+id+" "); Response.getwriter (). Write ("Put ID is:" +id); return "index.jsp"; return null; }
Retrofit index.jsp page:
<%@ page language= "java" pageencoding= "UTF-8"%>
To access http://localhost:8080/SpringREST/simple/index/, enter a value in the input box in the page to see the returned data:
Demo Download: http://download.csdn.net/source/3383568