我尝试将JSF与Bean验证结合使用。基本上,一切都很好,验证工作如预期,我得到了正确的消息,但我的Glassfish控制台上有一个例外:
Warnung: EJB5184:A system exception occurred during an invocation on EJB MyEntityFacade, method: public void com.mycompany.testbv.AbstractFacade.create(java.lang.Object)
Warnung: javax.ejb.EJBException
at com.sun.ejb.containers.EJBContainerTransactionManager.processSystemException(EJBContainerTransactionManager.java:748)
....
....
at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:544)
at java.lang.Thread.run(Thread.java:744)
Caused by: javax.validation.ConstraintViolationException: Bean Validation constraint(s) violated while executing Automatic Bean Validation on callback event:'prePersist'. Please refer to embedded ConstraintViolations for details.
如果使用自定义约束和预定义约束,则会出现此异常。
这是我的示例代码。
样本实体:
@Entity
@ValidEntity
public class MyEntity implements Serializable {
private static final long serialVersionUID = 3104398374500914142L;
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@Size(min = 2)
private String name;
public MyEntity(String name) {
this.name = name;
}
public MyEntity() {
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
自定义约束:
@Constraint(validatedBy = MyValidator.class)
@Target({FIELD, METHOD, TYPE})
@Retention(RUNTIME)
public @interface ValidEntity {
String message() default "fail";
Class<?>[] groups() default {};
Class<? extends Payload>[] payload() default {};
}
自定义验证器:
public class MyValidator implements ConstraintValidator<ValidEntity, MyEntity>{
@Override
public void initialize(ValidEntity a) {
}
@Override
public boolean isValid(MyEntity t, ConstraintValidatorContext cvc) {
return false;
}
}
样品控制器:
@Named
@SessionScoped
public class MyController implements Serializable {
private static final long serialVersionUID = -6739023629679382999L;
@Inject
MyEntityFacade myEntityFacade;
String text;
public String getText() {
return text;
}
public void setText(String text) {
this.text = text;
}
public void saveNewEntity() {
try {
myEntityFacade.create(new MyEntity(text));
} catch (Exception e) {
Throwable t = e;
while (t != null) {
if (t instanceof ConstraintViolationException) {
FacesContext context = FacesContext.getCurrentInstance();
Set<ConstraintViolation<?>> constraintViolations = ((ConstraintViolationException) t).getConstraintViolations();
for (ConstraintViolation<?> constraintViolation : constraintViolations) {
FacesMessage facesMessage = new FacesMessage(constraintViolation.getMessage());
facesMessage.setSeverity(FacesMessage.SEVERITY_ERROR);
context.addMessage(null, facesMessage);
}
}
t = t.getCause();
}
}
}
}
jsf页面示例:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://xmlns.jcp.org/jsf/html">
<h:head></h:head>
<h:body>
<h:form>
<h:messages id="messages" />
<h:inputText value="#{myController.text}" />
<h:commandButton value="Save" action="#{myController.saveNewEntity()}" />
</h:form>
</h:body>
</html>
MyEntityFacade仅从实体管理器调用persistent。
如前所述,应用程序运行良好,正确的消息为shwon,但我希望在Glassfish控制台中避免出现此异常。
将persistence.xml中的验证模式设置为NONE是没有选择的,因为我想要一个验证。
我在2.2版本中使用JSF,实现是Mojarra。Bean Validation的版本是1.1,实现是Hibernate Validator。应用程序服务器是Glassfish 4.0。
类级约束不适用于JSF。看看这个答案。当您按下"保存"按钮时,JSF只检查名称是否至少有2个字符,并且不考虑ValidEntity约束。另一方面,JPA抱怨bean无效并抛出异常。
更新
1) @Size约束在MyEntity.name
属性上,而在facelet中则具有MyController.text
属性。从JSF的角度来看,没有什么可验证的。它根本不知道MyEntity。
2) ValidEntity总是无效的,因此即使在facelet中正确设置了MyEntity.name
,JPA也总是抛出异常(除非禁用验证)。