3. JSP Example

To make sure pages display UTF-8 characters properly, it's important to set the page encoding and content type at the very top of the JSP page. Since this example webapp is using Tiles, it's only necessary to set it at the top of the main template.

Excerpt from /WEB-INF/templates/main.jsp
                
<%@ page pageEncoding="UTF-8" contentType="text/html; charset=UTF-8" %>
                
            

The footer of the Tiles template shows how to display something from the message resource files and also how to create a link to change locales. The fmt:message retrieves 'button.locale' from the property file that matches the current locale, or if that key isn't in the current locale's file it will use the default locale's message.

The URL to change the locale uses the c:url JSTL tag and redirects to the home page. It sets the parameter to match what was configured on the LocaleChangeInterceptor bean's paramName property. Which in this case is 'locale'. Passing in no locale switches to the default locale and for Spanish the parameter 'es' is used.

/WEB-INF/templates/footer.jsp
                
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt" %>

<div align="right">
    <div>
        <fmt:message key="button.locale"/>:
            <c:url var="englishLocaleUrl" value="/index.html">
                <c:param name="locale" value="" />
            </c:url>
            <c:url var="spanishLocaleUrl" value="/index.html">
                <c:param name="locale" value="es" />
            </c:url>
        
            <a href='<c:out value="${englishLocaleUrl}"/>'><fmt:message key="locale.english"/></a>
            <a href='<c:out value="${spanishLocaleUrl}"/>'><fmt:message key="locale.spanish"/></a>
    </div>
    
    <div>&nbsp;</div>
    
    <div><fmt:message key="site.footer"/></div>
</div>