Spring MVCのアーキテクチャを理解しようとしています。しかし、私は@SessionAttributesの動作によって完全に混乱しています。@SessionAttributesの使い方が混乱しています
下記のSampleControllerを見てください。これは、SuperFormクラスのpostメソッドを処理しています。実際、SuperFormクラスのフィールドは、私が期待したようにバインドしているだけです。
しかし、コントローラに@SessionAttributesを置くと、処理メソッドはSubAFormとしてバインドされます。このバインディングで何が起こったのか誰にでも説明できますか?
POST
要求を処理するとき、スプリングは、以下のない
-------------------------------------------------------
@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>
ありがとうございます!あなたの説明はスーパーです!私は明らかになっています。 – zono
説明のおかげで、私はちょっと混乱しています。 "@SessionAttribute register()は、getCreateForm()によってモデルに配置されたモデル属性オブジェクトの同じインスタンスを取得します。どちらの方法でも、ユーザーがフォームに入力したものを受け取っているので、この文の意味は?とにかく、各フィールドの正しい値を受け取っているので、sessionAttributeを使用するポイントは何ですか?何か不足していますか? – Jack
は@SessionAttributesまたは@SessionAttributeでしたか? :p –