问题
Basically it should return a message if the field is not valid. but in my case it's not working here!!
below is my controller class.
@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";
    }
}        
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;
}
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>
Thanks in advance !
回答1:
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
compile group: 'org.hibernate.validator', name: 'hibernate-validator', version: '6.1.5.Final'
Maven
<dependency>
    <groupId>org.hibernate.validator</groupId>
    <artifactId>hibernate-validator</artifactId>
    <version>6.1.5.Final</version>
</dependency>
来源:https://stackoverflow.com/questions/62248960/why-vaild-annotation-is-not-working-in-my-spring-mvc-application-i-searched-a