开发者

Do I have to put @Controller or @SessionAttributes in SuperController class?

开发者 https://www.devze.com 2023-02-08 16:52 出处:网络
Post methods handled by SuperController class due to code-sharing. I guess such as @Controller and @SessionAttributes are inheritable so I have to just put these in subController class. Am I correct i

Post methods handled by SuperController class due to code-sharing. I guess such as @Controller and @SessionAttributes are inheritable so I have to just put these in subController class. Am I correct in assuming?

-------------------------------------------------------

@Controller
@SessionAttributes("form")
@RequestMapping(value = "/sample")
public class SampleController extends BaseController {

    @RequestMapping(method = RequestMethod.GET)
    public String getCreateForm(Model model) {
        model.addAttribute("form", new SubAForm());
        return "sample/input";
    }

}

-------------------------------------------------------

public class BaseController {

    @RequestMapping(method = RequestMethod.POST)
    public String register(@ModelAttribute("form") SuperForm form, Model model) {
        return "sample/input";
    }

}

-------------------------------------------------------

public class SuperForm {

    private Long superId;

    public Long getSuperId() {
        return superId;
    }

    public void setSuperId(Long superId) {
        this.superId = superId;
    }

}

-------------------------------------------------------

public class SubAForm extends SuperForm {

    private Long subAId;

    public Long getSubAId() {
        return subAId;
    }

 开发者_高级运维   public void setSubAId(Long subAId) {
        this.subAId = subAId;
    }

}

-------------------------------------------------------

<form:form modelAttribute="form" method="post">
    <fieldset>
        <legend>SUPER FIELD</legend>
        <p>
            SUPER ID:<form:input path="superId" />
        </p>
    </fieldset>
    <fieldset>
        <legend>SUB A FIELD</legend>
        <p>
            SUB A ID:<form:input path="subAId" />
        </p>
    </fieldset>
    <p>
        <input type="submit" value="register" />
    </p>
</form:form>


No, you don't need that, unless you want your base controller to be able to act as a controller as well.

0

精彩评论

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