关于JSR-303,Tapestry和JPA实体继承的复杂用例
我有一个名为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约束被忽略。
任何人都可以请帮忙吗?
问候,
我注意到,如果我添加了一个JSR-303注释的挂毯类的属性,它得到考虑。那么为什么Tapestry在放入JPA实体的属性时不会考虑注释?
在Tapestry中,一个表单与ValidationTracker相关,后者跟踪表单中每个字段的所有提供的用户输入和验证错误。
它似乎默认ValidationTrackerImpl
不在乎。你可以通过Form的跟踪器参数提供你的跟踪器实现。我做了一些类似于Struts2的事情来验证我的实体。
也许(希望)用@Valid
注释parentAccount
(在JSR-303规范中调用图验证)也可以(提供Tapestry关心)。
谢谢。我试图添加@Valid注释到parentAccount属性无济于事。我将尝试其他建议的解决方案并相应地在此发布。 – balteo 2011-10-19 18:55:58
我注意到,如果我将一个JSR-303注释添加到Tapestry类中的属性,它会考虑到。那么为什么Tapestry在放入JPA实体的属性时不会考虑注释? – balteo 2011-10-17 15:44:54