为什么@vaild注释在我的Spring MVC应用程序中不起作用?我搜索了很多,但是找不到解决方案 [英] Why @vaild annotation is not working in my spring MVC application? I searched a lot, but couldn't find solution
本文介绍了为什么@vaild注释在我的Spring MVC应用程序中不起作用?我搜索了很多,但是找不到解决方案的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
基本上,如果该字段无效,它应该返回一条消息.但就我而言,它在这里不起作用!
Basically it should return a message if the field is not valid. but in my case it's not working here!!
下面是我的控制器类.
@Controller
public class StoreController {
private NearByStoreInterface getmestore;
@Inject
public StoreController(NearByStoreInterface getmestore) {
this.getmestore=getmestore;
}
@RequestMapping(value = "/nearbystore", method = RequestMethod.GET)
public String display(Model m)
{
final ControllerTolibModel userDetails=ControllerTolibModel.builder().build();
m.addAttribute("user", userDetails);
return "formTosubmit";
}
@RequestMapping(value="/nearbystore", method=RequestMethod.POST)
public String submit(@Valid @ModelAttribute("user") ControllerTolibModel userDetails,BindingResult errors,Model m)
{
if(errors.hasErrors())
return "formTosubmit";
final List<StoresDetails> storedetails=getmestore.getmeStore(userDetails);
if(storedetails.size()==0)
return "nostores";
m.addAttribute("details", storedetails);
return "viewStoreDetails";
}
}
这里@valid不起作用.也在其下面的controllerTolibModel的modelClass.
here @valid is not working. also below it's modelClass of controllerTolibModel .
package com.amazon.lib;
import javax.validation.constraints.Max;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotBlank;
import javax.validation.constraints.NotNull;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
@Data
@NoArgsConstructor
@AllArgsConstructor
@Builder
public class ControllerTolibModel {
@NotBlank
private String pincode;
@NotNull
@Min(1)
@Max(100)
private Double radius;
@NotNull
private String category;
}
这是一个formTosubmit.jsp文件.如果它可以帮助您了解更多.
Here it's a formTosubmit.jsp file. if it can help you understand more.
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<html>
<h2> Enter details here...</h2>
<body>
<form:form action="nearbystore" method="post" modelAttribute="user">
Enter your pincode : <form:input path="pincode"/><br><br>
<form:errors path="pincode" cssClass="text-danger"/>
Enter radius in KM : <form:input path="radius"/><br><br>
Select category of store : <form:select path="category">
<form:option value="all" label="all"/>
<form:option value="grocery" label="grocery"/>
<form:option value="garments" label="garments"/>
<form:option value="electronics" label="electronics"/>
<form:option value="Dairy" label="Dairy"/>
<form:option value="QSR" label="QSR"/>
</form:select><br><br>
<input type="submit" name="submit"/>
</form:form>
</form>
</body>
</html>
提前谢谢!
推荐答案
您需要实现 Bean验证 在您的类路径中,用于
You need to have an implementation of Bean Validation in your classpath for @Valid
to work. For example, add Hibernate Validator in your classpath.
Gradle
Gradle
compile group: 'org.hibernate.validator', name: 'hibernate-validator', version: '6.1.5.Final'
Maven
Maven
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
<version>6.1.5.Final</version>
</dependency>
这篇关于为什么@vaild注释在我的Spring MVC应用程序中不起作用?我搜索了很多,但是找不到解决方案的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文