SPRINGMVC Learning Summary (ii) path mapping and request method qualification

Source: Internet
Author: User

SPRINGMVC Learning Summary (ii) path mapping and request method qualification one, path mapping no parameter access path
  • The access path to the SPRINGMVC project consists of a root path and a sub-path; In annotated development, the root path is labeled on top of the class name, and the subpath is labeled above the method name, for example:

    @Controller@RequestMapping(value = "/rootpath")public class Demo01Controller {     @RequestMapping(value = "/childpath.action")    public ModelAndView test(){        System.out.println("访问成功!");        ModelAndView mv = new ModelAndView();        mv.addObject("message", "测试成功");        mv.setViewName("hello");        return mv;    }}

    In this example:
    The root path is above the class name RequestMapping(value = "/rootpath") ;
    Above the method nameRequestMapping(value = "/rootpath")
    So the access path to this method is:http://localhost:8080/rootpath/childpath.action

    Access path with parameters in the path
  • If we want to pass one or more parameters to the background through the URL, we can use the URL to carry the parameter access without regard to the security problem, for example, we want to get an ID value, our background code is as follows:

    @Controller@RequestMapping(value = "/rootpath",method = RequestMethod.GET)public class Demo01Controller {    @RequestMapping(value = "/childpath/{id}")    public ModelAndView test(@PathVariable String id){        System.out.println("get提交的参数为:"+id);        ModelAndView mv = new ModelAndView();        mv.addObject("message", "测试成功");        mv.setViewName("hello");        return mv;    }}
    The access path at this time is:http://localhost:8080/rootpath/childpath.action/3.action
    The value of the parameter passed in this URL isid=1
      • When there are multiple parameters, as long as the parameter name of the method and the parameter in the path named one by one corresponds to one by one corresponding to the value, such as:

        @Controller@RequestMapping(value = "/rootpath",method = RequestMethod.GET)public class Demo01Controller {    @RequestMapping(value = "/childpath/{id}/{username}")    public ModelAndView test(@PathVariable String id,@PathVariable String username){        System.out.println("get提交的参数id为:"+id+"用户名为:"+username);        ModelAndView mv = new ModelAndView();        mv.addObject("message", "测试成功");        mv.setViewName("hello");        return mv;    }}

    The access path at this time is:http://localhost:8080/rootpath/childpath.action/3/sunwukong.action
    The parameter value passed in this URL is the id=1 user name:sunwukong

Ii. Limitation of methods
    • The qualified encoding location of the method is also in the annotation @requestmapping (), such as:
      Above method Name: is the qualification of the method = RequestMethod.GET request method, there are several common methods to choose:

SPRINGMVC Learning Summary (ii) path mapping and request method qualification

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.