gpt4 book ai didi

Spring MVC 表单验证不起作用

转载 作者:行者123 更新时间:2023-12-02 03:11:44 28 4
gpt4 key购买 nike

我正在使用 Spring 4。我的表单包含以下变量:

@NotNull
@Email
private String email;
@NotNull
private String firstName;
@NotNull
private String lastName;
@Digits(fraction = 0, integer = 10)
private String phoneNo;
@NotNull
private String role;

我的 Controller :

@Controller
@RequestMapping("/user")
public class UserController {

@RequestMapping(value = "/add", method = RequestMethod.POST)
public ModelAndView add(@ModelAttribute("user") @Valid UserBean user, BindingResult result) {
String message;
if (result.hasErrors() && user != null)
return new ModelAndView("userAdd");
else {
userService.addUser(user);
message = "Successfully added user";
}
return new ModelAndView("success", "message", message);
}

@RequestMapping(value = "/register")
public ModelAndView register(@ModelAttribute("user") UserBean user, BindingResult result) {
List<String> roles = new ArrayList<String>();
roles.add("Receiver");
roles.add("Resolver");
roles.add("Logger");
Map<String, List<String>> model = new HashMap<String, List<String>>();
model.put("roles", roles);
return new ModelAndView("userAdd", "model", model);
}
}

我的jsp:

<c:url var="userAdd" value="user/add.do" />
<sf:form method="post" action="${userAdd}" modelAttribute="user">
<table>
<tr>
<td>First Name</td>
<td><sf:input path="firstName" /><br /> <sf:errors
path="firstName" cssClass="error" /></td>
</tr>
<tr>
<td>Last Name</td>
<td><sf:input path="lastName" /><br /> <sf:errors
path="lastName" cssClass="error" /></td>
</tr>
<tr>
<td>Email</td>
<td><sf:input path="email" /><br /> <sf:errors
path="email" cssClass="error" /></td>
</tr>
<tr>
<td>Phone No.</td>
<td><sf:input path="phoneNo" /><br /> <sf:errors
path="phoneNo" cssClass="error" /></td>
</tr>
<tr>
<td>Role</td>
<td><sf:select path="role" items="${model.roles}" /><br /> <sf:errors
path="role" cssClass="error" /></td>
</tr>
<tr>
<td><input type="submit" value="Submit" /></td>
</tr>
</table>
</sf:form>

当我将输入留空时,表单不会验证并且不会抛出任何错误。 BindingResult 中没有任何错误。

我的图书馆是: Libraries

我的dispatcher-servet.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/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd
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">
<!-- Scans for annotated @Controllers in the classpath -->
<context:component-scan base-package="com.mj.cchp.controller">
<context:include-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<bean id="myBeansValidator"
class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />
<mvc:annotation-driven validator="myBeansValidator" />
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix">
<value>/WEB-INF/pages/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>

我的applicationContext是:

<?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"
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">
<!-- Scans for annotated @Controllers in the classpath -->
<context:component-scan base-package="com.mj.cchp">
<context:exclude-filter type="annotation"
expression="org.springframework.stereotype.Controller" />
</context:component-scan>
<bean id="dataSource"
class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.ibm.db2.jcc.DB2Driver" />
<property name="url" value="jdbc:db2://172.16.2.181:60000/CCHP" />
<property name="username" value="db2inst1" />
<property name="password" value="db2inst1" />
</bean>
</beans>

我的web.xml是:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" version="3.0">
<display-name>CCHP</display-name>
<welcome-file-list>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/application-context.xml</param-value>
</context-param>
<!-- Creates the Spring Container shared by all Servlets and Filters -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
</web-app>

最佳答案

您需要添加

<bean id="myBeansValidator" class="org.springframework.validation.beanvalidation.LocalValidatorFactoryBean" />

<mvc:annotation-driven validator="myBeansValidator">

    <!-- Hibernate Validator -->
<dependency>
<groupId>org.hibernate</groupId>
<artifactId>hibernate-validator</artifactId>
<version>4.2.0.Final</version>
</dependency>

应该可以!

关于Spring MVC 表单验证不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21478299/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com