在我的Spring应用程序中,我必须提供“另存为草稿”功能。但是由于实体验证规则,我无法部分提交表单。我只想在单击“保存最终”按钮时验证表单,而不在单击“保存草稿”按钮时验证表单。我尝试删除@Valid批注,但仍然收到javax.validation.ConstraintViolationException错误。
控制器:
@PostMapping("/saveSalesDetails")
public String saveSales(Sales sales, @RequestParam("action") String formAction, BindingResult result, Model model) {
if(formAction.equalsIgnoreCase("Save Draft")) {
}
else if (formAction.equalsIgnoreCase("Save Final") {
}
这是堆栈跟踪:
Caused by: javax.validation.ConstraintViolationException: Validation failed for classes [mip.salesproj.model.Sales] during persist time for groups [javax.validation.groups.Default, ]
List of constraint violations:[
ConstraintViolationImpl{interpolatedMessage='Target Of Sale should be between 5-20 characters', propertyPath=targetSale, rootBeanClass=class mip.salesproj.model.Sales, messageTemplate='Target Of Sale should be between 5-20 characters'}
ConstraintViolationImpl{interpolatedMessage='Target Of Sale field should not be blank', propertyPath=targetSale, rootBeanClass=class mip.salesproj.model.Sales, messageTemplate='Target Of Sale field should not be blank'}
]
at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.validate(BeanValidationEventListener.java:140)
at org.hibernate.cfg.beanvalidation.BeanValidationEventListener.onPreInsert(BeanValidationEventListener.java:80)
at org.hibernate.action.internal.EntityInsertAction.preInsert(EntityInsertAction.java:205)
at org.hibernate.action.internal.EntityInsertAction.execute(EntityInsertAction.java:82)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:600)
at org.hibernate.engine.spi.ActionQueue.executeActions(ActionQueue.java:474)
at org.hibernate.event.internal.AbstractFlushingEventListener.performExecutions(AbstractFlushingEventListener.java:337)
at org.hibernate.event.internal.DefaultFlushEventListener.onFlush(DefaultFlushEventListener.java:39)
at org.hibernate.internal.SessionImpl.doFlush(SessionImpl.java:1437)
at org.hibernate.internal.SessionImpl.managedFlush(SessionImpl.java:494)
at org.hibernate.internal.SessionImpl.flushBeforeTransactionCompletion(SessionImpl.java:3245)
at org.hibernate.internal.SessionImpl.beforeTransactionCompletion(SessionImpl.java:2451)
at org.hibernate.engine.jdbc.internal.JdbcCoordinatorImpl.beforeTransactionCompletion(JdbcCoordinatorImpl.java:473)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.beforeCompletionCallback(JdbcResourceLocalTransactionCoordinatorImpl.java:156)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl.access$100(JdbcResourceLocalTransactionCoordinatorImpl.java:38)
at org.hibernate.resource.transaction.backend.jdbc.internal.JdbcResourceLocalTransactionCoordinatorImpl$TransactionDriverControlImpl.commit(JdbcResourceLocalTransactionCoordinatorImpl.java:231)
at org.hibernate.engine.transaction.internal.TransactionImpl.commit(TransactionImpl.java:68)
... 107 common frames omitted