Search code examples
javaspring-mvcconvention-over-configur

Spring MVC: CoC for requesting mapping


Having used other MVC frameworks such as Zend Framework in the past, I am used to the idea of requests being mapped as follows by default.

http://mysite.com/user/add calls the add method in the User controller class, which then calls a view named add, located in a directory named user.

Is there a way that I can achieve this (or similar) in Spring MVC 3 rather than using @RequestMapping in my Controller classes and specifying the view to use in each method?


Solution

  • I believe this is possible, but then you're limited by what Spring MVC's convention-over-configuration will do.

    I think that the bare minimum is that your controller be annotated with @Controller, and also a class-level @RequestMapping annotation. The individual public methods on that class are then mapped by name. If the methods do not return a view or view name, then the view name will be inferred from the method name.

    The documentation doesn't really go into this, though, you're very much encouraged to use method-level annotations, to keep things explicit.