2013-09-26 32 views
0

UPDATE 基於下面的建議,我已經包括春天驗證拋出錯誤提交事務

<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 

description服務器遇到內部錯誤,導致無法完成此請求。如果我從產品對象中刪除驗證,一切正常(只是沒有驗證)。

例外:

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) 

回答

0

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

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

是驗證器的依賴關係。

下一步該BindingResult必須直接跟在@ModelAttribute註釋的屬性,所以在你的控制器方法也切換ModelMapBindingResult屬性。

+0

而且?它工作嗎,不是嗎?如果不是,請發佈完整的堆棧跟蹤,並請顯示spring mvc配置。 (並告訴我們您使用的是哪個Spring版本)。 –