java - 休眠验证标签不起作用。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>
解决方案
这里的相关解决方案: https ://stackoverflow.com/a/60667815/1911760
该问题与内置 IDE 的编译机制和库/工件管理有关。
推荐阅读
- javascript - 如何修复'在反应js中带有复选框的带有回车键的按键'
- rest - 使用 ServiceNow 表单进行出站邮寄电话
- java - handleWindowVisibility:令牌 android.os.BinderProxy 没有活动
- sql - 带有“not in”的 SQL 查询非常慢
- apache-spark - 为什么在向量的 Spark 数据帧上进行 MergeSort 连接后的不同计数会给出不确定的结果
- java - 匹配的通配符是严格的,但是在servlet.xml中找不到多个元素的声明
- javascript - 为什么 localStorage.getItem 总是返回字符串?
- wordpress - 如何将核心 WordPress Jquery 排入类
- xml - 如何使用PowerShell获取不是当前节点父节点的XML节点
- javascript - 如何点击延迟加载网页上的“显示更多”按钮