Springmvc URL request to action Mapping (1)

Source: Internet
Author: User

    1. URL Path Mapping

      1.1. Configure multiple URL mappings for an action:

@RequestMapping (value={"/index", "/hello"}, method = {Requestmethod.get}), which indicates that the action is configured with/index and/hello two mappings. Run the test and you can see that the/helloworld/hello request also matches successfully.

1.2.URL Request Parameter Mapping:

This is often used when querying, for example, we get a record based on ID or number.

In Helloworldcontroller add a getdetail action with the following code:

@RequestMapping (value= "/detail/{id}", method = {Requestmethod.get}) public Modelandview Getdetail (@PathVariable (      Value= "id") Integer ID) {Modelandview modelandview = new Modelandview ();      Modelandview.addobject ("id", id);      Modelandview.setviewname ("detail"); return Modelandview;}

where value= "/detail/{id}", the {ID} in the placeholder represents a URL that can map requests to/detail/xxxx such as:/detail/123, and so on.

The parameter of the @pathvariable method (value= "id") Integer ID is used to map the variable corresponding to the placeholder in the URL to the parameter ID, @PathVariable (value= "id") of the value to be and the placeholder/{id} The values in the curly braces are the same.

Add the detail.jsp view to the views to show the ID value that was obtained. The contents of the view are as follows:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

Run the test, request the URL address http://localhost:8080/SpringMVCLesson/helloworld/detail/123, and you can see that the ID we requested is displayed correctly.

1.3.URL Wildcard Mapping:

We can also configure URL mappings with wildcards with the wildcard character "? "and" * "two characters. Which "? "Represents 1 characters," * "means matching multiple characters," * * "means matching 0 or more paths.

For example:

"/helloworld/index?" Can match "/helloworld/indexa", "/helloworld/indexb", but can not match "/helloworld/index" also can not match "/HELLOWORLD/INDEXAA";

"/helloworld/index*" can Match "/helloworld/index", "/helloworld/indexa", "/HELLOWORLD/INDEXAA" but not "/helloworld/index/" A ";

"/helloworld/index/*" can Match "/helloworld/index/", "/helloworld/index/a", "/helloworld/index/aa", "/helloworld/index/ AB "but can not match"/helloworld/index ","/helloworld/index/a/b ";

"/helloworld/index/**" can Match "/helloworld/index/" under the many sub-paths, such as: "/HELLOWORLD/INDEX/A/B/C/D";

If you now have "/helloworld/index" and "/helloworld/*", what would you do if the request address is "/helloworld/index"? Spring MVC matches the longest matching precedence principle (that is, the maximum number of matches in the mapping configuration), so it matches "/helloworld/index" and is tested as follows:

Add a urltest action to Helloworldcontroller in the following:

@RequestMapping (value= "/*", method = {Requestmethod.get}) public Modelandview urltest () {Modelandview Modelandview =  New Modelandview ();        modelandview.setviewname ("Urltest"); return Modelandview;}

In the Views folder to add a new view urltest.jsp, in order to make the difference with index.jsp urltest.jsp content as follows:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

Request Http://localhost:8080/SpringMVCLesson/helloworld/index View results: You can see that the map is the action of index.

Request Http://localhost:8080/SpringMVCLesson/helloworld/AAA View results: You can see that the map is the urltest corresponding action.

1.4.URL Regular Expression mapping:

Spring MVC also supports the mapping configuration of regular expressions, which we show through a test:

Add a regurltest action to Helloworldcontroller in the following:

@RequestMapping (value= "/reg/{name:\\w+}-{age:\\d+}", method = {Requestmethod.get}) public Modelandview regurltest (@ Pathvariable (value= "name") String name, @PathVariable (value= "age") Integer age) {Modelandview Modelandview = new Mo  Delandview ();     modelandview.addobject ("name", name);  Modelandview.addobject ("Age", age);  Modelandview.setviewname ("Regurltest"); return Modelandview;}

In the Views folder, add a new view regurltest.jsp content as follows:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

Request HTTP://LOCALHOST:8080/SPRINGMVCLESSON/HELLOWORLD/REG/HANMEIMEI-18 View Results:

Request Http://localhost:8080/SpringMVCLesson/helloworld/reg/Hanmeimei-Lilei View results (you will find that the corresponding action is not found to return 404):

2. Limit the way the action accepts requests (GET or POST):

Previously we configured on Helloworldcontroller's index () action method as @requestmapping (value= "/*", method = {Requestmethod.get}) We changed it to @requestmapping (value= "/*", method = {Requestmethod.post}) and requested again http://localhost:8080/SpringMVCLesson/ Helloworld/index Try it:

Here you can see that the result maps to the Urltest action, because we configured the urltest on the @requestmapping (value= "/*", method = {Requestmethod.get}), When the index action map is not compliant, it maps to urltest.

We can also configure @requestmapping (value= "/*", method = {requestmethod.get, requestmethod.post}) to indicate that the action can accept a GET or POST request, However, it is simpler to do not configure the method to support all requests by default.

3. Limit the parameters of the request that the action accepts:

We can specify that a parameter must be included in a request for a map for an action, or must not contain a parameter, or a parameter must be equal to a value, or a parameter must not be equal to a value.

3.1. Specify that the mapping request must contain a parameter:

Add a paramstest action to Helloworldcontroller in the following:

@RequestMapping (value= "/paramstest", params= "Example", method = {Requestmethod.get}) public Modelandview paramstest ()  {Modelandview Modelandview = new Modelandview ();        modelandview.setviewname ("Paramstest"); return Modelandview;}

In the Views folder, add a new view paramstest.jsp content as follows:

<%@ page language= "java" contenttype= "text/html; Charset=utf-8 "pageencoding=" UTF-8 "%><! DOCTYPE HTML PUBLIC "-//w3c//dtd HTML 4.01 transitional//en" "Http://www.w3.org/TR/html4/loose.dtd" >

Request Http://localhost:8080/SpringMVCLesson/helloworld/paramstest View Results:

Here you can see that the action result is not found paramstest or mapped to the Urltest action.

Request Http://localhost:8080/SpringMVCLesson/helloworld/paramstest?example View Results:

This time you can see that the request is mapped to the Paramstest action.

3.2. Specify that the mapping request must not contain a parameter:

Put the paramstest you just added to the @requestmapping (value= "/paramstest", params= "Example", method = {Requestmethod.get}) Change to @requestmapping (value= "/paramstest", params= "!example", method = {Requestmethod.get})

Re-request Http://localhost:8080/SpringMVCLesson/helloworld/paramstest?example View results:

You can see that the action is mapped to urltest without finding paramstest.

3.3. Specify that the mapping request or a parameter must be equal to a value:

Put the paramstest you just added to the @requestmapping (value= "/paramstest", params= "Example", method = {Requestmethod.get}) Change to @requestmapping (value= "/paramstest", params= "example=aaa", method = {Requestmethod.get})

Request Http://localhost:8080/SpringMVCLesson/helloworld/paramstest?example=BBB View Results:

You can see that the action was mapped to urltest without finding paramstest.

Request Http://localhost:8080/SpringMVCLesson/helloworld/paramstest?example=AAA View Results:

This time you can see that the request is mapped to the Paramstest action.

3.4. Specify that the mapping request or a parameter must not be equal to a value:

Put the paramstest you just added to the @requestmapping (value= "/paramstest", params= "Example", method = {Requestmethod.get}) Change to @requestmapping (value= "/paramstest", params= "example!=aaa", method = {Requestmethod.get})

Request Http://localhost:8080/SpringMVCLesson/helloworld/paramstest?example=BBB View Results:

You can see that the request is mapped to the Paramstest action.

Request Http://localhost:8080/SpringMVCLesson/helloworld/paramstest?example=AAA View Results:

You can see that the action was mapped to urltest without finding paramstest.

Note: When we specify multiple parameters for the params such as: params={"example1", "example2"}, it represents an and relationship, that is, two parameter limits must be met at the same time.

4. Limit the request header parameters that the action accepts:

As with the request parameter that restricts the action, we can also specify that a parameter must be included in the request header for an action, or must not contain a parameter, or a parameter must be equal to a value, or a parameter must not be equal to a value.

4.1. Specify that the map request header must contain a parameter:

@RequestMapping (value= "/headertest", headers = "Example"). As with the limit request parameter, you can test it by referring to the example above.

4.2. Specify that the map request header must not contain a parameter:

@RequestMapping (value= "/headertest", headers = "!example"). As with the limit request parameter, you can test it by referring to the example above.

4.3. Specify the mapping request header or a parameter must be equal to a value:

@RequestMapping (value= "/headertest", headers = "accept=text/html"). As with the limit request parameter, you can test it by referring to the example above.

4.4. Specify that the mapping request header or a parameter must not be equal to a value:

@RequestMapping (value= "/headertest", headers = "accept!=text/html"). As with the limit request parameter, you can test it by referring to the example above.

Note: When we specify multiple parameters for headers such as: headers={"example1", "example2"}, it represents an and relationship, that is, two parameter limits must be met at the same time.

Springmvc URL request to action Mapping (1)

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.