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