2011-10-17 89 views
0

我有一個名爲ParentAccount的JPA實體,它擴展了抽象Account實體(請參閱JPA繼承)。我已將JSR-303驗證約束放置在Account實體中。 現在我有以下的掛毯類和模板和JSR-303的驗證似乎不工作:關於JSR-303,Tapestry和JPA實體繼承的複雜用例

掛毯類:

public class Inscription {   
    @Property 
    //this is not validated... 
    private ParentAccount parentAccount; 

    @Property 
    @Validate("required") 
    private String accountPasswordConfirmation;  

    @InjectComponent 
    private Form registrationForm; 

    @OnEvent(EventConstants.PREPARE) 
    void prepareAccount(){ 
     parentAccount = new ParentAccount(); 
    } 

    @OnEvent(value= EventConstants.VALIDATE) 
    void validateRegistrationForm() { 
     if(registrationForm.isValid()) { 
      if(accountPasswordConfirmation.equals(parentAccount.getAccountPassword())) { 
       System.out.println("ok for insert"); 
      } 
     } 
    } 
} 

Tapestry頁面:

<html xmlns:t="http://tapestry.apache.org/schema/tapestry_5_1_0.xsd"> 
<head> 
    <title>Hello World Page</title> 
</head> 
<body> 
<form t:type="form" t:id="registrationForm" validate="this"> 
    <t:errors/> 
    <div> 
     <label t:type="label" for="accountEmailAddress"/> 
     <input t:type="textfield" t:id="accountEmailAddress" value="parentAccount.accountEmailAddress"/> 
    </div> 
    <div> 
     <label t:type="label" for="accountFirstName"/> 
     <input t:type="textfield" t:id="accountFirstName" value="parentAccount.accountFirstName"/> 
    </div> 
    <div> 
     <label t:type="label" for="accountLastName"/> 
     <input t:type="textfield" t:id="accountLastName" value="parentAccount.accountLastName"/> 
    </div> 
    <div> 
     <label t:type="label" for="accountPassword"/> 
     <input t:type="textfield" t:id="accountPassword" value="parentAccount.accountPassword"/> 
    </div> 
    <div> 
     <label t:type="label" for="accountPasswordConfirmation"/> 
     <input t:type="textfield" t:id="accountPasswordConfirmation" value="accountPasswordConfirmation"/> 
    </div> 
    <div><input type="submit" value="ok"/></div> 
</form> 
</body> 
</html> 

不幸的是,儘管我已經使用@NotNull註釋註釋了實體,那些JSR-303約束被忽略。

任何人都可以請幫忙嗎?

問候,

+0

我注意到,如果我將一個JSR-303註釋添加到Tapestry類中的屬性,它會考慮到。那麼爲什麼Tapestry在放入JPA實體的屬性時不會考慮註釋? – balteo 2011-10-17 15:44:54

回答

1

我注意到,如果我添加了一個JSR-303註釋的掛毯類的屬性,它得到考慮。那麼爲什麼Tapestry在放入JPA實體的屬性時不會考慮註釋?

在Tapestry中,一個表單與ValidationTracker相關,後者跟蹤表單中每個字段的所有提供的用戶輸入和驗證錯誤。

它似乎默認ValidationTrackerImpl不在乎。你可以通過Form的跟蹤器參數提供你的跟蹤器實現。我做了一些類似於Struts2的事情來驗證我的實體。

也許(希望)用@Valid註釋parentAccount(在JSR-303規範中調用圖驗證)也可以(提供Tapestry關心)。

+0

謝謝。我試圖添加@Valid註釋到parentAccount屬性無濟於事。我將嘗試其他建議的解決方案並相應地在此發佈。 – balteo 2011-10-19 18:55:58