"3" SPRINGMVC common annotations of @controller and @requestmapping

Source: Internet
Author: User

For various annotations, the first is of course "@Controller", indicating that a class is a Controller.

The "@RequestMapping" request path mapping, if labeled at the class level of a controller, indicates that access to the method under such a path is accompanied by the path of its configuration, most commonly labeled on the method, indicating which specific method is used to accept processing of a request.

@Controller @requestmapping (value= "/book") public class Bookcontroller {@RequestMapping (value= "/title") public String GetTitle () {return ' title ';} @RequestMapping (value= "/content") public String getcontent () {return "content";}}

Since the Bookcontroller class adds "@RequestMapping" annotations to the Value= "/book", all related paths are prefixed with "/book", that is, the requested URLs are:

    1. Http://localhost:8080/book/title
    2. http://localhost:8080/book/content

" /" before and after "@RequestMapping" value has no effect on the requested path, that is, value= "book", "/book", "/book/" have the same effect


properties for "@RequestMapping"

  • Value: Specifies the actual URL of the request
    1. Normal specific values. As in the previous value= "/book".
    2. A class of values with a variable
    @RequestMapping (value= "/get/{bookid}") public string Getbookbyid (@PathVariable string Bookid,model Model) { Model.addattribute ("BookId", bookId); return "book";}

    the BookID in the path can be used as a variable, @PathVariable annotation to extract the value of a variable in the path

    3.ant style
    @RequestMapping (value= "/get/id?") : Can match "/get/id1" or "/get/ida", but does not match "/get/id" or "/GET/IDAA";
    @RequestMapping (value= "/get/id*"): Can Match "/get/idabc" or "/get/id", but does not match "/GET/IDABC/ABC";
    @RequestMapping (value= "/get/id/*"): Can Match "/GET/ID/ABC", but does not match "/GET/IDABC";
    @RequestMapping (value= "/get/id/**/{id}"): Can Match "/get/id/abc/abc/123" or "/get/id/123", that is, ant style and URI template variable style can be mixed;

    4. A class of values with regular expressions
    @RequestMapping (value= "/get/{idpre:\\d+}-{idnum:\\d+}"): Can Match "/get/123-1", but cannot match "/get/abc-1", so you can design stricter rules.
    can be done by@PathVariable Annotations Extract the variables in the path (idpre,idnum)

    5. or relationship
    @RequestMapping (value={"/get", "/fetch"}) that is/get or/fetch are mapped to the method

  • Method: Specifies the type of method requested, GET, POST, PUT, delete, and so on;
    @RequestMapping (value= "/get/{bookid}", method={requestmethod.get,requestmethod.post})

  • Params
    @RequestMapping (params= "Action=del"), the request parameter contains "Action=del", such as: Http://localhost:8080/book?action=del

  • Headers
    @RequestMapping (value= "/header/id", headers = "Accept=application/json"): Indicates that the requested URL must be "/header/id and that the request header must have" Accept = Application/json "parameter to match.


"3" SPRINGMVC common annotations of @controller and @requestmapping

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.