The MAVEN Project Spring Framework integrates restful

Source: Internet
Author: User

First, the introduction of the restful

The Resturl of SPRINGMVC is provided through @requestmapping and @pathvariable annotation,
Method=requestmethod.delete's DELETE request can be processed through such as @requestmapping (value= "/blog/{id}",/BLOG/1).

When sending a request pass parameter, it is no longer the localhost:8080/blog?id=1 form, but the LOCALHOST:8080/BLOG/1.

@RequestMapping (value= "/blog/{id}", method=requestmethod.delete)modelandview DELETE (@ Pathvariable Long id,httpservletrequest request,httpservletresponse response) {      Blogmanager.removebyid (ID);       Modelandview ( list_action);} 

In this way, the parameters of the id=1 can be taken in the controller.

Second, configure restful

1. Web. XML configuration

<?xml version= "1.0" encoding= "UTF-8"? ><web-app xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance"xmlns= "Http://java.sun.com/xml/ns/javaee"xsi:schemalocation= "Http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"ID= "Schedule-console" version= "3.0" > <!--Configure Spring servlet--<servlet> &LT;SERVLET-NAME&GT;SPRINGMV C</servlet-name> <servlet-class>Org.springframework.web.servlet.DispatcherServlet</servlet-class> <init-param> <param-name>contextConfigLocation</param-name> <param-value>/web-in F/applicationcontext.xml</param-value> </init-param> <load-on-startup>1</load-on-startup > </servlet><!--Overwrite the default servlet's/, the SPRINGMVC servlet will handle the mapping of the original processing static resources--><servlet-mapping><  Servlet-name>springmvc</servlet-name><url-pattern>/</url-pattern></servlet-mapping> <!--the servlet is provided for containers such as tomcat,jetty, mapping static resources from/To/Static/directory, such as the original access http://Localhost/foo.css, now http://localhost/static/foo.css --<servlet-mapping><servlet-name>default</servlet-name><url-pattern>/Static/*</url-pattern></servlet-mapping> <!--URL Rewrite filter to convert the Access static resource Http://localhost/foo.css to HTTP// Localhost/static/foo.css--><filter><filter-name>urlrewritefilter</filter-name>< filter-class>org.tuckey.web.filters.urlrewrite.urlrewritefilter</filter-class><init-param>< param-name>confreloadcheckinterval</param-name><param-value>60</param-value></ init-param><init-param><param-name>loglevel</param-name><param-value>debug</ param-value></init-param></filter><!--Browser does not support Put,delete method, the filter will/blog?_method= The delete is converted to the standard HTTP Delete method--><filter><filter-name>hiddenhttpmethodfilter</filter-name>< filter-class>org.springframework.web.filter.hiddenhttpmethodfilter</filter-class></filter>< Filter-mapping><filter-name>hiddenhttpmethodfilter</filter-name><servlet-name>springmvc </servlet-name></filter-mapping> <!--Spring log4j listener--<listener> <listener-class>org.springframework.web.ut Il.          Log4jconfiglistener</listener-class> </listener> <!--character Set filter--<filter> <filter-name>CharacterEncodingFilter</filter-name> <filter-class>org.springframework.web. Filter. Characterencodingfilter</filter-class> <init-param> <param-name>encoding</param -name> <param-value>UTF-8</param-value> </init-param> &LT;INIT-PARAM&G              T <param-name>forceEncoding</param-name> <param-value>true</param-value> </ init-param> </filter> <filter-mapping> <filter-name>characterencodingfilter</fi Lter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!--Welcome Page--Lt;welcome-file-list> <welcome-file>index.jsp</welcome-file> </welcome-file-list> <!--settings S Ession Effective Time-<session-config> <session-timeout>9999</session-timeout> </session-config > </web-app>

2, add dependency in Pom.xml, introduce jar package

    <!--URL rewrite filter to access static resources http://localhost/foo.css to http://localhost/static/ Foo.css-    <dependency>         <groupId>org.tuckey</groupId>         <artifactId> urlrewritefilter</artifactid>         <version>4.0.4</version>    </dependency>

3. Controller Writing

 PackageCom.zp.test.controller;Importjavax.servlet.http.HttpServletRequest;ImportJavax.servlet.http.HttpServletResponse;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.PathVariable;Importorg.springframework.web.bind.annotation.RequestMapping, @Controller @requestmapping ("/userinfo") Public classUsercontroller {@RequestMapping (value= "/{id}")       Public voidTest (@PathVariable Long id,httpservletrequest request,httpservletresponse response) {SYSTEM.OUT.PR                          INTLN (ID); }      }

4. JSP page

<%@ page language= "java" contenttype= "text/html; Charset=iso-8859-1 "pageencoding= "Iso-8859-1"%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >function Test1 () {$.ajax ({URL:"/TEST2/USERINFO/5", data:{}, type:"POST", DataType:"JSON", Success:function (data) {alert ("Success");}, Error:function () {alert ("Error");        }     }); }function test2 () {window.location.href= "Test2.jsp";}</script><body><input type= "button" value= "Test1" onclick= "test1 ();" /><input type= "button" value= "Test2" onclick= "test2 ();" /></body>

5, start the service, enter the URL: localhost:8080/test2

Test1 button is send request, enter controller;

The Test2 button is a jump static page test2.jsp.

Console output: 5 to prove restful build success

The MAVEN Project Spring Framework integrates restful

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.