Results 1 to 2 of 2

Thread: Velocity & Themes

  1. #1
    Join Date
    Mar 2011
    Posts
    8

    Default Velocity & Themes

    Hello,

    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?

    Thx.

  2. #2
    Join Date
    Mar 2011
    Posts
    8

    Default

    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:

    Code:
    <!-- Themes configuration -->
      <bean id="themeSource" class="org.springframework.ui.context.support.ResourceBundleThemeSource">
        <property name="basenamePrefix" value="resources.themes." />
      </bean>
      
      <bean id="themeResolver" class="org.springframework.web.servlet.theme.CookieThemeResolver">
        <property name="defaultThemeName" value="blue" />
      </bean>
    
    <!-- Register "global" interceptor beans to apply to all registered HandlerMappings -->
      <mvc:interceptors>
        <!-- <bean class="org.springframework.web.servlet.i18n.LocaleChangeInterceptor"/> -->
        <bean class="org.springframework.web.servlet.theme.ThemeChangeInterceptor"/>
      </mvc:interceptors>
    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 black.properties, etc, etc.

    Each of this files should have something like:

    Code:
    css=resources/themes/black.css
    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:

    Code:
    <html>
      <head>
        <link rel="stylesheet" href="#springTheme("css")" type="text/css"/>
        <title>Home</title>
      </head>
      <body>
        <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>
        </span>
      </body>
    </html>
    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:

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

    Hope it helps.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •