2012-04-17 93 views
0

我在Spring Application.I中使用Spring MVC 3和Apache Tiles顯示錯誤消息時出現問題。當我在窗體中出現錯誤時,它不會顯示錯誤消息。 請幫我聽聽是這段代碼。Spring MVC 3 Validator not displayed錯誤消息

Web.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5"> 
    <display-name>SpringExample</display-name> 
    <welcome-file-list> 
    <welcome-file>index.html</welcome-file> 
    <welcome-file>index.htm</welcome-file> 
    <welcome-file>index.jsp</welcome-file> 
    <welcome-file>default.html</welcome-file> 
    <welcome-file>default.htm</welcome-file> 
    <welcome-file>default.jsp</welcome-file> 
    </welcome-file-list> 
    <servlet> 
     <servlet-name>dispatcher</servlet-name> 
     <servlet-class> 
      org.springframework.web.servlet.DispatcherServlet 
     </servlet-class> 
     <init-param> 
      <param-name>contextConfigLocation</param-name> 
      <param-value> 
       /WEB-INF/spring/dispatcher-servlet.xml 
      </param-value> 
     </init-param> 
     <load-on-startup>1</load-on-startup> 
    </servlet> 
    <servlet-mapping> 
     <servlet-name>dispatcher</servlet-name> 
     <url-pattern>/*</url-pattern> 
    </servlet-mapping> 

</web-app> 

調度-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="com.examples.spring" /> 


    <bean id="viewResolver" 
     class="org.springframework.web.servlet.view.UrlBasedViewResolver"> 
     <property name="viewClass"> 
      <value> 
       org.springframework.web.servlet.view.tiles2.TilesView 
      </value> 
     </property> 
    </bean> 

    <bean id="tilesConfigurer" 
     class="org.springframework.web.servlet.view.tiles2.TilesConfigurer"> 
     <property name="definitions"> 
      <list> 
       <value>/WEB-INF/tiles.xml</value> 
      </list> 
     </property> 
    </bean> 


</beans> 

tiles.xml

<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE tiles-definitions PUBLIC 
     "-//Apache Software Foundation//DTD Tiles Configuration 2.0//EN" 
     "http://tiles.apache.org/dtds/tiles-config_2_0.dtd"> 
<tiles-definitions> 


    <definition name="create_account_success" template="/WEB-INF/jsp/account/SuccessJSP.jsp"> 
    </definition> 

    <definition name="account_create" template="/WEB-INF/jsp/account/testForm.jsp"> 

    </definition> 


</tiles-definitions> 

testForm.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" 
    pageEncoding="ISO-8859-1"%> 
    <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> 
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> 
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"> 
<html> 
<head> 
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"> 
<title>Insert title here</title> 
</head> 
<body> 
    <form:form id="form" modelAttribute="formCreate" method="post"> 
    <table> 
     <tr> 
      <td>Name :</td> 
      <td><form:input path="name"/> 
       <form:errors path="name"/> 
      </td> 
     </tr> 
     <tr> 
      <td>Password :</td> 
      <td><form:input path="password"/> 
       <form:errors path="password"/> 
      </td> 
     </tr> 
     <tr> 
      <td colspan="2"><input type="submit" /></td> 
     </tr> 
    </table> 
    </form:form> 
</body> 
</html> 

AccountRegistrationController.java

package com.examples.spring.controller; 

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 com.examples.spring.dto.AccountDTO; 
import com.examples.spring.validator.AccountValidator; 

@Controller 
public class AccountRegistrationController { 


    public static final String BASE_URL = "/account"; 

    @RequestMapping(value = BASE_URL + "/create" ,method = RequestMethod.GET) 
    public ModelAndView accountCreate(){ 
     ModelAndView view = new ModelAndView(); 
     view.addObject("formCreate", new AccountDTO()); 
     view.setViewName("account_create"); 
     return view; 
    } 

    @RequestMapping(value = BASE_URL + "/create" ,method = RequestMethod.POST) 
    public ModelAndView accountRegistration(@ModelAttribute AccountDTO accountDTO,BindingResult result){ 

     ModelAndView view = new ModelAndView(); 

     AccountValidator validator = new AccountValidator(); 
     validator.validate(accountDTO, result); 
     if (result.hasErrors()) { 
      view.setViewName("account_create"); 
      view.addObject("formCreate", accountDTO); 
      return view; 
     } 
     view.setViewName("create_account_success"); 
     return view; 
    } 

    @RequestMapping(value = BASE_URL + "/login" ,method = RequestMethod.GET) 
    public ModelAndView accountLogin(){ 
     ModelAndView view = new ModelAndView(); 
     view.setViewName("account_login"); 
     return view; 
    } 
} 

AccountValidator.java

package com.examples.spring.validator; 

import org.springframework.validation.Errors; 
import org.springframework.validation.ValidationUtils; 
import org.springframework.validation.Validator; 

import com.examples.spring.dto.AccountDTO; 

public class AccountValidator implements Validator { 

    @Override 
    public boolean supports(Class<?> clazz) { 
     return AccountDTO.class.isAssignableFrom(clazz); 
    } 

    @Override 
    public void validate(Object result, Errors error) { 
     AccountDTO accountDTO = (AccountDTO) result; 

     ValidationUtils.rejectIfEmpty(error, "name", "require.name", "Name Requires."); 
     ValidationUtils.rejectIfEmpty(error, "password", "require.password", "Password Requires."); 
    } 

} 

AccountDTO.java

package com.examples.spring.dto; 

public class AccountDTO { 


    private String name; 
    private String password; 

    public AccountDTO() { 
     // TODO Auto-generated constructor stub 
    } 

    public String getName() { 
     return name; 
    } 
    public void setName(String name) { 
     this.name = name; 
    } 
    public String getPassword() { 
     return password; 
    } 
    public void setPassword(String password) { 
     this.password = password; 
    } 

} 

我的代碼工作良好,但它在<form:errors />不顯示錯誤消息。

請幫幫我。

+3

[Spring 3 MVC:使用自定義驗證程序顯示驗證消息]的可能重複(http://stackoverflow.com/questions/4031266/spring-3-mvc-show-validation-message-with-custom-validator) – axtavt 2012-04-17 16:44:56

+0

感謝axtvat這個鏈接幫助我。 – 2012-04-18 07:28:27

回答

3

ValidationUtils.rejectIfEmpty的第三個參數是錯誤代碼。它應該是您的資源包中實際消息的關鍵。如果它找不到價值,我不知道它的行爲。您應該將其更改爲使用密鑰,並可以選擇設置默認消息。請參閱api

+0

我已經嘗試使用'ValidationUtils.rejectIfEmpty(錯誤,「名稱」,「require.name」,「名稱需要」); \t \t ValidationUtils.rejectIfEmpty(error,「password」,「require.password」,「Password Requires。」); '但也仍然不起作用 – 2012-04-18 05:08:04

+0

我必須在控制器中設置@ModelAttribute(「formCreate」),而不是隻顯示錯誤消息。 – 2012-04-18 07:27:31