2010-10-27 117 views
16

我需要幫助。我是jsp,MVC的初學者。我想在Spring 3 MVC中使用自定義驗證器驗證表單輸入。Spring 3 MVC:使用自定義驗證器顯示驗證消息

我的驗證器類

package validators; 

import models.UserModel; 

import org.springframework.stereotype.Component; 
import org.springframework.validation.Errors; 
import org.springframework.validation.ValidationUtils; 
import org.springframework.validation.Validator; 
@Component 
public class UserValidator implements Validator { 

    @Override 
    public boolean supports(Class clazz) { 
     return UserModel.class.isAssignableFrom(clazz); 
    } 

    @Override 
    public void validate(Object target, Errors errors) { 
     ValidationUtils.rejectIfEmptyOrWhitespace(errors, "firstname", "Enter firstname."); 
     ValidationUtils.rejectIfEmptyOrWhitespace(errors, "surname", "Enter surname."); 
     ValidationUtils.rejectIfEmptyOrWhitespace(errors, "login", "Enter login."); 

    } 

} 

控制器類用戶

package controllers; 

import java.util.ArrayList; 
import models.UserModel; 
import org.springframework.stereotype.Controller; 
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 org.springframework.web.servlet.ModelAndView; 
import validators.UserValidator; 
import database.UserDB; 


@Controller 
public class UserController { 

@RequestMapping(value="pouzivatel/new", method=RequestMethod.POST) 
    public ModelAndView newUser(@ModelAttribute UserModel user, BindingResult result){ 
     UserValidator validator = new UserValidator(); 
     validator.validate(user, result); 
     if(result.hasErrors()){ 
     return new ModelAndView("/user/new","command",user); 

     } 
     ... 
} 

模型

package models; 

public class UserModel { 
    private String firstname=""; 
    private String surname=""; 

    public String getFirstname() { 
     return firstname; 
    } 
    public String getSurname() { 
     return surname; 
    } 

    public void setFirstname(String firstname) { 
     this.firstname = firstname; 
    } 
    public void setSurname(String surname) { 
     this.surname = surname; 
    } 

} 

JSP教職員new.jsp這是在目錄/ WEB-INF /用戶(它只是形式)

<form:form method="post" action="new.html"> 
      <fieldset> 
       <table> 
        <tr> 
        <td> 
         <form:label path="firstname">FirstName</form:label> 
        </td> 
        <td> 
         <form:input path="firstname" /> 
         <form:errors path="firstname" /> 
        </td> 
        </tr> 
        <tr> 
        <td> 
         <form:label path="surname">Surname</form:label> 
        </td> 
        <td> 
         <form:input path="surname" /> 
         <form:errors path="surname" /> 
        </td> 
        </tr> 
       </table> 
      </fieldset> 
      <div> 
       <button type="submit" id="btOk">Ok</button> 
      </div> 
</form:form> 

調度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:p="http://www.springframework.org/schema/p" 
    xmlns:context="http://www.springframework.org/schema/context" 
    xsi:schemaLocation="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"> 

    <context:component-scan base-package="controllers" /> 
    <context:component-scan base-package="validators" /> 

    <bean id="viewResolver" class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 
     <property name="viewClass" value="org.springframework.web.servlet.view.JstlView" /> 
     <property name="prefix" value="/WEB-INF/jsp/" /> 
     <property name="suffix" value=".jsp" /> 
    </bean> 

</beans> 

問題是在視圖的顯示驗證消息。驗證成功並且在變量resut(BindingResult)中有錯誤。控制器返回遵循的代碼部分

if(result.hasErrors()){ 
     return new ModelAndView("/user/new","command",user); 

另一種方法是使用註釋驗證(我preffer自定義驗證),但爲什麼我不能看到視圖驗證消息,當輸入字段爲空。

你能舉個例子怎麼做對嗎?

感謝您的回覆。

回答

20

這是因爲默認模型之間的不匹配的屬性在視圖和控制器的名字:

  • 當你寫<form:form>沒有modelAttribute(或commandName)屬性,它使用默認的模型屬性名稱command
  • 當您在您的控制器中編寫@ModelAttribute UserModel user時,它假定此屬性的名稱是一個非資產類名稱,即userModel

也就是說,通過驗證產生的錯誤消息被綁定到模型命名userModel屬性,而你的觀點試圖顯示錯誤的模型屬性command

您需要在視圖(<form:form modelAttribute = "userModel" ...>)或控制器(@ModelAttribute("command"))中明確設置模型屬性名稱。

+0

非常感謝,現在正在工作。 – Jerry 2010-10-27 15:15:13