开发者

what is the dif between requestMapping on controller and method

If I have:

@RequestMapping("/user")
public class RegistrationController {

   @RequestMapping(value = "/register", method = RequestMethod.GET)
   public String getRegisterPage(Model model) {

What is the difference? I mean what will happen if I remove the /user mapping, will my /register mapping s开发者_如何学运维till work?


A @RequestMapping on the class level is not required. Without it, all paths are simply absolute, and not relative.

see 15.3.2 Mapping requests with @RequestMapping

This means if you specify the classlevel annotations, the url shall be relative, so for register it shall be /user/register(URL to Handler mapping) and likewise.


As described here you can also use Type level mapping and relative path mappings on method level to be dry and don't duplicate root at every methods.

@Controller
@RequestMapping("/employee/*")
public class Employee {

    @RequestMapping("add")
    public ModelAndView add(
            @RequestParam(value = "firstName") String firstName,
            @RequestParam(value = "surName") String surName) {
        //....
    }

    @RequestMapping(value={"remove","delete"})
    public ModelAndView delete(
        //....
    }   
}

Spring doc: At the method level, relative paths (e.g. "edit.do") are supported within the primary mapping expressed at the type level.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜