Announcement Announcement Module
Collapse
No announcement yet.
Spring MVC Integration with Sitemesh / Velocity and Freemarker Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • Spring MVC Integration with Sitemesh / Velocity and Freemarker

    Hi All

    I have been trying to to get spring mvc integrated with sitemesh and freemarker for a few days and had no joy . I did quite a bit of searching on the web but could not find any documentation until yesterday when I finally figured it all. The process should be the same for veolcity however you will have to change the freemarker specifics to veclocity. I hope this will help some people so they dont have to go through the same pain as I went through as there is no proper documentation on the process.


    Before going through the process described below make sure you configured your spring mvc platform (obviously ). see http://mhimu.wordpress.com/2007/11/2...-mvc-tutorial/ or here http://static.springsource.org/docs/...tep/index.html for details. Then download sitemesh-2.4.x.jar from the sitemesh website i.e. http://www.opensymphony.com/sitemesh/download.action. It should come with a distribution of of freemarker 2.3.x.jar. If not, you can download freemarker from http://freemarker.sourceforge.net/fr...rdownload.html. Copy sitemesh-2.4.x.jar and freemarker 2.3.x.jar into your /WEB-INF/lib directory.

    Configuring Sitemesh Freemarker for Spring MVC

    1. In the web.xml setup the filter that is going to be used by Sitemesh freemarker as described below
    Code:
    <filter>
            <filter-name>sitemesh</filter-name>
            <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter
            </filter-class>
        </filter>
    2. Set up the filter mappings for sitemesh. The filter mappings will determine what urls are handled by by the filter from above.

    Code:
        <filter-mapping>
            <filter-name>sitemesh</filter-name>
            <url-pattern>/*</url-pattern> 
            <dispatcher>FORWARD</dispatcher>
            <dispatcher>REQUEST</dispatcher> 
        </filter-mapping>
    3.In the web.xml file, set up the sitemesh freemarker servlet as shown below. You can set up other servlets if you want to e.g, Your [springapp] servlets. NOTE: The init-param TemplatePath, defines where the servlet will start looking for the decorators

    Code:
    <servlet>
    		<servlet-name>sitemesh-freemarker</servlet-name>
    		<servlet-class>com.opensymphony.module.sitemesh.freemarker.FreemarkerDecoratorServlet</servlet-class>
    		
    		<init-param>
    			<param-name>TemplatePath</param-name>
    			<param-value>/</param-value>
    		</init-param> 
    		<init-param>
    			<param-name>default_encoding</param-name>
    			<param-value>ISO-8859-1</param-value>
    		</init-param>
    		<load-on-startup>5</load-on-startup>
    	</servlet>

    4. Set up the url mappings that the sitemesh freemarker servlet will handle as shown below. Any URL that satisfies this url mapping will be handled passed onto sitemesh to apply the decorators.

    Code:
                                         
    <servlet-mapping>
    		<servlet-name>sitemesh-freemarker</servlet-name>
    		<url-pattern>*.dec</url-pattern>
    	</servlet-mapping>
    	
    	<servlet-mapping>
    		<servlet-name>sitemesh-freemarker</servlet-name>
    		<url-pattern>*.jsp</url-pattern>
    	</servlet-mapping>
    5.Setup the sitemesh.xml. This is stored in the WEB-INF folder. Note the <property name="decorators-file>. It defines where the decorators file(will configure this later) will be found.

    Sitemesh.xml

    Code:
         <?xml version="1.0" encoding="UTF-8"?>
    <sitemesh>
      <property name="decorators-file" value="/WEB-INF/decorators.xml"/>
      <excludes file="${decorators-file}"/>
        
        <page-parsers>
    		<parser content-type="text/html" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser" />
    		<parser content-type="text/html;charset=ISO-8859-1" class="com.opensymphony.module.sitemesh.parser.HTMLPageParser" />
    	</page-parsers>
    
    	<decorator-mappers>
    		<mapper class="com.opensymphony.module.sitemesh.mapper.PageDecoratorMapper">
    			<param name="property.1" value="meta.decorator" />
    			<param name="property.2" value="decorator" />
    		</mapper>
    		<mapper class="com.opensymphony.module.sitemesh.mapper.FrameSetDecoratorMapper"/>
    		<mapper class="com.opensymphony.module.sitemesh.mapper.PrintableDecoratorMapper">
    			<param name="decorator" value="printable" />
    			<param name="parameter.name" value="printable" />
    			<param name="parameter.value" value="true" />
    		</mapper>
    		<mapper class="com.opensymphony.module.sitemesh.mapper.FileDecoratorMapper"/>
    		<mapper class="com.opensymphony.module.sitemesh.mapper.ConfigDecoratorMapper">
    			<!--param name="config" value="/WEB-INF/decorators.xml" /-->
    				<param name="config" value="${decorators-file}"/>
    		</mapper>
      </decorator-mappers>
    </sitemesh>
    6. Create a sitemesh freemarker aware decorator(e.g. sitemesh-freemarker-decorators.dec) and store it in the decorators folder, in this case [ Context root]/decorators/ instance e.g.

    sitemesh-freemarker-decorators.dec
    Code:
        <html>
      <head>
        <title>My Site - ${title}</title>
          ${head}
      </head>
      <body>
        <table border="1">
          <tr>
            <td>My Corporation</td>
          </tr>
          <tr>
          <b>Using freemarker decorator Hooray!!!!</b> 
            <td>${body}</td>
          </tr>
          <tr>
            <td>My corpoartion copyright</td>
          </tr>
        </table>
      </body>
    </html>
    7.Create your decoraters.xml file i.e./WEB-INF/ decorators.xml and save it in folder described by the
    <property name="decorators-file> tag in the sitemesh.xml file i.e. /WEB-INF

    decorators.xml

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <decorators defaultdir="/decorators">
       <decorator name="main" page="sitemesh-freemarker-decorators.dec">
              <pattern>/*</pattern>
        </decorator>
    </decorators>
    8. In your [springapp]-servlet .xml file e.g. springapp-servlet.xml file, configure your view resolver bean to use the freemarker view resolver as described below

    Code:
      <bean id="freemarkerConfig" class="org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer">
      <property name="templateLoaderPath" value="/"/>
    </bean>
     
     <bean id="viewResolver" class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">
      <property name="cache" value="true"/> 
      <property name="prefix" value="/view/"/> 
      <property name="suffix" value=".jsp"/>
      <property name="exposeSpringMacroHelpers" value="true"/>
      </bean>
    9. Finally, you need to create a web page that the decorators will be applied to (obviously ) e.g. badAssPage.jsp

    Code:
    <%@ page language="java" contentType="text/html; charset=ISO-8859-1"
        pageEncoding="ISO-8859-1"%>
    <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
    <html>
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
    <title>Some bad ass title</title>
    </head>
    <body>
    Some bad ass content
    </body>
    </html>
    My final Web.xml. This also describes a servlet used by spring app.

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    	xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    	id="WebApp_ID" version="2.5">
    	<display-name>springapp UI</display-name>
        <filter>
            <filter-name>sitemesh</filter-name>
            <filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter
            </filter-class>
        </filter>
    
        <filter-mapping>
            <filter-name>sitemesh</filter-name>
            <url-pattern>/*</url-pattern> 
            <dispatcher>FORWARD</dispatcher>
            <dispatcher>REQUEST</dispatcher> 
        </filter-mapping>
        
    	<servlet>
    		<display-name>springapp Servlet</display-name>
    		<servlet-name>springapp</servlet-name>
    		<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    		<init-param>
    			<param-name>contextConfigLocation</param-name>
    			<param-value>/WEB-INF/iBankapp-servlet.xml</param-value>
    	  	</init-param>
    		<load-on-startup>2</load-on-startup>
    	</servlet>
    
    
    <servlet>
    
    <servlet-name>sitemesh-freemarker</servlet-name>
    <servlet-class>com.opensymphony.module.sitemesh.freemarker.FreemarkerDecoratorServlet</servlet-class>
    		
    		<init-param>
    			<param-name>TemplatePath</param-name>
    			<param-value>/</param-value>
    		</init-param> 
    		<init-param>
    			<param-name>default_encoding</param-name>
    			<param-value>ISO-8859-1</param-value>
    		</init-param>
    		<load-on-startup>5</load-on-startup>
    	</servlet>
     
    	<servlet-mapping>
    		<servlet-name>springapp</servlet-name>
    		<url-pattern>*.html</url-pattern>
    	</servlet-mapping>
    
    <servlet-mapping>
    		<servlet-name>sitemesh-freemarker</servlet-name>
    		<url-pattern>*.dec</url-pattern>
    	</servlet-mapping>
    	
    <servlet-mapping>
    		<servlet-name>sitemesh-freemarker</servlet-name>
    		<url-pattern>*.ftl</url-pattern>
    	</servlet-mapping>
    
    <servlet-mapping>
    		<servlet-name>sitemesh-freemarker</servlet-name>
    		<url-pattern>*.ftd</url-pattern>
    	</servlet-mapping>
    
    	<servlet-mapping>
    		<servlet-name>sitemesh-freemarker</servlet-name>
    		<url-pattern>*.jsp</url-pattern>
    	</servlet-mapping>
     
    	<welcome-file-list>
    		<welcome-file>index.html</welcome-file>
    		<welcome-file>index.htm</welcome-file>
    		<welcome-file>index.jsp</welcome-file>
    		<welcome-file>/view/Test.jsp</welcome-file>
    		<welcome-file>default.html</welcome-file>
    		<welcome-file>default.htm</welcome-file>
    		<welcome-file>default.jsp</welcome-file>
    		<welcome-file>/view/login.html</welcome-file>
    	</welcome-file-list>
    
    	<jsp-config>
    		<taglib>
    			<taglib-uri>/spring</taglib-uri>
    			<taglib-location>/WEB-INF/tld/spring-form.tld</taglib-location>
    		</taglib>
    	</jsp-config>
    
    </web-app>
    Last edited by quophyie; Nov 18th, 2009, 06:30 AM.

  • #2
    Works.

    Thank you very much for this walk through. It saved me a lot of time.

    Comment

    Working...
    X