Java Spring验证在提交事务时抛出错误
更新 根据下面的建议,我将Java Spring验证在提交事务时抛出错误,java,spring,Java,Spring,更新 根据下面的建议,我将 <dependency> <groupId>org.hibernate</groupId> <artifactId>hibernate-validator</artifactId> <version>4.3.1.Final</version> </dependency> 结果是另一个例
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.1.Final</version>
</dependency>
结果是另一个例外:
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
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)
我试图让Spring进入一个验证表单字段。我已将Hibernate验证设置为依赖项:
<dependency>
<groupId>org.hibernate.javax.persistence</groupId>
<artifactId>hibernate-jpa-2.0-api</artifactId>
<version>1.0.1.Final</version>
</dependency>
在对象模型中,我有:
@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";
}
说明服务器遇到内部错误,无法满足此请求。如果我从产品对象中删除验证,一切都会正常工作(只是没有验证)
例外情况:
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
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验证器进行验证
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.1.Final</version>
</dependency>
org.hibernate
休眠验证器
4.3.1.最终版本
是验证程序的依赖项
接下来,
BindingResult
必须直接跟随@modeldattribute
注释属性,因此在控制器方法中也切换ModelMap
和BindingResult
属性。和?它起作用了吗?如果没有,请发布完整的stacktrace,并显示spring mvc配置。(并告诉我们您使用的是哪个Spring版本)。
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.3.1.Final</version>
</dependency>