2012-02-12 7 views
2

私は非常に春に新しく、私は次の問題に遭遇しています。@ModelAttributeのために@Autowired

@Autowiredが完全に動作する次のコントローラがあります(デバッグを試しても問題ありません)。

@Controller 
@RequestMapping(value = "/registration") 
@SessionAttributes("rf") 
public class RegistrationController 
{ 
    @Autowired 
    UserJpaDao userDao; 

    @RequestMapping(method = RequestMethod.GET) 
    @Transactional 
    public String setupForm(Model model) throws Exception 
    { 
     model.addAttribute("rf", new RegistrationForm()); 
     return "registration"; 
    } 

    @RequestMapping(method = RequestMethod.POST) 
    @Transactional 
    public String submitForm(@ModelAttribute("rf") RegistrationForm rf, Model model) throws Exception 
    { 
     // ... 

     User user = rf.getUser(); 
     userDao.save(user); 

     // ... 

     return "registration"; 
    } 
} 

私のフォームを送信すると、RegistrationFormの@Autowiredフィールドはnullのままです。

RegistrationForm.java:ここ

@Component 
public class RegistrationForm 
{ 
    @Autowired 
    CountryJpaDao countryDao; 

    // ... fields... 

    public RegistrationForm() 
    { 

    } 

    @Transactional 
    public User getUser() throws InvalidUserDataException 
    { 
     //... 

     Country c = countryDao.findByCode("GB"); // Throws java.lang.NullPointerException 

     // ... 
    } 

    // ... getters/setters... 
} 

は、フォームのHTML/JSTLです:

<form:form method="POST" modelAttribute="rf"> 
    ... 
</form:form> 

誰も私を助けることができますか?

ありがとうございます。 (SpringSourceのフォーラムでthis postに触発)

+0

春の設定applicationContext.xmlなどを貼り付けてください。 iddqd

答えて

1

あなたがここにあなたの概念を混同しています。 Spring管理Beanには@Component@Autowiredのようなものを使用し、フォームデータのバインドに使用される一時的な廃棄オブジェクトには@ModelAttributeを使用します。 2つは混在しないでください。 @Component@Autowired注釈がRegistrationFormにある場合は、そのコンテキストでは適切でないため、Springでは無視されます。

RegistrationFormなどのクラスは、フォームデータを表す必要があります。通常、コントローラはユーザIDとしてRegistrationFormを要求し、DAO自体から実際のUserオブジェクトを調べます。 RegistrationFormUserを検索するようにするには、Userオブジェクトを要求するときにコントローラが手作業でDAOをRegistrationFormに供給する必要があります。

Springフォーラムの投稿に関する限り、答えが返ってこなかったことに気づくでしょう。インスピレーションを得るには良い源ではありません。

フォームバックオブジェクトに豆をオートワイヤーしたいということは悪い考えであると言っているわけではありませんが、私は春がそれをしないと言っています。

+0

ありがとう@スカフマン - これは私が考えたものです。残念ながら、登録フォームのフィールドを直接Userクラスに「翻訳」することはできません。たとえば、登録フォームには生年月日(日、月、年)の3つのフィールドがあり、ユーザークラスは日時を使用します。これに春のソリューションはありますか?すべてのフォームフィールドをStringとして含むRegistrationFormクラスを作成し、getUser()メソッド(RegistrationFormインスタンスフィールドの読み込み)でUserインスタンスを返すことを望んでいましたが、Autowired Country DAOが必要です。ありがとうございました。 – satoshi

1

あなたのモデルに@Configurableアノテーションを使用し、場合、それは仕事とあなたのGradleファイルでこのAspectJの構成:AspectJの自動配線を行うコードを生成します。このように

compileJava << { 
    ant.taskdef(
     resource: 'org/aspectj/tools/ant/taskdefs/aspectjTaskdefs.properties', 
     classpath: configurations.compile.asPath) 

    ant.iajc(
     inpath: sourceSets.main.output.classesDir.absolutePath, 
      classpath: configurations.compile.asPath, 
      aspectPath: configurations.aspects.asPath, 
      destDir: sourceSets.main.output.classesDir.absolutePath 
    ) 
} 

@Configurable 
public class RegistrationForm 
{ 
    ... 
} 
関連する問題