In the end I used Spring's theme support to achieve what I wanted.
In my view code I use the <spring:theme code=""/>
tag to get the path to image file:
<img src="<spring:theme code="theme.images.actions.edit.link"/>" />
This tag behaves like any <fmt:message>
or <spring:message>
tag, but it has its own "message bundles". The necessary configurations in my applicationContext are:
<!--
=========================================================
Themes
=========================================================
-->
<bean id="themeResolver" class="org.springframework.web.servlet.theme.SessionThemeResolver">
<property name="defaultThemeName" value="themes.default"/>
</bean>
<bean id="themeSource" class="org.springframework.ui.context.support.ResourceBundleThemeSource" />
All themes of my application are stored under /WEB-INF/classes/themes/
. The default theme properties are in /WEB-INF/classes/themes/default.properties
It looks like this:
...
theme.images.actions.show.link=/@contextPath@/shared/images/famfam/zoom.png
theme.images.actions.delete.link=/@contextPath@/shared/images/famfam/cross.png
...
To change the theme (and icons) of my app I use a ThemeChangeInterceptor (in applicationContext)
<!--
=========================================================
Theme resolving
=========================================================
-->
<bean id="themeChangeInterceptor" class="org.springframework.web.servlet.theme.ThemeChangeInterceptor">
<property name="paramName" value ="theme" />
</bean>
This enables the user to switch the theme via a "&theme=themes.default"
or "&theme=themes.alternative"
request parameter.
One key part of my setup is the @contextPath@
in the theme properties file. This is replaced during the Ant build process with the correct context path for development/testing/production environment. The key part of my build.xml is:
<!-- copy all common themes to classes -->
<copy todir="${build.war}/WEB-INF/classes/themes" overwrite="true" filtering="true">
<fileset dir="resources/themes" includes="**/*.properties" />
<filterchain>
<replacetokens>
<token key="contextPath" value="${setup.contextPath}"/>
</replacetokens>
</filterchain>
</copy>
I hope this gives you a "running start" on Spring web app themes. In my opinion this setup makes it quite easy to alter the look and feel of an application.
References: