2014-10-06 18 views
0

我在這裏介紹http://docs.spring.io/docs/Spring-MVC-step-by-step/在這裏描述的一步一步的web應用程序。如何在春季更改錯誤消息?

<?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:context="http://www.springframework.org/schema/context" 
     xmlns:mvc="http://www.springframework.org/schema/mvc" 
     xsi:schemaLocation="http://www.springframework.org/schema/beans 
     http://www.springframework.org/schema/beans/spring-beans.xsd 
     http://www.springframework.org/schema/context 
     http://www.springframework.org/schema/context/spring-context.xsd 
     http://www.springframework.org/schema/mvc 
     http://www.springframework.org/schema/mvc/spring-mvc.xsd"> 

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

     <!-- Scans the classpath of this application for @Components to deploy as beans --> 
     <context:component-scan base-package="com.companyname.springapp.web" /> 

     <!-- Configures the @Controller programming model --> 
     <mvc:annotation-driven/> 

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

<%@ include file="/WEB-INF/views/include.jsp" %> 
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %> 

<html> 
<head> 
    <title><fmt:message key="title"/></title> 
    <style> 
    .error { color: red; } 
    </style> 
</head> 
<body> 
<h1><fmt:message key="priceincrease.heading"/></h1> 
<form:form method="post" commandName="priceIncrease"> 
    <table width="95%" bgcolor="f8f8ff" border="0" cellspacing="0" cellpadding="5"> 
    <tr> 
     <td align="right" width="20%">Increase (%):</td> 
     <td width="20%"> 
      <form:input path="percentage"/> 
     </td> 
     <td width="60%"> 
      <form:errors path="percentage" cssClass="error"/> 
     </td> 
    </tr> 
    </table> 
    <br> 
    <input type="submit" value="Execute"> 
</form:form> 
<a href="<c:url value="hello.htm"/>">Home</a> 
</body> 
</html> 

package com.companyname.springapp.service; 

import javax.validation.constraints.Max; 
import javax.validation.constraints.Min; 

import org.apache.commons.logging.Log; 
import org.apache.commons.logging.LogFactory; 

public class PriceIncrease { 

    /** Logger for this class and subclasses */ 
    protected final Log logger = LogFactory.getLog(getClass()); 

    @Min(0) 
    @Max(50) 
    private int percentage; 

    public void setPercentage(int i) { 
     percentage = i; 
     logger.info("Percentage set to " + i); 
    } 

    public int getPercentage() { 
     return percentage; 
    } 
} 

package com.companyname.springapp.web; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Controller; 
import org.springframework.validation.BindingResult; 
import org.springframework.web.bind.annotation.RequestMapping; 
import org.springframework.web.bind.annotation.RequestMethod; 

import javax.servlet.ServletException; 
import javax.servlet.http.HttpServletRequest; 
import javax.validation.Valid; 

import org.apache.commons.logging.Log; 
import org.apache.commons.logging.LogFactory; 

import com.companyname.springapp.service.PriceIncrease; 
import com.companyname.springapp.service.ProductManager; 

@Controller 
@RequestMapping(value="/priceincrease.htm") 
public class PriceIncreaseFormController { 

    /** Logger for this class and subclasses */ 
    protected final Log logger = LogFactory.getLog(getClass()); 

    @Autowired 
    private ProductManager productManager; 

    @RequestMapping(method = RequestMethod.POST) 
    public String onSubmit(@Valid PriceIncrease priceIncrease, BindingResult result) 
    { 
     if (result.hasErrors()) { 
      return "priceincrease"; 
     } 

     int increase = priceIncrease.getPercentage(); 
     logger.info("Increasing prices by " + increase + "%."); 

     productManager.increasePrice(increase); 

     return "redirect:/hello.htm"; 
    } 

    @RequestMapping(method = RequestMethod.GET) 
    protected PriceIncrease formBackingObject(HttpServletRequest request) throws ServletException { 
     PriceIncrease priceIncrease = new PriceIncrease(); 
     priceIncrease.setPercentage(15); 
     return priceIncrease; 
    } 

    public void setProductManager(ProductManager productManager) { 
     this.productManager = productManager; 
    } 

    public ProductManager getProductManager() { 
     return productManager; 
    } 
} 

title=SpringApp 
heading=Hello :: SpringApp 
greeting=Greetings, it is now 
priceincrease.heading=Price Increase :: SpringApp 
error.not-specified=Percentage not specified!!! 
error.too-low=You have to specify a percentage higher than {0}! 
error.too-high=Don''t be greedy - you can''t raise prices by more than {0}%! 
required=Entry required. 
typeMismatch=Invalid data. 
typeMismatch.percentage=That is not a number!!! 

問題是我沒有得到我,當我在percentaje字段中插入65我沒有收到以下消息定義如消息:特別申明,不要貪 - 你們不能提高價格超過{0}%!而不是我得到的默認消息。 我已經刪除了與autowired註釋關聯的代碼,只是爲了減短文章。 您必須知道應用程序正常工作,問題出現在返回的消息中。

如何顯示我想要的消息?

回答

1

這篇文章詳細解釋瞭如何在spring mvc中進行驗證。 Click Here

+0

非常感謝,它的工作。 – Martin 2014-10-06 20:48:58

+0

非常歡迎。 Upvoting將幫助:) – javafan 2014-10-06 21:05:50

0

你只需要實現Validator這裏有一個簡單的例子...

public class PriceIncreaseValidator implements Validator { 

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

    @Override 
    public void validate(Object o, Errors e) {    
     PriceIncrease priceIncrease = (PriceIncrease) o; 
     if(priceIncrease.getPercentage() == 0) { 
      e.rejectValue("priceIncrease", "error.too-low"); 
     } else if(priceIncrease.getPercentage() >= 50) { 
      e.rejectValue("priceIncrease", "error.too-high"); 
     } 

    } 
}