2012-09-16 100 views
0

我正在學習如何使用本教程進行簡單的表單驗證: http://www.raistudies.com/spring/spring-mvc/form-validation-spring-mvc-3-hibernate-validator-jsr-303/Spring MVC 3表單驗證註釋被忽略?

的問題是,當我輸入無效數據(空白字段),對用戶沒有任何錯誤的任何地方。 result.hasErrors()總是產生false。

我產生以下代碼:

IndexController.java:

package pl.aadamczyk.springtest.controllers; 

import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 
import pl.aadamczyk.springtest.beans.LoginBean; 

@Controller 
@RequestMapping("/index") 
public class IndexController 
{ 
    @RequestMapping(method = RequestMethod.GET) 
    public String getIndex(ModelMap model) 
    { 
     LoginBean loginBean = new LoginBean(); 
     model.addAttribute("loginBean", loginBean); 

     return "index"; 
    } 
} 

+ LoginController.java:

package pl.aadamczyk.springtest.controllers; 

import javax.validation.Valid; 
import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.ui.ModelMap; 
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 pl.aadamczyk.springtest.beans.LoginBean; 
import pl.aadamczyk.springtest.validators.LoginValidator; 

@Controller 
@RequestMapping("/login") 
public class LoginController 
{ 
    private LoginValidator loginValidator; 

    private LoginValidator getValidator() 
    { 
     return loginValidator; 
    } 

    @Autowired 
    public void setValidator(LoginValidator loginValidator) 
    { 
     this.loginValidator = loginValidator; 
    } 

    @RequestMapping(method= RequestMethod.POST) 
    public String login(@Valid @ModelAttribute(value="loginBean") LoginBean loginBean, 
         ModelMap model, 
         BindingResult result) 
    { 
     System.out.println(result.hasErrors()); // always false 

     loginValidator.validate(loginBean, result); 

     return "redirect:index.html"; 

    } 
} 

+ LoginValidator.java

package pl.aadamczyk.springtest.validators; 

import org.springframework.stereotype.Component; 
import org.springframework.validation.Errors; 
import org.springframework.validation.Validator; 
import pl.aadamczyk.springtest.beans.LoginBean; 

@Component 
public class LoginValidator implements Validator 
{ 
    @Override 
    public boolean supports(Class<?> type) 
    { 
     return LoginBean.class.isAssignableFrom(type); 
    } 

    @Override 
    public void validate(Object o, Errors errors) 
    { 

    } 
} 

LoginBean.java:

<form:form method="post" action="login.html" modelAttribute="loginBean"> 
    <table> 
     <tr> 
      <td>Login:</td> 
      <td><form:input path="username"/></td> 
      <td><form:errors path="username"/></td> 
     </tr> 
     <tr> 
      <td>Password</td> 
      <td><form:input path="password"/></td> 
      <td><form:errors path="password"/></td> 
     </tr> 
     <tr> 
      <td colspan="3"><button type="submit">Login</button></td> 
     </tr> 
    </table> 
</form:form> 
調度員的servlet

重要內容:

<context:annotation-config /> 
<context:component-scan base-package="pl.aadamczyk.springtest"/> 
<mvc:annotation-driven/> 

<bean id="viewResolver" 
     class="org.springframework.web.servlet.view.InternalResourceViewResolver" 
     p:prefix="/WEB-INF/jsp/" 
     p:suffix=".jsp" /> 

pom.xml的依賴關係:

<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-aop</artifactId> 
    <version>3.1.1.RELEASE</version> 
</dependency> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-asm</artifactId> 
    <version>3.1.1.RELEASE</version> 
</dependency> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-aspects</artifactId> 
    <version>3.1.1.RELEASE</version> 
</dependency> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-beans</artifactId> 
    <version>3.1.1.RELEASE</version> 
</dependency> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-context</artifactId> 
    <version>3.1.1.RELEASE</version> 
</dependency> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-context-support</artifactId> 
    <version>3.1.1.RELEASE</version> 
</dependency> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-core</artifactId> 
    <version>3.1.1.RELEASE</version> 
</dependency> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-expression</artifactId> 
    <version>3.1.1.RELEASE</version> 
</dependency> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-instrument</artifactId> 
    <version>3.1.1.RELEASE</version> 
</dependency> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-instrument-tomcat</artifactId> 
    <version>3.1.1.RELEASE</version> 
</dependency> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-jdbc</artifactId> 
    <version>3.1.1.RELEASE</version> 
</dependency> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-jms</artifactId> 
    <version>3.1.1.RELEASE</version> 
</dependency> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-orm</artifactId> 
    <version>3.1.1.RELEASE</version> 
</dependency> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-oxm</artifactId> 
    <version>3.1.1.RELEASE</version> 
</dependency> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-test</artifactId> 
    <version>3.1.1.RELEASE</version> 
</dependency> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-tx</artifactId> 
    <version>3.1.1.RELEASE</version> 
</dependency> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-web</artifactId> 
    <version>3.1.1.RELEASE</version> 
</dependency> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-webmvc-portlet</artifactId> 
    <version>3.1.1.RELEASE</version> 
</dependency> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-webmvc</artifactId> 
    <version>3.1.1.RELEASE</version> 
</dependency> 
<dependency> 
    <groupId>org.springframework</groupId> 
    <artifactId>spring-struts</artifactId> 
    <version>3.1.1.RELEASE</version> 
</dependency> 
<dependency> 
    <groupId>javax.servlet</groupId> 
    <artifactId>jstl</artifactId> 
    <version>1.1.2</version> 
</dependency> 
<dependency> 
    <groupId>taglibs</groupId> 
    <artifactId>standard</artifactId> 
    <version>1.1.2</version> 
</dependency> 
<dependency> 
    <groupId>org.hibernate</groupId> 
    <artifactId>hibernate-validator</artifactId> 
    <version>3.0.0.ga</version> 
    <type>jar</type> 
</dependency> 
<dependency> 
    <groupId>javax</groupId> 
    <artifactId>javaee-web-api</artifactId> 
    <version>6.0</version> 
    <scope>provided</scope> 
</dependency> 

package pl.aadamczyk.springtest.beans; 

import org.hibernate.validator.Length; 
import org.hibernate.validator.NotEmpty; 

public class LoginBean 
{ 
    @NotEmpty(message="Username is mandatory.") 
    @Length(min=3,max=25,message="Username must be between 3 and 25 characters length") 
    private String username; 

    @NotEmpty(message="Password field is mandatory.") 
    @Length(min=3,max=25,message="Password must be between 5 and 25 characters length") 
    private String password; 

    // getters+setters skipped 
} 

的index.jsp的部分

+1

你確定在編譯之後你的類路徑中有hibernate-validator嗎? –

+0

我已經將hibernate-validator-4.1.0.Final.jar添加到WEB-INF/lib文件夾中,無需更改。 Hibernate-validator-3.0.0.ga.jar在Mavens的依賴中。我在第一篇文章中包含了我的pom.xml。 –

+0

好的,我已經包含了ant-apache-log4j-1.6.5,hibernate-validator-4.3.0.Final,slf4j-api-1.6.6,slf4j-log4j12-1.6.6到我的依賴項,它似乎工作正常現在。另外,我必須將BindingResult結果更接近LoginBean loginBean參數。 result.hasErrors()現在返回true,但html中仍然沒有消息。 –

回答

1

要獲得驗證工作,請確保在類路徑中有近期版本的hibernate-validator

要打印中的錯誤JSP頁面,使用類似下面的

<%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %> 
... 

<spring:hasBindErrors name="person"> 
    <div class="error"> 
     <c:forEach var="error" items="${errors.allErrors}"> 
      <p>Errors ${error.defaultMessage}</p> 
     </c:forEach> 
    </div> 
</spring:hasBindErrors> 

您還需要在類路徑中jstlspring-webmvc庫。

+0

但是我沒有使用你的解決方案,你的帖子爲我提供了食物。我通過(重新)將loginBean添加到模型中,並將返回值從「redirect:index」更改爲「index」來解決顯示窗體錯誤的問題。謝謝。 –

1

BindingResult必須在驗證了模型對象後立即來到否則將無法​​驗證的對象,並拋出一個異常。

看到@Valid,它試圖找到驗證爲對象進行驗證。如果您啓用了註釋驅動,Spring將自動提取驗證註釋。 Spring然後調用驗證器並將任何錯誤放入BindingResult並將BindingResult添加到視圖模型。