开发者

Can I user ModelAttribute in parent abstract class for controllers

开发者 https://www.devze.com 2023-03-04 18:46 出处:网络
Example: class CommonController { @ModelAttribute(\"refs\") public List getRef() { ... } @ModelAttribute(\"refs2\")

Example:

class CommonController {

    @ModelAttribute("refs")
    public List getRef() {
       ...
    }

    @ModelAttribute("refs2")
    public List getRef2() {
       ...
    }
}

class MyControll开发者_如何学Pythoner extends CommonController {

    @RequestMapping("/my")
    public String request() {
        return "/my";
    }
}

The ModelAttribute objects will be available on my page?


Why you wouldn't recommend to do it?

For example - I have two controllers create and edit.

They use the same references getting through @ModelAttribute and instean to copy past it would be better to create parent class for controllers.

class ReferenceParentController {

    @ModelAttribute("refs")
    public List getRef() {
       ...
    }

    @ModelAttribute("refs2")
    public List getRef2() {
       ...
    }
}

class Create extends ReferenceParentController  {
    ...
}

class Edit extends ReferenceParentController  {
    ...
}


According to this answer in Spring forum you can:

http://forum.springsource.org/showthread.php?103368-spring-3-mvc-help-with-multiple-forms-on-one-page&p=345386#post345386

But personally I wouldn't recommend to do it.

0

精彩评论

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