Announcement Announcement Module
No announcement yet.
Velocity & Themes Page Title Module
Move Remove Collapse
Conversation Detail Module
  • Filter
  • Time
  • Show
Clear All
new posts

  • Velocity & Themes


    How do I call the theme on a velocity template?

    Like using JSP yo do something like:

    <link rel="stylesheet" href="<spring:theme code="styleSheet"/>" type="text/css"/>

    But I don't see any Spring tag to do this.

    Any ideas?


  • #2
    Ok, got the solution. I'm posting it here hope it helps anyone trying to do themes:

    On your servlet context.xml file you'll need:

    <!-- Themes configuration -->
      <bean id="themeSource" class="">
        <property name="basenamePrefix" value="resources.themes." />
      <bean id="themeResolver" class="org.springframework.web.servlet.theme.CookieThemeResolver">
        <property name="defaultThemeName" value="blue" />
    <!-- Register "global" interceptor beans to apply to all registered HandlerMappings -->
        <!-- <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/> -->
        <bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor"/>
    On Spring 3+ you'll have to use <mvc:interceptors> so the switch themes will get applied correctly. If you don't do this a use the old way it will not work and more hacking will have to be done.

    Then you'll need to define the properties files. This files should be on your classpath. If you see the ResourceBundleThemeSource which actually is the one who loads the props files it says on my case:

    resources.themes. -> this translates to classes/resources/themes/<here goes the theme>.properties

    I'm using the default so the "theme" variable populated by CookieThemeResolver will have by default "blue" but when changes it can have "black". So your props file should be, etc, etc.

    Each of this files should have something like:

    Which tells you the association between the variable and the actual value that wiull be used on the templates (velocity, or JSP, etc).

    Then you'll need the CSS files which should be located @ the base of the WebContent (outside the WEB-INF dir) and based on our props file I have something like:

    [path to webapp]/resources/themes/black.css -> the same for all other themes.

    After all this we need the template. Spring 3+ comes with bindings for velocity and JSP to get the themes so you just need something like this:

        <link rel="stylesheet" href="#springTheme("css")" type="text/css"/>
        <h1>Hello world!</h1>
        <span style="float: left">
          <a href="?theme=default">default</a>
          <a href="?theme=black">black</a>
          <a href="?theme=blue">blue</a>
    You see there the binding #springTheme("css") (I'm using velocity as my template engine, but you can have it with JSP as well) that will load the actual CSS variable from the props file.

    Then remember to set up the static resources on your servlet context.xml file so it doesn't go trough the dispatcher servlet like this:

    <!-- Setting up static resources --> 
      <mvc:resources mapping="/resources/**" location="/resources/"/>
    And that's it. That should get you going.

    Hope it helps.