2011-02-06 60 views
17

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> 

答えて

21

@SessionAttributesなし

  • :春SuperFormの新しいインスタンスをインスタンス化(タイプはregister()の署名から推測される)、値によってその特性を移入フォームフィールドから入力し、register()メソッドに渡します。 @SessionAttributes

  • :春(GETを処理するとき、それが起因@SessionAttributesの存在下に置かれた)セッションからのモデルの属性のインスタンスを取得し、フィールドからの値によってそのプロパティを更新し、register()メソッドに渡します。

    @SessionAttributesと、register()getCreateForm()によってモデルに配置されたモデル属性オブジェクトの同じインスタンスを取得し、ある

+0

ありがとうございます!あなたの説明はスーパーです!私は明らかになっています。 – zono

+0

説明のおかげで、私はちょっと混乱しています。 "@SessionAttribute register()は、getCreateForm()によってモデルに配置されたモデル属性オブジェクトの同じインスタンスを取得します。どちらの方法でも、ユーザーがフォームに入力したものを受け取っているので、この文の意味は?とにかく、各フィールドの正しい値を受け取っているので、sessionAttributeを使用するポイントは何ですか?何か不足していますか? – Jack

+0

は@SessionAttributesまたは@SessionAttributeでしたか? :p –

3

@axtavtが言ったことに追加:getCreateFormで、ドロップダウン(リストまたはマップ)の値を入力しているとします。または、登録メソッドで必要なフォームに値を入力していますが、それらをフォームに表示したい(隠されたフィールドでさえも)。ここで、registerメソッドでエラーが発生し、フォームを再度表示する必要があるとします。次の投稿に必要なドロップダウン値やその他の値を入力するには、それらをフォームに再投入する必要があります。 @SessionAttributeは、ここで@axtavtとして上でよく説明されています。

+0

は@SessionAttributeまたはSessionAttributesですか? :p –

0
@Controller 
@SessionAttributes("test") 
public class Controller{ 
    Customer customer; 

    public Controller() { 
     super(); 
     customer = new Customer(); 
    } 

    @ModelAttribute("test") 
    public Customer getCustomer() { 
     customer.setName("Savac"); 
     return customer; 
    } 

    @RequestMapping({"/index"}) 
    public ModelAndView showMainPage (@ModelAttribute("test") Customer customer, ModelMap model, method = RequestMethod.GET) { 
     //in the view you set the name 
     return new ModelAndView("index"); 
    } 

    @RequestMapping(value = "customer/{customerID}", method = RequestMethod.GET) 
    public ModelAndView viewAdvice(@PathVariable("customerID") int customerID, @ModelAttribute("test") Customer customer, ModelMap model) { 
     customer.setName("AnotherName"); 
     model.addAttribute("test", customer); 
     return new ModelAndView("customer"); 
    } 

} 
関連する問題