• 周六. 10 月 12th, 2024

5G编程聚合网

5G时代下一个聚合的编程学习网

热门标签

Several ways for controller to accept parameters in spring MVC

King Wang

1 月 3, 2022

1.@RequestParam Annotations can get the requested parameters

@RequestMapping("/get")
public ModelAndView get(@RequestParam("id") Integer id){
...
}

For example, through the above way , stay Controller In the method parameter of @RequestParam(“id”) The annotation can get the id Property value , And give it to those who join in Integer Type of id.

2.@SessionAttribute Annotations can be obtained Session Parameters in domain

@RequestMapping("/get")
public ModelAndView get(@SessionAttribute("user") User user){
...
}

For example, the above usage , You can do this by using @SessionAttribute(“user”) The annotation can be obtained session The domain is called user Object and assign to type User Of user

3.@PathVariable Annotations can be obtained URL Parameters in

@RequestMapping("/get/{id}")
public ModelAndView get(@PathVariable("id") Integer id){
...
}

The above usage is to use @PathVariable(“id”) Comments to get requests URL route “/get/{id}” Medium id value , for example URL  by “/get/2”, It will put 2 Assign to Integer Type of id.

4.@RequestAttribute Annotations can be obtained request Property values in domain

For example, in jsp Waiting for page , Or in other Controller The method is in request The value… Is stored in the domain

request.setAttribute("id",1);

You can get

@RequestMapping("/get")
public ModelAndView get(@RequestAttribute("id") Integer id){
...
}

发表回复