Spring CrudRepository - 在JSF项目中保存之前进行验证

时间:2014-07-08 11:03:34

标签: spring jsf-2

我无法弄清楚为什么验证错误会在提交表单后显示而不是仅仅显示?我正在使用HIbernate,Spring和JSF。我在JSR 303中声明了验证,并使用Hibernate注释如下:

@Entity
@Table(name = "posts")
public class Post implements Serializable {
  @Column(name = "title")
    @NotEmpty(message = "Title should not be empty")
    private String title;
... other fields are ommited
}

在相应的bean中,我正在尝试保存帖子,如下所示:

@Component
@ManagedBean
@RequestScoped
@URLMappings(mappings = {
        @URLMapping(id = "posts", pattern = "/posts/", viewId = "/faces/posts/list.xhtml"),
        @URLMapping(id = "new", pattern = "/posts/new", viewId = "/faces/posts/new.xhtml")
})
public class PostBean {

    @Autowired
    private PostService postService;

    private List<Post> posts;

    private Post post = new Post();

    public List<Post> getPosts() {
        return postService.findAll();
    }

    public Post getPost() {
        return post;
    }

    public void setPost(Post post) {
        this.post = post;
    }

    public String create(Post post) {
        postService.save(post);
        return "pretty:posts";
    }
....
}

Post相关的存储库声明如下:

public interface PostRepository extends CrudRepository<Post, Long> {
    public Post findByTitleIgnoreCase(String title);
}

提交表单时,我收到以下错误:

Caused by: javax.faces.el.EvaluationException: javax.validation.ConstraintViolationException: Validation failed for classes [com.airial.domain.Post] during persist time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
    ConstraintViolationImpl{interpolatedMessage='Title should not be empty', propertyPath=title, rootBeanClass=class com.airial.domain.Post, messageTemplate='Title should not be empty'}
]
    at javax.faces.component.MethodBindingMethodExpressionAdapter.invoke(MethodBindingMethodExpressionAdapter.java:101)
    at com.sun.faces.application.ActionListenerImpl.processAction(ActionListenerImpl.java:102)
    ... 39 more
Caused by: javax.validation.ConstraintViolationException: Validation failed for classes [com.airial.domain.Post] during persist time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
    ConstraintViolationImpl{interpolatedMessage='Title should not be empty', propertyPath=title, rootBeanClass=class com.airial.domain.Post, messageTemplate='Title should not be empty'}
]
    at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:161)
    at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:94)
    at org.hibernate.action.EntityIdentityInsertAction.preInsert(EntityIdentityInsertAction.java:160)
    at org.hibernate.action.EntityIdentityInsertAction.execute(EntityIdentityInsertAction.java:65)
    at org.hibernate.engine.ActionQueue.execute(ActionQueue.java:273)
    at org.hibernate.event.def.AbstractSaveEventListener.performSaveOrReplicate(AbstractSaveEventListener.java:320)
    at org.hibernate.event.def.AbstractSaveEventListener.performSave(AbstractSaveEventListener.java:203)
    at org.hibernate.event.def.AbstractSaveEventListener.saveWithGeneratedId(AbstractSaveEventListener.java:129)
    at org.hibernate.ejb.event.EJB3PersistEventListener.saveWithGeneratedId(EJB3PersistEventListener.java:69)
    at org.hibernate.event.def.DefaultPersistEventListener.entityIsTransient(DefaultPersistEventListener.java:179)
    at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:135)
    at org.hibernate.event.def.DefaultPersistEventListener.onPersist(DefaultPersistEventListener.java:61)
    at org.hibernate.impl.SessionImpl.firePersist(SessionImpl.java:808)
    at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:782)
    at org.hibernate.impl.SessionImpl.persist(SessionImpl.java:786)
    at org.hibernate.ejb.AbstractEntityManagerImpl.persist(AbstractEntityManagerImpl.java:672)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at org.springframework.orm.jpa.SharedEntityManagerCreator$SharedEntityManagerInvocationHandler.invoke(SharedEntityManagerCreator.java:241)

有关如何修复它的想法,即在保存之前运行验证吗?

您可以在github repo找到该项目:https://github.com/Javix/blog-jsf

谢谢

0 个答案:

没有答案