2012-01-04 31 views
3

我使用Spring MVC的3.0.Here是我的控制器類:如何提供我選擇的消息在表單驗證失敗

@Controller 
@RequestMapping("/author") 
public class AuthorController { 
    @Autowired 
    private IAuthorDao authorDao; 

    @InitBinder 
    public void initBinder(WebDataBinder binder) { 
     SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd"); 
     binder.registerCustomEditor(Date.class, new CustomDateEditor(
       dateFormat, true)); 
    } 

    @RequestMapping(method = RequestMethod.GET) 
    public String get(Model model) { 
     return "author-list"; 
    } 

    @RequestMapping(value = "/new", method = RequestMethod.GET) 
    public String create(Model model) { 
     model.addAttribute("author", new Author()); 
     return "author-form"; 
    } 

    @RequestMapping(value = "/new", method = RequestMethod.POST) 
    public String createPost(@ModelAttribute("author") Author author, 
      BindingResult result) { 
     new AuthorValidator().validate(author, result); 
     if (result.hasErrors()) { 
      return "author-form"; 
     } else { 
      authorDao.persist(author); 
      return "redirect:/author/" + author.getId(); 
     } 
    } 

    @RequestMapping(value = "/{authorId}", method = RequestMethod.GET) 
    public String view(@PathVariable("authorId") int authorId) { 
     return "author-view"; 
    } 
} 

我試圖驗證筆者的對象。它有dob attr類型是Date。

我使用下面的類進行驗證:

public class AuthorValidator { 
    public void validate(Author author, Errors errors) { 

     if(author.getfName()==null) 
      errors.rejectValue("fName", "required", "required"); 
     else if (!StringUtils.hasLength(author.getfName())) { 
      errors.rejectValue("fName", "required", "required"); 
     } 

     if(author.getfName()==null) 
      errors.rejectValue("lName", "required", "required"); 
     else if (!StringUtils.hasLength(author.getlName())) { 
      errors.rejectValue("lName", "required", "required"); 
     } 

     if(author.getDob()==null) 
      errors.rejectValue("dob", "required", "required"); 
     else if (!StringUtils.hasLength(author.getDob().toString())) { 
      errors.rejectValue("dob", "required", "required"); 
     } 
    } 
} 

當我這樣做沒有輸入任何內容到它給需要的信息,這是正確的形式,但是當我給不正確的格式,那麼它給寫Failed to convert property value of type java.lang.String to required type java.util.Date for property dob; nested exception is java.lang.IllegalArgumentException: Could not parse date: Unparseable date: "asdads" required爲一個消息。如何使它像「無效的日期」。

感謝。

回答

5

如果使用標準的Spring設施,所有你需要做的是:

添加爲messageSource:

<bean id="messageSource" class="org.springframework.context.support.ResourceBundleMessageSource"> 
    <property name="basename" value="com.example.messages" /> 
</bean> 

添加屬性文件中包含的一個com/example/messages.properties

typeMismatch.author.date = Invalid date 
typeMismatch.date = Invalid date 
typeMismatch.java.util.Date = Invalid date 
typeMismatch = Invalid date 

首先讓你爲特定命令對象(author)配置特定字段(date)的錯誤消息,second-a date f可以在任何命令對象中使用,第三種 - 類別爲java.util.Date的字段的綁定錯誤的消息,以及最終 - 由任何轉換導致的錯誤的一般消息。

這裏是我的JSP頁面:

<%@ taglib uri="http://www.springframework.org/tags" prefix="s" %> 
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="sf" %> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> 
</head> 
<body> 
    <sf:form commandName="author"> 
     <sf:errors path="date" /><br /> 
     <sf:input path="name"/><br /> 
     <sf:input path="date"/><br /> 
     <input type="submit" value="ok" /> 
    </sf:form> 
</body> 
</html>