spring 验证无效。当我使用 entitymanager.persist(); 提交空白表格时

spring validation is not working. when i submit the blank form using entitymanager.persist();

添加 user.But 没有问题,当我提交空白表格时我没有收到任何错误。请帮我解决这个问题。我在 pom 文件中添加了 hibernate-validator(5.4.0.Final) 依赖项。以下是您可以检查并告诉我哪里错了的代码。我正在使用 entitymanager.persist() 方法

实体Class @实体 @Table(名字="USERS")

public class Users {


    @Id
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "users_generator")
    @SequenceGenerator(name="users_generator", sequenceName = "SEQ_USERS", allocationSize=1)
    @Column(name="USER_ID")
    private Integer userId;

    @Column(name="LOGIN_NAME")
    private String loginName;

    @NotNull(message="*required")
    @Column(name="FIRST_NAME")
    private String firstName;

    @NotNull
    @Column(name="LAST_NAME")
    private String lastName;


Controller.page
@GetMapping("/user")
    public ModelAndView landingPage(Model model) {

        model.addAttribute("userList", userService.findAllUsers());
        Users user=new Users();
        user.getRoles().addAll(userRoleService.findAllUserRoles());
        model.addAttribute("user", user);
        //model.addAttribute("searchRole", userRoleService.findAllUserRoles());
        return new ModelAndView(landingPage);
    }


    @PostMapping("/user")
    public ModelAndView processRegistrationForm(@ModelAttribute("user") @Valid Users user,BindingResult result) {
        if (result.hasErrors()) {
            return new ModelAndView(landingPage);
        } else {
            try {

                userService.saveUser(user);
            }catch (Exception e) {
                e.printStackTrace();
            }
            return new ModelAndView("redirect:/user");
        }
    }

home.jsp

<c:url value="/user" var="registerUrl" />
                <form:form action="${registerUrl}" modelAttribute="user" method="post">
                    <div class="form-group">
                        <label>First Name: <form:errors path="firstName" cssClass="error" /></label>
                        <form:input path="firstName" cssClass="form-control" />
                    </div>
                    <div class="form-group">
                        <label>Last Name: <form:errors path="lastName" cssClass="error" /></label>
                        <form:input path="lastName" cssClass="form-control" />
                    </div>
                    <div class="form-group">
                        <label>Email: <form:errors path="mailId" cssClass="error" /></label>
                        <form:input path="mailId" cssClass="form-control" />
                    </div>
                    <div class="form-group">
                        <label>Password: <form:errors path="password" cssClass="error" /></label>
                        <form:input path="password" type="password" cssClass="form-control" />
                    </div>

web.xml

<mvc:annotation-driven>
        <mvc:message-converters>
            <bean class="org.springframework.http.converter.StringHttpMessageConverter" />
            <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
        </mvc:message-converters>
    </mvc:annotation-driven>

    <context:component-scan base-package="com.genpact.cvs.*" />
    <mvc:resources location="/resources/" mapping="/resources/**" />

    <aop:aspectj-autoproxy />

    <!-- <bean id="myBeansValidator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" /> -->
    <bean
        class="org.springframework.web.servlet.view.ResourceBundleViewResolver">
        <property name="basename" value="spring-views" />
        <property name="order" value="0" />
    </bean>
    <bean id="multipartResolver"
        class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
    </bean>

    <bean class="org.springframework.web.servlet.view.XmlViewResolver">
        <property name="location" value="/WEB-INF/spring-views.xml" />
        <property name="order" value="1" />
    </bean>

    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/views/" />
        <property name="suffix" value=".jsp" />
        <property name="order" value="2" />
    </bean>

试试看。也许你有空字符串(不为空)

@NotNull(message="*required")
@Size(min=1)
@Column(name="FIRST_NAME")
private String firstName;

@NotNull
@Size(min=1)
@Column(name="LAST_NAME")
private String lastName;