Валидация у меня весеннего проекта не работает.Мне кажется, что 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>
Обновление: