Himu\’s Attempt at Blogging

Tidbits from my thoughts

Spring MVC Tutorial, 2

with 16 comments

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:

&amp;lt;bean &amp;lt;strong&amp;gt;id="messageSource"&amp;lt;/strong&amp;gt;
&amp;lt;property name="basenames"&amp;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 Loan

heading.home=Welcome to Spring MVC Tutorial

greet.morning=Good morning
greet.day=Good day
greet.evening=Good evening
greet.afternoon=Good afternoon

message.haveErrors=You have errors in your input!

loancalc.prompt.nyears=Number of Years:
loancalc.prompt.yearperiods=Periods Per Year:
loancalc.message.installmentSize=The installment size is:

label.paymentNo=Payment No.
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:

&amp;lt;%@ page contentType="text/html;charset=UTF-8" language="java" %&amp;gt;
&amp;lt;%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %&amp;gt;
&amp;lt;%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %&amp;gt;
&amp;lt;%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %&amp;gt;

&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;&amp;lt;spring:message code="title.home"/&amp;gt;&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;
&amp;lt;h1&amp;gt;&amp;lt;c:out value="${greeting}"/&amp;gt;! &amp;lt;spring:message code="heading.home"/&amp;gt;&amp;lt;/h1&amp;gt;
&amp;lt;p align="center"&amp;gt;
&amp;lt;spring:message code="label.serverTime"/&amp;gt; &amp;lt;fmt:formatDate value="${time}" type="time"/&amp;gt;
&amp;lt;p align="center"&amp;gt;
&amp;lt;b&amp;gt;&amp;lt;spring:message code="label.tenRandomInts"/&amp;gt;&amp;lt;/b&amp;gt;&amp;lt;br/&amp;gt;
&amp;lt;c:forEach items="${randList}" var="num"&amp;gt;
&amp;lt;c:out value="${num}"/&amp;gt;&amp;lt;br/&amp;gt;
&amp;lt;p&amp;gt;&amp;lt;img src="&amp;lt;c:url value="/images/poweredBySpring.gif"/&amp;gt;" alt="Powered By Spring"/&amp;gt;&amp;lt;/p&amp;gt;

The file loanCalc.jsp takes the form:

&amp;lt;%@ page contentType="text/html;charset=UTF-8" language="java" %&amp;gt;
&amp;lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %&amp;gt;
&amp;lt;%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %&amp;gt;
&amp;lt;%@ taglib uri="http://www.springframework.org/tags" prefix="spring" %&amp;gt;

&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;&amp;lt;spring:message code="title.loancalc"/&amp;gt;&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;

&amp;lt;!-- one way to display error messages - globally --&amp;gt;
&amp;lt;spring:hasBindErrors name="loanInfo"&amp;gt;
&amp;lt;h3&amp;gt;&amp;lt;spring:message code="message.haveErrors"/&amp;gt;&amp;lt;/h3&amp;gt;
&amp;lt;font color="red"&amp;gt;
&amp;lt;c:forEach items="${errors.allErrors}" var="error"&amp;gt;
&amp;lt;spring:message code="${error.code}" text="${error.defaultMessage}"/&amp;gt;&amp;lt;br/&amp;gt;

&amp;lt;!-- note second way of displaying error messages - by field --&amp;gt;
&amp;lt;form:form commandName="loanInfo" method="POST" action="loancalc.htm"&amp;gt;
&amp;lt;spring:message code="loancalc.prompt.principal"/&amp;gt;&amp;lt;form:input path="principal" /&amp;gt;&amp;lt;form:errors path="principal" /&amp;gt;&amp;lt;br /&amp;gt;
&amp;lt;spring:message code="loancalc.prompt.apr"/&amp;gt;&amp;lt;form:input path="apr" /&amp;gt;&amp;lt;form:errors path="apr" /&amp;gt;&amp;lt;br /&amp;gt;
&amp;lt;spring:message code="loancalc.prompt.nyears"/&amp;gt;&amp;lt;form:input path="years" /&amp;gt;&amp;lt;form:errors path="years" /&amp;gt;&amp;lt;br /&amp;gt;
&amp;lt;spring:message code="loancalc.prompt.yearperiods"/&amp;gt;&amp;lt;form:input path="periodPerYear" /&amp;gt;&amp;lt;form:errors path="periodPerYear" /&amp;gt;&amp;lt;br /&amp;gt;
&amp;lt;input type="submit" title="&amp;lt;spring:message code="loancalc.button.submit"/&amp;gt;" /&amp;gt;

And loanCalcResult.jsp turns into:

&amp;lt;%@ page contentType="text/html;charset=UTF-8" language="java" %&amp;gt;
&amp;lt;%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %&amp;gt;
&amp;lt;%@ taglib uri="http://java.sun.com/jsp/jstl/fmt" prefix="fmt"%&amp;gt;
&amp;lt;%@ taglib uri="http://www.springframework.org/tags" prefix="spring"%&amp;gt;

&amp;lt;head&amp;gt;&amp;lt;title&amp;gt;Calculate Loan Result&amp;lt;/title&amp;gt;&amp;lt;/head&amp;gt;
&amp;lt;spring:message code="loancalc.message.installmentSize"/&amp;gt;&amp;lt;fmt:formatNumber value="${loanInfo.payment}" type="currency"/&amp;gt;
&amp;lt;table border="1"&amp;gt;
&amp;lt;td width="10%"&amp;gt;&amp;lt;spring:message code="label.paymentNo"/&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td align="right" width="30%"&amp;gt;&amp;lt;spring:message code="label.principal"/&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td align="right" width="30%"&amp;gt;&amp;lt;spring:message code="label.interest"/&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td align="right" width="30%"&amp;gt;&amp;lt;spring:message code="label.outstandingPrincipal"/&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;c:forEach items="${loanInfo.schedule}" var="entry"&amp;gt;
&amp;lt;td align="right"&amp;gt;&amp;lt;fmt:formatNumber value="${entry.principal}" type="currency"/&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td align="right"&amp;gt;&amp;lt;fmt:formatNumber value="${entry.interest}" type="currency"/&amp;gt;&amp;lt;/td&amp;gt;
&amp;lt;td align="right"&amp;gt;&amp;lt;fmt:formatNumber value="${entry.outstanding}" type="currency"/&amp;gt;&amp;lt;/td&amp;gt;

Source Code Download

The source code is available here as an IDEA project along with an Ant build file.


Written by mhimu

December 10, 2007 at 3:07 pm

Posted in spring

Tagged with , ,

16 Responses

Subscribe to comments with RSS.

  1. Thanks, your tutorial really helped me today to jump-start Spring MVC.


    January 27, 2008 at 9:45 pm

  2. 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

  3. wrgwcd hi nice site man thx http://peace.com


    March 4, 2008 at 7:49 pm

  4. free car quote

    Excellent post. Keep it up!


    March 9, 2008 at 3:59 pm

  5. 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


    applicationContext.xml Configuration

    jsp Code


    April 11, 2008 at 10:59 am

  6. 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.


    April 13, 2008 at 11:44 am

  7. 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

  8. Подскажите шооблончег под WordPress 2.6.2, чтобы был похож на ваш mhimu.wordpress.com.

    Заранее благодарю)


    October 7, 2008 at 2:22 pm

  9. 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)…..


    October 16, 2008 at 4:55 pm

  10. Hello webmaster
    I would like to share with you a link to your site
    write me here preonrelt@mail.ru


    March 3, 2009 at 8:10 pm

  11. hi
    I found it very good stuff


    March 10, 2009 at 6:47 pm

  12. 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 😦


    May 7, 2009 at 8:02 pm

  13. Hi WJaahat,

    use following as your resource loader and specify the path as specified.. it will surely work…


    give it a try..


    Sanjay Sharma

    July 15, 2009 at 1:19 pm

  14. 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

  15. […] For introduction to Spring MVC in particular, see Spring MVC Tutorial and Spring MVC Tutorial 2. […]

  16. privv boo kok 445


    November 23, 2011 at 3:04 am

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: