我试图理解Spring MVC的体系结构。然而,我完全被@SessionAttributes的行为弄糊涂了。
请看下面的SampleController,它正在通过SuperForm类处理post方法。事实上,只是SuperForm类的字段正如我所期望的那样是绑定的。
然而,在我将@SessionAttributes放入Controller之后,处理方法绑定为SubAForm。有人能解释一下这次捆绑发生了什么吗。
-------------------------------------------------------
@Controller
@SessionAttributes("form")
@RequestMapping(value = "/sample")
public class SampleController {
@RequestMapping(method = RequestMethod.GET)
public String getCreateForm(Model model) {
model.addAttribute("form", new SubAForm());
return "sample/input";
}
@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>