Skip to Main Content
Jakarta Struts Cookbook
book

Jakarta Struts Cookbook

by Bill Siggelkow
February 2005
Intermediate to advanced content levelIntermediate to advanced
528 pages
12h 53m
English
O'Reilly Media, Inc.
Content preview from Jakarta Struts Cookbook

12.9. Localizing Look and Feel

Problem

You want your HTML pages to have a different style depending on the user's locale.

Solution

Use a combination of a global Cascading Style Sheet (CSS) and a locale-specific style sheet. The paths for the style sheets are stored as resource bundle properties. Because style sheets are merged, you only need to override those locale-specific styles that are different than the global styles.

Using Struts tags

<style>
    <!--
        <bean:define id="globalStyle">
            <bean:message key="css.global"/>
        </bean:define>
        @import url(<html:rewrite page="<%=globalStyle%>"/>);

        <bean:define id="localStyle">
            <bean:message key="css.local"/>
        </bean:define>
        @import url(<html:rewrite page="<%=localStyle%>"/>);
    -->
</style>

Using JSTL tags

<style>
    <!--
    <fmt:message key="css.global" var="globalStyle"/>
    @import url(<c:url value="${globalStyle}"/>)

    <fmt:message key="css.local" var="localStyle"/>
    @import url(<c:url value="${localStyle}"/>)
    -->
</style>

Discussion

Style sheets can be chosen and applied based on locale in the same manner that images are retrieved as shown in Recipe 12.7. You can use the Struts tags or the JSTL tags. Use bean:message or fmt:message to retrieve a context-relative style sheet path. For example, you would configure paths for a global and locale-specific style sheet in your base resource bundle (ApplicationResources.properties):

css.global=/styles/global.css
css.local=/styles/local.css

The locale-specific file, /styles/local.css, needs to be defined but doesn't need ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

Programming Jakarta Struts

Programming Jakarta Struts

Chuck Cavaness
Beginning Spring Framework 2

Beginning Spring Framework 2

Bruce Snyder, Sing Li, Anne Horton, Thomas Van de Velde, Naveen Balani, Christian Dupuis
Java Cookbook

Java Cookbook

Ian F. Darwin
Struts 2 in Action

Struts 2 in Action

J. Scott Stanlick, Chad Michael Davis, Donald J. Brown

Publisher Resources

ISBN: 059600771XSupplemental ContentErrata Page