首页 > 解决方案 > 休眠验证标签不起作用。BindingResults 总是假的

问题描述

我的春季项目中的验证不起作用。我似乎BindingResult没有看到标签。我添加的库女巫列表: 在此处输入图像描述

当我使用调试运行应用程序时,我bindingResult.hasErrors()总是错误的。

我的学生类与字段:

public class Student {

    private String firstName;

    @NotNull(message = "is required")
    @Size(min = 1)
    private String lastName;
    // Other fields - ChoiceBoxes... GEtters&Setters.

我的控制器类:

@Controller
@RequestMapping("/student")
public class StudentController {

    @RequestMapping("/showForm")
    public String showForm(Model model) {

        Student student = new Student();
        model.addAttribute("student", student);

        return "st-form";
    }

    @RequestMapping("/processForm")
    public String processForm(
          @Valid @ModelAttribute("student") Student student,
            Model model, BindingResult bindingResult) {

        if (bindingResult.hasErrors()) {
            return "st-form";
        } else {

            String[] systems = student.getOperatingSystems();
            model.addAttribute("os", systems);

            return "st-conf";
        }
    }
}

我的spring-mvc-demo-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans
        http://www.springframework.org/schema/beans/spring-beans.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context.xsd
        http://www.springframework.org/schema/mvc
       http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">



    <!-- Step 3: Add support for component scanning -->
    <context:component-scan base-package="com.spring" />

    <!-- Step 4: Add support for conversion, formatting and validation support -->
    <mvc:annotation-driven/>

    <!-- Step 5: Define Spring MVC view resolver -->
    <bean
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/view/" />
        <property name="suffix" value=".jsp" />
    </bean>



</beans>

我的 .jsp 表单:

<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %>
<%--
  Created by IntelliJ IDEA.
  User: __it
  Date: 08.04.2019
  Time: 13:49
  To change this template use File | Settings | File Templates.
--%>
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
    <title>Stuent Registration Form</title>
    <style>
        .error{color: red}
    </style>
</head>
<body>
<form:form action="processForm" modelAttribute="student">
    First name: <form:input path="firstName"/>

    <br><br>

    Last name(*): <form:input path="lastName"/>
    <form:errors path="lastName" cssClass="error"/>
    <br><br>

    Country:

    <br><br>

    <%--Hardcode--%>
   <%-- <form:select path="country">
        <form:option value="UA" label="Ukraine"/>
        <form:option value="BRA" label="Brazil"/>
        <form:option value="ARG" label="Argentina"/>
        <form:option value="USA" label="USA"/>
        <form:option value="SPN" label="Spain"/>
        <form:option value="ITA" label="Italia"/>
    </form:select>--%>

    <form:select path="country">
        <form:options items="${student.countryOptions}"/>
    </form:select>


    <br><br>
    <p>Favorite language</p>
    Java <form:radiobutton path="favLnguage" value="Java"/>
    C# <form:radiobutton path="favLnguage" value="C#"/>
    Python <form:radiobutton path="favLnguage" value="Python"/>
    Ruby <form:radiobutton path="favLnguage" value="Ruby"/>

    <br><br>
    <p> Operating systems </p>
    Linux <form:checkbox path="operatingSystems" value="Linux"/>
    Mac OS <form:checkbox path="operatingSystems" value="MacOS"/>
    Windows <form:checkbox path="operatingSystems" value="Wimdows OS"/>
    <br><br>
    <input type="submit" value="Submit">

    <br><br>
</form:form>
</body>
</html>

更新: 在此处输入图像描述

标签: javaspringtomcathibernate-validator

解决方案


这里的相关解决方案: https ://stackoverflow.com/a/60667815/1911760

该问题与内置 IDE 的编译机制和库/工件管理有关。


推荐阅读