2012-01-16 76 views
0

我在Spring 3上創建驗證表單,我的問題是,我看到很多帶驗證表單的例子。我甚至創建了一個。但我的表單傳遞「result.hasErrors()」方法,即使存在錯誤。使用Spring 3的驗證表單

我的代碼是:

控制器:

package com.esb.sso; 

import java.io.*; 
import java.net.HttpURLConnection; 
import java.net.URL; 
import java.net.URLEncoder; 

import javax.servlet.http.*; 

import org.slf4j.Logger; 
import org.slf4j.LoggerFactory; 
import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import org.springframework.validation.BindingResult; 
import java.util.Map; 

import javax.validation.Valid; 

import com.esb.sso.form.LoginForm; 
import javax.servlet.http.HttpServletRequest; 

/** 
* Handles requests for the application home page. 
*/ 
@Controller 
@RequestMapping(value = "/") 
public class HomeController { 

LoginForm loginForm = new LoginForm(); 

     model.put("loginForm", loginForm); 

     return "home"; 
    } 


@RequestMapping(method = RequestMethod.POST) 
public String validation(@Valid LoginForm loginForm, BindingResult result, 
     Map model) throws IOException { 

    logger.info("Login POST var"); 

    logger.info(loginForm.getLogin()); 
    logger.info(loginForm.getPassword()); 

    if (result.hasErrors()) { 
     logger.info("error"); 
     return "home"; 
    } 

    model.put("loginForm", loginForm); 

    return "logged"; 

} 

} 

驗證:

包com.esb.sso.form;

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

public class LoginForm { 

    @NotNull(message = "notNull") 
    @Size(min=1, max=50, message="mote charters") 
    private String login; 

    @NotNull(message = "notNull") 
    @Size(min=1, max=50, message="mote charters") 
    private String password; 

    public void setLogin(String login){ 
     this.login = login; 
    } 

    public String getLogin(){ 
     return login; 
    } 

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

    public String getPassword(){ 
     return password; 
    } 


} 

查看:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> 
<html> 
<head> 
    <title>Home</title> 
</head> 
<body> 
Autoryzacja!!! 
<form:form action="" commandName="loginForm"> 

<table> 

<tr> 

<td>User Name:<FONT color="red"><form:errors path="login" /></FONT></td> 

</tr> 

<tr> 

<td><form:input path="login" /></td> 

</tr> 

<tr> 

<td>Password:<FONT color="red"><form:errors path="password" /></FONT></td> 

</tr> 

<tr> 

<td><form:password path="password" /></td> 

</tr> 

<tr> 

<td><input type="submit" value="Submit" /></td> 

</tr> 

</table> 

</form:form> 
</body> 
</html> 

的web.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"> 

    <!-- The definition of the Root Spring Container shared by all Servlets and Filters --> 
    <context-param> 
     <param-name>contextConfigLocation</param-name> 
     <param-value>/WEB-INF/spring/root-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> 

    <!-- Processes application requests --> 
    <servlet> 
     <servlet-name>appServlet</servlet-name> 
     <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 

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

</web-app> 

的servlet上下文:

<?xml version="1.0" encoding="UTF-8"?> 
<beans:beans xmlns="http://www.springframework.org/schema/mvc" 
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
    xmlns:beans="http://www.springframework.org/schema/beans" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
     http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
     http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd"> 

    <!-- DispatcherServlet Context: defines this servlet's request-processing infrastructure --> 

    <!-- Enables the Spring MVC @Controller programming model --> 
    <annotation-driven /> 

    <!-- Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory --> 
    <resources mapping="/resources/**" location="/resources/" /> 

    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory --> 
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> 
     <beans:property name="prefix" value="/WEB-INF/views/" /> 
     <beans:property name="suffix" value=".jsp" /> 
    </beans:bean> 

    <context:component-scan base-package="com.esb.sso" /> 

    <beans:bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
     <beans:property name="basename" value="/WEB-INF/messages" /> 
    </beans:bean> 

</beans:beans> 

和messages.properties:

NotNull.loginForm.login=must not be blank. 
NotNull.loginForm.password=must not be blank. 
Size.loginForm.login=Login size must be between 1 and 50 charters. 
Size.loginForm.password=Password size must be between 1 and 50 charters. 

我不知道問題出在哪裏

Coul'd enybody幫助我 ?

回答

0

假設HomeController中的複製粘貼錯誤 - 「/」映射方法的get方法。

要回答您的問題 - 您必須指定LoginForm對象作爲模型屬性,以便將驗證綁定到BindingResult。下面是正確的代碼

public String validation((@ModelAttribute("loginForm") @Valid LoginForm loginForm, BindingResult result, 
     Map model) throws IOException { 

僅供參考看到documentation

+0

我已經添加@ModelAttribute,但什麼都沒有改變。我仍然沒有正確的驗證。 :( – Ilkar 2012-01-17 08:02:12

+0

你可以嘗試'@NotEmpty'註釋從Hibernate驗證器(hibernate-validator-4.0.2.GA.jar)而不是'@NotNull'。實際上可能是因爲'form:input'標籤提交了一個空值字符串,當你沒有指定任何輸入,從而通過@NotNull驗證。 – 2012-01-18 07:14:09

+0

這個改變什麼也沒做。我的項目是基於'http://www.roseindia.net/tutorial/spring/spring3/web/spring-3-mvc -validation-example.html'。當我下載該示例時,它的工作原理是正確的。但是當我嘗試用自己的方法構建它時,或者只是將文件從該示例複製到新項目時,表單無法驗證。驗證不起作用,每個表單都通過驗證,也許我應該嘗試其他示例? – Ilkar 2012-01-18 08:28:32