2

これが私のコードです:

public ModelAndView login(@ModelAttribute("testVO") TestVO testVO){
    //test the VO work theory
    //testVO = new TestVO();
    testVO.setTestStr("this is my test!");
    return "index/index";
}

new を使用して testVO のオブジェクトを作成したとき。jsp ページで値を取得できません。set メソッドを使用した場合は機能します。

つまり、オブジェクト testVo は IOC コンテナーによって作成されているため、JSP は、自分で作成したものではなく、コンテナーから参照を取得します。

私は正しいですか?ありがとうございます。

4

2 に答える 2

6

あなたはそれを正しく推測しました。以下のテキストは春のドキュメントからのものです:

An @ModelAttribute on a method argument indicates the argument should be 
retrieved from the model. If not present in the model, the argument should be
instantiated first and then added to the model.

自分で作成したい場合は、明示的にモデルに追加する必要があります(以下のように)。これにより、jsp で使用できるようになります。

public String login(Model model){

    TestVO testVO = new TestVO();
    testVO.setTestStr("this is my test!");

    model.addAttribute("testVO", testVO);
    return "index/index";
}
于 2013-06-28T10:24:52.207 に答える
1

男、 @ModelAttribute アノテーションの最良の説明は次のとおりです。

http://static.springsource.org/spring/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-ann-modelattrib-methods

少なくとも 2 つの章を読んでください。これにより、理論的な知識が得られます。

私のブログで見つけることができる質問の実際的な側面:

http://fruzenshtein.com/spring-mvc-form-handling/

お役に立てば幸いです

于 2013-06-28T12:18:57.593 に答える