Dynamic Command Class in Annotated Controller

后端 未结 1 1409
忘了有多久
忘了有多久 2021-01-05 22:56

As of Spring MVC 3, AbstractCommandController is deprecated so you can no longer specify the command class in setCommandClass(). Instead you hard-c

相关标签:
1条回答
  • 2021-01-05 23:37

    Instantiation of the command object is the only place where Spring needs to know a command class. However, you can override it with @ModelAttribute-annotated method:

    @RequestMapping(method = RequestMethod.POST) 
    public void show(HttpServletRequest request, 
        @ModelAttribute("objectToShow") Object objectToShow) 
    {
        ...
    }
    
    @ModelAttribute("objectToShow")
    public Object createCommandObject() {
        return getCommandClass().newInstance();
    }
    

    By the way, Spring also works fine with the real generics:

    public abstract class GenericController<T> {
        @RequestMapping("/edit")  
        public ModelAndView edit(@ModelAttribute("t") T t) { ... }
    }
    
    @Controller @RequestMapping("/foo")
    public class FooController extends GenericController<Foo> { ... }
    
    0 讨论(0)
提交回复
热议问题