2016-04-17 5 views
0

Ich benutze validation-api-1.1.0.Final.jar zu gültiger Formulareingabe in Spring 4 MVC. Aber ich stecke hier fest, das Ergebnis. HasErrors() scheint immer falsch zu sein (es bedeutet, dass @Valid nicht richtig funktioniert). Ich folgte dieser Anleitung Validating Form Input, aber dieser Leitfaden Validierung Formulareingabe mit Spring Boot. Ich verwende Tomcat 8 anstelle von Spring Boot. Ich habe viel darüber gesucht, aber das Problem bestand immer noch. Seeking für Hilfe. Danke!Spring4 MVC Formularüberprüfung result.hasErrors() ist immer falsch

Projektstruktur:

Project Structure

UserController.java

package com.ro.user.controller; 

import com.ro.user.model.UserModel; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.Model; 
import org.springframework.validation.BindingResult; 
import org.springframework.web.bind.annotation.ModelAttribute; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import javax.validation.Valid; 

@Controller 
public class UserController { 
    @RequestMapping(value = "/user", method = RequestMethod.GET) 
    public UserModel user(Model model) { 

     UserModel userModel = new UserModel(); 
     model.addAttribute("userModel", userModel); 

     return userModel; 
    } 

    @RequestMapping(value = "/userLogin", method = RequestMethod.POST) 
    public String userLogin(@Valid @ModelAttribute("userModel") UserModel userModel, BindingResult bindingResult) { 
     System.out.println("Before: bindingResult.hasErrors()"); 
     if (bindingResult.hasErrors()) { 
      System.out.println("After: bindingResult.hasErrors()"); 
      return "user"; 
     } 

     return "result"; 
    } 
} 

UserModel.java

package com.ro.user.model; 

import javax.validation.constraints.NotNull; 
import javax.validation.constraints.Size; 

public class UserModel { 

    @NotNull 
    @Size(min = 6, max = 12) 
    private String username; 

    @Size(min = 8, max = 16) 
    private String password; 

    public String getUsername() { 
     return username; 
    } 

    public void setUsername(String username) { 
     this.username = username; 
    } 

    public String getPassword() { 
     return password; 
    } 

    public void setPassword(String password) { 
     this.password = password; 
    } 
} 

user.jsp

<%@ page contentType="text/html;charset=UTF-8" language="java" %> 
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %> 
<html> 
<head> 
    <title>Spring MVC Form Handling</title> 
    <style> 
     .error { 
      color: #ff0000; 
     } 

     .errorblock { 
      color: #000; 
      background-color: #ffEEEE; 
      border: 3px solid #ff0000; 
      padding: 8px; 
      margin: 16px; 
     } 
    </style> 
</head> 
<body> 

<h2>User Information</h2> 
<form:form action="userLogin" modelAttribute="userModel" method="POST"> 
    <form:errors path="*" cssClass="errorblock" element="div"/> 
    <table> 
     <tr> 
      <td><form:label path="username">Username:</form:label></td> 
      <td><form:input path="username"/></td> 
      <td><form:errors path="username" cssClass="error"/></td> 
     </tr> 
     <tr> 
      <td><form:label path="password">Password:</form:label></td> 
      <td><form:input path="password"/></td> 
      <td><form:errors path="username" cssClass="error"/></td> 
     </tr> 
     <tr> 
      <td colspan="3"> 
       <input type="submit" value="Submit"/> 
      </td> 
     </tr> 
    </table> 
</form:form> 
</body> 
</html> 

web.xml

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
     xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" 
     version="3.1"> 
    <listener> 
     <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> 
    </listener> 

    <servlet> 
     <servlet-name>UserLogin</servlet-name> 
     <servlet-class> 
      org.springframework.web.servlet.DispatcherServlet 
     </servlet-class> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

    <servlet-mapping> 
     <servlet-name>UserLogin</servlet-name> 
     <url-pattern>/</url-pattern> 
    </servlet-mapping> 

</web-app> 

Userlogin-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"> 

    <mvc:annotation-driven/> 

    <context:component-scan base-package="com.ro.user.controller"/> 
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <property name="prefix" value="/WEB-INF/jsp/"/> 
     <property name="suffix" value=".jsp"/> 
    </bean> 

    <bean class="org.springframework.context.support.ResourceBundleMessageSource" id="messageSource"> 
     <property name="basename" value="com.ro.user.model.error"/> 
    </bean> 

</beans> 

Antwort

2

Dieses Problem wird durch das Fehlen der Abhängigkeit von Hibernate Validator verursacht. Es soll alle Jars in einem Ordner namens benoted zur Projekt-lib hinzufügen (download zip von der offiziellen Website und entpacken Sie es, Sie werden diesen Ordner sehen). Nicht nur die hibernate-validator.jar und validation-api.jar.

1

Ich denke, Sie haben einen benutzerdefinierten HandlerAdapter erstellt, dann müssen Sie das WebBinding manuell initialisieren.

<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> 
    <property name="webBindingInitializer"> 
     <bean class="org.springframework.web.bind.support.ConfigurableWebBindingInitializer"> 
      <property name="validator" ref="validator"/> 
     </bean> 
    </property> 
</bean> 
Verwandte Themen