In my controller,My controller method names are equals to the requestmapping url.For example,/list
is equal to method name list
. Is there has a common handler method to short my code?I do not want to write every controller and method in these way.I remember that .net mvc has a comom way to config it.What about Spring MVC?
@Controller
@RequestMapping(value = "/fooController ")
public class FooController {
@RequestMapping("/list") public String list(...) { ... }
@RequestMapping("/save") public String save(...) { ... }
@RequestMapping("/delete") public String delete(...) { ... }
}
@Controller
@RequestMapping(value = "/basketballController ")
public class BasketballController {
@RequestMapping("/list") public String list(...) { ... }
@RequestMapping("/save") public String save(...) { ... }
@RequestMapping("/delete") public String delete(...) { ... }
}
You can extend to
AbstractControllerUrlHandlerMapping
and override the method and add bean in web.xml.Here is an example.
You can use
RequestMappingHandlerMapping
and override default codeAs you can see here it tries to resolve RequestMapping annotation from method and combine with Controller class annotation.
Just replace the logic to use method name instead.
See here a similar logic. Instead of method name security check was used.
UPDATE:
The classes to test. For me it works. MappingHandler I use method name check because there are much more controllers, errors controllers etc. For real solution I would introduce an annotation on the controllers to exclude default spring controllers from the logic
Config to use the mapping
Controller
test class
Will an abstract base class for both controllers work for you?