0

更新 根据下面的建议,我已经包括

<dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-validator</artifactId>
            <version>4.3.1.Final</version>
        </dependency>

还将方法签名更改为:

public String updateProducts(@Valid  @ModelAttribute("updateProductsForm")    BindingResult result, UpdateProductsForm updateProductsForm, ModelMap modelMap) {

结果是一个不同的异常:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.beans.BeanInstantiationException: Could not instantiate bean class [org.springframework.validation.BindingResult]: Specified class is an interface

我正在尝试让 Spring 验证表单字段。我已将 Hibernate 验证设置为依赖项:

    <dependency>
      <groupId>org.hibernate.javax.persistence</groupId>
      <artifactId>hibernate-jpa-2.0-api</artifactId>
      <version>1.0.1.Final</version>
    </dependency>

然后在我的对象中,我有这个:

@javax.validation.constraints.NotNull
@javax.validation.constraints.Size(min=1,max=255)

在对象模型中,我有:

  @Column(
    name                                     = "description",
    nullable                                 = false
  )
  private String            description;

最后,在控制器中我有:

    public String updateProducts(
        @Valid @ModelAttribute("updateProductsForm") UpdateProductsForm updateProductsForm, 
        ModelMap modelMap, BindingResult result) {

        List<Product> products = (List<Product>) modelMap.get("products");

        if (result.hasErrors()) {

        modelMap.addAttribute("products", products);
        modelMap.addAttribute("errors", result.getAllErrors());

        return "updateProducts";
    }

我已经尝试了我能想到的一切,但无论我做什么,我都会在运行时遇到这个异常。

ype Exception report

message Request processing failed; nested exception is
org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; 
nested exception is javax.persistence.RollbackException: Error while committing the transaction

描述 服务器遇到一个内部错误,阻止它完成这个请求。如果我从 Product 对象中删除验证,一切正常(只是没有验证)。

例外:

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is org.springframework.transaction.TransactionSystemException: Could not commit JPA transaction; nested exception is javax.persistence.RollbackException: Error while committing the transaction
    org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:894)
    org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:789)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:647)
    javax.servlet.http.HttpServlet.service(HttpServlet.java:728)
    org.springframework.security.web.FilterChainProxy$VirtualFilterChain.doFilter(FilterChainProxy.java:330)
4

1 回答 1

0

您还没有设置验证只有 jpa。您需要 hibernate-validator 进行验证。

<dependency>
    <groupId>org.hibernate</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>4.3.1.Final</version>
</dependency>

是验证器的依赖项。

旁边BindingResult必须直接跟随带@ModelAttribute注释的属性,因此也要在控制器方法中切换ModelMap和属性。BindingResult

于 2013-09-26T17:35:16.827 回答