我的春季项目中的验证无效。我似乎BindingResult
没有看到标签。我添加的库女巫列表:
使用调试运行应用程序时,我的bindingResult.hasErrors()
始终为false。
我的学生课程包括以下字段:
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>
更新:
java参考方案
这里的相关解决方案:
https://stackoverflow.com/a/60667815/1911760
该问题与内置IDE的编译机制和库/工件管理有关。
页面加载而不是提交时发生struts验证 - java请原谅我;我对Struts有点陌生。我遇到一个问题,即页面加载而不是我实际提交表单时发生了验证。我整天都在论坛上搜寻和搜寻,没有任何运气。我显然做错了一些事情,应该很容易确定,但是我还没有发现问题所在。这是我的struts.xml的片段:<action name="*Test" method="{1}" clas…
DataSourceTransactionManager和JndiObjectFactoryBean和JdbcTemplate的用途是什么? - java以下的用途是什么:org.springframework.jdbc.core.JdbcTemplate org.springframework.jdbc.datasource.DataSourceTransactionManager org.springframework.jndi.JndiObjectFactoryBean <tx:annotatio…
Spring MVC拦截器映射问题 - java我有这段XML:<mvc:interceptors> <mvc:interceptor> <mvc:mapping path="/statics/**" /> <bean class="com.company.website.servlet.StaticsHandlerIntercept…
尝试在Spring MVC中使用OAuth保护资源 - java我们已经有在Spring MVC上用Java编写的REST Web服务,我一直在尝试保护它们。OAuth服务器是在另一个处理访问令牌登录和创建的网站中实现的。因此,在向用户授予对Web服务的访问权限之前,我需要验证访问令牌是否正确。但是,带有OAuth的Spring Security的文档似乎真的很差,并且示例代码实际上并未解释其作用!我什至不确定是否应该为…
AppCompat不支持当前主题 - java我的应用在Android N上运行正常,但在Android M上的setContentView(R.layout.activity_main)崩溃: Caused by: java.lang.IllegalArgumentException: AppCompat does not support the current theme features: { w…