Search code examples
springspring-mvc

How exactly works the Spring MVC themes support? (some doubts about a proposed concrete example)


I am pretty new in Spring MVC and I am actually studying the theming support.

So I am working on an example application that have the following characteristics:

1] Into the src/webapp/resources/css are defined 3 CSS theme files, respectivelly named: theme-black.css, theme-chocolate.css and theme-dafault.css. So these files represents the CSS settings associated to a theme that have to be chosen by the user.

2] Into the servlet-context.xml configuration file that should be the file that configure the MVC support there are defined the following beans related to the theming support:

<beans:bean id="themeSource"
    class="org.springframework.ui.context.support.ResourceBundleThemeSource">
    <beans:property name="basenamePrefix" value="META-INF.theme-" />
</beans:bean>


<beans:bean id="themeChangeInterceptor"
    class="org.springframework.web.servlet.theme.ThemeChangeInterceptor">
    <beans:property name="paramName" value="theme" />
</beans:bean>


<mvc:interceptors>
    <beans:ref bean="themeChangeInterceptor" />
</mvc:interceptors> 

<beans:bean id="themeResolver"
    class="org.springframework.web.servlet.theme.CookieThemeResolver">
    <beans:property name="defaultThemeName" value="default" />
</beans:bean>

And into the META-INF application folder there are the following 3 properties file related to the previous CSS theme files:

theme-black.properties:

css=resources/css/theme-black.css

theme-chocolate.properties:

css=resources/css/theme-chocolate.css

theme-default.properties:

css=resources/css/theme-default.css

And now I want to understand what these beans exactly does.

From what I have understood, the Spring MVC theming support works in the following way:

The web application have a default theme at the beginning (is it the one named theme-default.css by default? So if I use the theme-default.css for a theme it is loaded as default theme or am I missing something?)

I need to provide to the user a set of theme from which to choose the theme that he want to use.

When the user choose a theme the application send a cookie to the user browser and this cookie contain the information about the chosen theme.

Then, any time that the user make any HTTP request to the application (handled by a controller) there must be something that intercept these request and that read the cookie for the chosen theme identification and then use this theme for the response.

So I think that the previous declared beans works in the following way:

1] The first class is the theme source:

<beans:bean id="themeSource"
    class="org.springframework.ui.context.support.ResourceBundleThemeSource">
    <beans:property name="basenamePrefix" value="META-INF.theme-" />
</beans:bean>

and take as input parameter a property named basenamePrefix that identify the previous properties file that contains the concrete themes path (theme-black.properties, theme-chocolate.properties and theme-default.properties).

So this bean should accomplish the first requirement of the theme support (identify and access to the list of themes)

2] The second class is the theme interceptor:

<beans:bean id="themeChangeInterceptor"
    class="org.springframework.web.servlet.theme.ThemeChangeInterceptor">
    <beans:property name="paramName" value="theme" />
</beans:bean>

that is a class that I can use to intercept any request made by user and do pre action and post action and from what I have understand it define the URL parameter name that let the user to change the theme.

So in this example the parameter name is theme so when the user call an URL like:

http://localhost:8080/springchocolatestore/?theme=chocolate

it is choosing the chocolate theme that is related to the theme-chocolate.properties so the resources/css/theme-chocolate.css file will be used.

So my doubt is: the preaction is to check the presence of the ?theme=chocolate in the URL and apply the style?

3] The third bean is the theme resolver:

<beans:bean id="themeResolver"
    class="org.springframework.web.servlet.theme.CookieThemeResolver">
    <beans:property name="defaultThemeName" value="default" />
</beans:bean>

So this resolver use cookie to identify the chosed theme.

So, for example, if I chose the chocolate theme (by the request http://localhost:8080/springchocolatestore/?theme=chocolate) the web app send to the browser a cookie that store the information that specify that the chocolate theme have to be used.

Then if I reload again the application the page still use the chocolate theme (and not the default theme).

It means that the CookieThemeResolver had access to this cookie, read it and use this information to select the theme to use?

So I have the following doubts: this cookie is on my file system and the web application is on a remote application server. How can it access to the cookie on my file system? Where this cookie is into my file system and what is its name ? (I want to open it with text editor to examinate it)

Finally it is also declared this configuration:

<mvc:interceptors>
    <beans:ref bean="themeChangeInterceptor" />
</mvc:interceptors> 

What exactly does this configuration?


Solution

  • org.springframework.ui.context.support.ResourceBundleThemeSource
    

    Loads properties files from the specified base usually classpath.From spring api docs ResourceBundleThemeSource is a ThemeSource implementation that looks up an individual ResourceBundle per theme. The theme name gets interpreted as ResourceBundle basename, supporting a common basename prefix for all themes.

    org.springframework.web.servlet.theme.ThemeChangeInterceptor
    

    Interceptor that allows for changing the current theme on every request, via a configurable request parameter (default parameter name: "theme"). More information can be found in spring api docs as well

    org.springframework.web.servlet.theme.CookieThemeResolver
    

    ThemeResolver implementation that uses a cookie sent back to the user in case of a custom setting, with a fallback to the default theme. This is particularly useful for stateless applications without user sessions. Again more information here

    this cookie is on my file system and the web application is on a remote application server. How can it access to the cookie on my file system? Where this cookie is into my file system and what is its name ?

    What's an HTTP cookie in the first place?

    To check cookies sent by any website use firebug if you use Firefox or developer tools if you use chrome