有关 JSR-303、Tapestry 和 JPA 实体继承的复杂用例
我有一个名为 ParentAccount
的 JPA 实体,它扩展了一个抽象 Account
实体(请参阅 JPA 继承)。我已将 JSR-303 验证约束放置在 Account 实体中。 现在我有以下 Tapestry 类和模板,并且 JSR-303 验证似乎不起作用:
Tapestry 类:
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 约束被忽略。
有人可以帮忙吗?
问候,
I have a JPA entity called ParentAccount
that extends an abstract Account
entity (see JPA inheritance). I have place the JSR-303 validation constraints in the Account entity.
Now I have the following Tapestry class and templates and JSR-303 validation does not seem to work:
Tapestry class:
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 page:
<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>
Unfortunately, even though I have annotated the entity with @NotNull
annotations, those JSR-303 constraints are ignored.
Can anyone please help?
Regards,
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在 Tapestry 中,表单由 ValidationTracker 关联它跟踪表单中每个字段的所有提供的用户输入和验证错误。
看来默认的 ValidationTrackerImpl 并不关心。您可以通过表单的跟踪器参数提供跟踪器实现。我做了一些与 Struts2 类似的事情来验证我的实体。
也许(希望)使用
@Valid
(JSR-303 规范中所称的图形验证)注释您的parentAccount
也有效(前提是 Tapestry 关心)。In Tapestry a Form is accociated by a ValidationTracker that tracks all the provided user input and validation errors for every field in the form.
It seems the default
ValidationTrackerImpl
doesn't care. You could provide your on tracker implementation via the Form's tracker parameter. I did something similiar with Struts2 to have my entities validated.Maybe (hopefully) annotating your
parentAccount
with@Valid
(graph validation as it is called in the JSR-303 specification) also works (provided Tapestry cares).