簡體   English   中英

Spring驗證拋出錯誤提交事務

[英]Spring Validation throws Error Committing Transaction

更新根據以下建議,我已將

<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)

您還沒有設置僅jpa驗證。 您需要使用hibernate-validator進行驗證。

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

是驗證程序的依賴項。

BindingResultBindingResult必須直接跟隨@ModelAttribute注釋的屬性,因此也請在您的控制器方法中切換ModelMapBindingResult屬性。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM