Spring MVC Tutorial, 2
This is my second installment of the tutorial. For the first part, see Spring MVC Tutorial.
6 Adding Localization Support
These days, there is much touting about i18n (internalization) and l10n (localization). So, I guess I need to support them also. In fact, I’ve already done the ground work for localizing error messages (error.invalid.principal in LoanCalcValidator, <form:errors> in loanCalc.jsp).
The first thing I’ll do now is add a resource bundle in springweb-servlet.xml for localized messages:
&lt;bean &lt;strong&gt;id="messageSource"&lt;/strong&gt;
class="org.springframework.context.support.ResourceBundleMessageSource"&gt;
&lt;property name="basenames"&gt;
&lt;list&gt;
&lt;value&gt;messages&lt;/value&gt;
&lt;value&gt;errors&lt;/value&gt;
&lt;/list&gt;
&lt;/property&gt;
&lt;/bean&gt;
The most important thing to note here is that the bean is given the id messageSource. It must be so as Spring looks for a message resource supplier by exactly that name. The implementation class ResourceBundleMessageSource uses the standard Java resource bundle facilities for providing localized messages. The bean is initialized through the basenames property which takes a list/array of resource bundle identifiers that will be looked for in the web application’s classpath. In the above configuration, Spring will try to retrieve messages from the files messages.properties and errors.properties by default. To add locale specific messages in French for example, introduce messages_fr.properties and errors_fr.properties with the same property keys.Here is messages.properties for the tutorial:
title.home=Spring MVC Tutorial (Home Page)
title.loancalc=Calculate Loanheading.home=Welcome to Spring MVC Tutorial
greet.morning=Good morning
greet.day=Good day
greet.evening=Good evening
greet.afternoon=Good afternoonmessage.haveErrors=You have errors in your input!
loancalc.prompt.principal=Principal:
loancalc.prompt.apr=APR:
loancalc.prompt.nyears=Number of Years:
loancalc.prompt.yearperiods=Periods Per Year:
loancalc.button.submit=Calculate!
loancalc.message.installmentSize=The installment size is:label.paymentNo=Payment No.
label.principal=Principal
label.interest=Interest
label.outstandingPrincipal=Outstanding Principal
label.serverTime=The time on the server is
label.tenRandomInts=Here are ten random integers:
And errors.properties is like this for now:
error.invalid.principal=Principal must be greater than 0
error.invalid.apr=APR must be greater than 0
error.invalid.years=Number of years must be greater than 0
error.invalid.periodPerYear=Period per year must be greater than 0
All that is left to do now is to update the view components, i.e. the JSPs.First I change home.jsp. The internationalized version is:
&lt;%@ page contentType="text/html;charset=UTF-8" language="java" %&gt;
&lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&gt;
&lt;%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %&gt;
&lt;%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %&gt;
&lt;html&gt;
&lt;head&gt;&lt;title&gt;&lt;spring:message code="title.home"/&gt;&lt;/title&gt;&lt;/head&gt;
&lt;body&gt;
&lt;h1&gt;&lt;c:out value="${greeting}"/&gt;! &lt;spring:message code="heading.home"/&gt;&lt;/h1&gt;
&lt;p align="center"&gt;
&lt;spring:message code="label.serverTime"/&gt; &lt;fmt:formatDate value="${time}" type="time"/&gt;
&lt;/p&gt;
&lt;p align="center"&gt;
&lt;b&gt;&lt;spring:message code="label.tenRandomInts"/&gt;&lt;/b&gt;&lt;br/&gt;
&lt;c:forEach items="${randList}" var="num"&gt;
&lt;c:out value="${num}"/&gt;&lt;br/&gt;
&lt;/c:forEach&gt;
&lt;/p&gt;
&lt;p&gt;&lt;img src="&lt;c:url value="/images/poweredBySpring.gif"/&gt;" alt="Powered By Spring"/&gt;&lt;/p&gt;
&lt;/body&gt;
&lt;/html&gt;
The file loanCalc.jsp takes the form:
&lt;%@ page contentType="text/html;charset=UTF-8" language="java" %&gt;
&lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %&gt;
&lt;%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %&gt;
&lt;%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %&gt;
&lt;html&gt;
&lt;head&gt;&lt;title&gt;&lt;spring:message code="title.loancalc"/&gt;&lt;/title&gt;&lt;/head&gt;
&lt;body&gt;
&lt;center&gt;
&lt;!-- one way to display error messages - globally --&gt;
&lt;spring:hasBindErrors name="loanInfo"&gt;
&lt;h3&gt;&lt;spring:message code="message.haveErrors"/&gt;&lt;/h3&gt;
&lt;font color="red"&gt;
&lt;c:forEach items="${errors.allErrors}" var="error"&gt;
&lt;spring:message code="${error.code}" text="${error.defaultMessage}"/&gt;&lt;br/&gt;
&lt;/c:forEach&gt;
&lt;/font&gt;
&lt;/spring:hasBindErrors&gt;
&lt;p&gt;
&lt;!-- note second way of displaying error messages - by field --&gt;
&lt;form:form commandName="loanInfo" method="POST" action="loancalc.htm"&gt;
&lt;spring:message code="loancalc.prompt.principal"/&gt;&lt;form:input path="principal" /&gt;&lt;form:errors path="principal" /&gt;&lt;br /&gt;
&lt;spring:message code="loancalc.prompt.apr"/&gt;&lt;form:input path="apr" /&gt;&lt;form:errors path="apr" /&gt;&lt;br /&gt;
&lt;spring:message code="loancalc.prompt.nyears"/&gt;&lt;form:input path="years" /&gt;&lt;form:errors path="years" /&gt;&lt;br /&gt;
&lt;spring:message code="loancalc.prompt.yearperiods"/&gt;&lt;form:input path="periodPerYear" /&gt;&lt;form:errors path="periodPerYear" /&gt;&lt;br /&gt;
&lt;input type="submit" title="&lt;spring:message code="loancalc.button.submit"/&gt;" /&gt;
&lt;/form:form&gt;
&lt;/p&gt;
&lt;/center&gt;
&lt;/body&gt;
&lt;/html&gt;
And loanCalcResult.jsp turns into:
&lt;%@ page contentType="text/html;charset=UTF-8" language="java" %&gt;
&lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %&gt;
&lt;%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%&gt;
&lt;%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%&gt;
&lt;html&gt;
&lt;head&gt;&lt;title&gt;Calculate Loan Result&lt;/title&gt;&lt;/head&gt;
&lt;body&gt;
&lt;center&gt;
&lt;p&gt;
&lt;spring:message code="loancalc.message.installmentSize"/&gt;&lt;fmt:formatNumber value="${loanInfo.payment}" type="currency"/&gt;
&lt;/p&gt;
&lt;table border="1"&gt;
&lt;tr&gt;
&lt;td width="10%"&gt;&lt;spring:message code="label.paymentNo"/&gt;&lt;/td&gt;
&lt;td align="right" width="30%"&gt;&lt;spring:message code="label.principal"/&gt;&lt;/td&gt;
&lt;td align="right" width="30%"&gt;&lt;spring:message code="label.interest"/&gt;&lt;/td&gt;
&lt;td align="right" width="30%"&gt;&lt;spring:message code="label.outstandingPrincipal"/&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;c:forEach items="${loanInfo.schedule}" var="entry"&gt;
&lt;tr&gt;
&lt;td&gt;${entry.paymentNo}&lt;/td&gt;
&lt;td align="right"&gt;&lt;fmt:formatNumber value="${entry.principal}" type="currency"/&gt;&lt;/td&gt;
&lt;td align="right"&gt;&lt;fmt:formatNumber value="${entry.interest}" type="currency"/&gt;&lt;/td&gt;
&lt;td align="right"&gt;&lt;fmt:formatNumber value="${entry.outstanding}" type="currency"/&gt;&lt;/td&gt;
&lt;/tr&gt;
&lt;/c:forEach&gt;
&lt;/table&gt;
&lt;/center&gt;
&lt;/body&gt;
&lt;/html&gt;
Source Code Download
The source code is available here as an IDEA project along with an Ant build file.
Thanks, your tutorial really helped me today to jump-start Spring MVC.
Vaclav
January 27, 2008 at 9:45 pm
Hello,
It is indeed a useful tutorial. Especially the code related to buttons helped me a lot.
Prasad Khandekar
March 4, 2008 at 1:15 pm
wrgwcd hi nice site man thx http://peace.com
bob
March 4, 2008 at 7:49 pm
free car quote
Excellent post. Keep it up!
Anonymous
March 9, 2008 at 3:59 pm
it’s not working with me. I have done all the configurations, but rather showing me the message from messages.properties it displays tag as it is.
is there any specific location for messages.properties file ?
web.xml Configuration
org.springframework.web.context.ContextLoaderListener
applicationContext.xml Configuration
jsp Code
Wjaahat
April 11, 2008 at 10:59 am
Dear Wjaahat,
Have you tried the source code download provided with this post?
… initialized through the basenames property which takes a list/array of resource bundle identifiers that will be looked for in the web application’s classpath …
So, the messages.properties file should be ideally located at the root of your source folder in the project or at the root of your program classpath.
mhimu
April 13, 2008 at 11:44 am
Good going man
Great job in summarizing Spring MVC in just 2 posts and covering almost every basic implementation.
I wondered why this work isn’t listed in top Google Search results.
Best Wishes
Nitesh Gautam
July 10, 2008 at 6:21 pm
Подскажите шооблончег под WordPress 2.6.2, чтобы был похож на ваш mhimu.wordpress.com.
Заранее благодарю)
blorway
October 7, 2008 at 2:22 pm
Its very easy tolearn,i searched so many websites for understanding MVC,but yours is very optimised and easy to understand! Can you discuss more about other Spring MVC topics like data persistence(Spring JDBC)…..
Viswanadh
October 16, 2008 at 4:55 pm
Hello webmaster
I would like to share with you a link to your site
write me here preonrelt@mail.ru
Alexwebmaster
March 3, 2009 at 8:10 pm
hi
I found it very good stuff
Vineela
March 10, 2009 at 6:47 pm
I need to use a SimpleFormController and return to the same page after the form submission.
How should I go it ?
If I do getFormView instead of getSucessView.. that does not work for me
Peter
May 7, 2009 at 8:02 pm
Hi WJaahat,
use following as your resource loader and specify the path as specified.. it will surely work…
/WEB-INF/errors
give it a try..
Thanks
sanjay
Sanjay Sharma
July 15, 2009 at 1:19 pm
Sorry but i think i saw this question very late.. WJaahat will be a champ now..
Sanjay Sharma
July 15, 2009 at 1:20 pm