URL Wildcard Mapping

Source: Internet
Author: User

Original: http://www.cnblogs.com/liukemng/p/3726897.ht

1.URL Path Mapping

1.1. Configure multiple URL mappings for an action:

We changed the @requestmapping of the Helloworldcontroller Index () action method in the previous article to @requestmapping (value={"/index", "/hello"}, method = {Requestmethod.get}), which indicates that the action is configured with a/index and/hello two mappings. Run the tests as follows:

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 the results are as follows:

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 that corresponds to index.

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

You can see that the map is the urltest corresponding action.

My pro-Test code:

@Controller @requestmapping ("/helloworld") Public classHelloworldcontrloller {//One action matches multiple URL paths@RequestMapping (value={"/index", "/hello"},method=requestmethod.get) Publicstring Hello (string Username,model Model) {System.out.println ("//////////"+username); Model.addattribute ("hello111", "Hello:" +username);//Username matches the name value of the JSP page input text box        return"Success"; }    //wildcard characters@RequestMapping (value= "/detail/{id}", method=requestmethod.get) PublicString Getdetail (@PathVariable (value= "id") (Integer Id,model Model) {Model.addattribute ("id", id); return"Detail"; }}

URL Wildcard Mapping

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.