SPRINGMVC Use @RequestMapping mapping requests

Source: Internet
Author: User

@RequestMapping annotations

SPRINGMVC uses this annotation to let the controller know which request paths can be handled, in addition to the methods that can be decorated, and can be decorated on the class.

– ClassfixedRighteousness Office: Provide initialStepof thePleasemapping information. PhasetheinWEBshould beuse the root of the eyeRecord
–MethodPlace: ProvidesintoaStepof theFineSub-mapping information. PhasetheinclassfixedRighteousness Officeof theURL. If
classfixedRighteousness OfficenotMarkNote@RequestMapping method Mark url Phase pair at
web should The root of the use record.

dispatcherservlet as the front controller of the SPRINGMVC, after intercepting the client request, The mapping information for this annotation determines how the request is processed.

@RequestMapping Interface Definition:
@Target ({ElementType.  METHOD, ElementType. TYPE}) @Retention (retentionpolicy.runtime) @Documented @mapping public @interface requestmapping {

Here is a test class

 Packagecom.led.test;ImportOrg.springframework.stereotype.Controller;Importorg.springframework.web.bind.annotation.RequestMapping;/** * @authorAlan * @date 2018/5/25 22:03*/@Controller @requestmapping ("/test") Public classSpringmvctest {Private Static FinalString SUCCESS = "SUCCESS"; @RequestMapping ("/testrequestmapping")     PublicString testrequestmapping () {System.out.println ("Testrequestmapping"); returnSUCCESS; }}

Index.jsp plus the link to send the corresponding request:

<a href= "test/testrequestmapping" >test requestmapping</a>

Run the project, click on the link, you can see the request path is the request path on the class and method of the request path stitching together, and the console also has a corresponding output.

@RequestMapping can also use the method property to define the request method:

/**      * Use method to define the     request method @return     *    /= "/testmethod", method =  Requestmethod.post)    public  String testMethod () {        System.out.println ("test Method ");         return SUCCESS;    }

index.jsp new Send Post Mode button, click to successfully jump to success.jsp

<form action= "Test/testmethod" method= "POST" >      <input type= "Submit" value= "Submit" >  </form >

If you are using a hyperlink (actually sending a GET request), the following error will be reported:

@RequestMapping Map Request a parameter (params), request a method, or an example of a Header (headers);

@RequestMapping (value = "/testparamsandheaders",            = {"Accept-language=zh-cn,zh;q=0.9,en-us;q=0.8,en;q =0.7 "},            = {" username "," age!=10 "})    public  String Testparamsandheaders () {        System.out.println ("testparamsandheaders");         return SUCCESS;    }

index.jsp Plus Test Link:

<a href= "test/testparamsandheaders?username=zhangsan&age=11" >test paramsandheaders</a>

Because both the params and the headers match the set value, the success.jsp is returned successfully.

SPRINGMVC Use @RequestMapping mapping requests

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.