A little supplement to the SPRINGMVC controller

Source: Internet
Author: User

First, review the previous controller's wording:http://www.cnblogs.com/eco-just/p/7882016.html.

We can see that the previous wording was this:

@RequestMapping ("/hello3")    //http://localhost: 8080/webmvc/hello4?name=eco     Publicstring Hello4 (string name) {System. out. println (name); return "Hello"; } @RequestMapping ("/hello4")     Public voidHello3 (HttpServletRequest req,httpservletresponse resp) throws exception{Req.setattribute ("a","that's me .");//A can be written on the following page with the EL expressionReq.getrequestdispatcher ("01.jsp"). Forward (req, resp);//request forwarding to the root directory jsp--does not require a view resolver//resp.sendredirect ("01.jsp");//request redirected to jsp--in root does not require view resolver}
}

Or combine the above two, but we find a problem, if it is written as above, how many links have to be written

The matching @RequestMappingcan be said to be very egg-sore. For example, for a class of links, we can combine this type of link into a

A @requestmapping up. So the following @PathVariableto say:

@RequestMapping ("/{put}")     Publicstring Hello1 (@PathVariable string put) {returnput; }
@RequestMapping ("/free/{put}") PublicString Hello2 (@PathVariable ("put") StringGet) { return Get; }

Two methods:

1. Pass the path parameter put to the method parameter put, note that the two put to be consistent, that is, the path parameter name is put, the function parameter name is also put;

[Email protected] ("put") gets the value of the path parameter named put and passes it to the method parameter get, where the function parameter name can be arbitrary;

In this way, although we only write a @requestmapping, but can achieve a lot of link jump behavior:

http://localhost:8080/projectname/index--------jump to index page;

http://localhost:8080/projectname/Hello---------jump to the Hello page;

http://localhost:8080/projectname/Free/good---------jump to good page;

http://localhost:8080/projectname/free/job-----------Jump to the job page;

A little supplement to the SPRINGMVC controller

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.