How do I integrate Sitemesh 3 with Spring MVC 3?

14,168

Solution 1

Since no one has posted actual content, here you go:

in pom.xml add:

<dependency>
    <groupId>org.sitemesh</groupId>
    <artifactId>sitemesh</artifactId>
    <version>3.0.0</version>
</dependency>

in WEB-INF/web.xml † add:

<filter>
    <filter-name>sitemesh</filter-name>
    <filter-class>org.sitemesh.config.ConfigurableSiteMeshFilter</filter-class>
</filter>

<filter-mapping>
    <filter-name>sitemesh</filter-name>
    <url-pattern>/*</url-pattern>
</filter-mapping>

in WEB-INF/sitemesh3.xml add:

<sitemesh>
    <mapping path="/*" decorator="/WEB-INF/decorator1.jsp"/>
</sitemesh>

in WEB-INF/decorator1.jsp add:

<html>
    <head>
    ...
    </head>
    <body>
        <sitemesh:write property='body'/>
    </body>
</html>

† put this below your Spring Security Filter Chain if using Spring Security.

Solution 2

Here is another blog that shows the integration between Sitemesh 3 and Spring MVC

Share:
14,168

Related videos on Youtube

DaFoot
Author by

DaFoot

Professional Java &amp; Angular developer Hobbyist photographer Train in Judo (competitor / coach / referee) Buy me a coffee (or a beer!): https://www.buymeacoffee.com/dafoot

Updated on June 04, 2022

Comments

  • DaFoot
    DaFoot almost 2 years

    I am trying to use Sitemesh 3 to control the decoration of JSP output from a Spring MVC application.

    When I hit the application it seems that Sitemesh is making a request to the Spring servlet to try and retreive its decorator file. This may, or may not be correct behaviour but it is causing me all many of headaches.

    My understanding of Sitemesh 3 is that it does its work after Spring, ie on the Response object.

    The error I'm getting in the browser is a 404 and in the logs (config/code follows):

    INFO: Server startup in 1367 ms
    DEBUG: org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 
    'springiness' processing GET request for [/clientmanager/]^M
    DEBUG: 
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - 
    Looking up handler method for path /^M
    DEBUG: org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - 
    Returning handler method [public java.lang.String 
    uk.co.hermes.HomeController.home(java.util.Locale,org.springframework.ui.Model)]^M
    DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Returning 
    cached instance of singleton bean 'homeController'^M
    DEBUG: org.springframework.web.servlet.DispatcherServlet - Last-Modified value for 
    [/clientmanager/] is: -1^M
    INFO : uk.co.hermes.HomeController - Welcome home! The client locale is en_GB.^M
    DEBUG: org.springframework.beans.factory.support.DefaultListableBeanFactory - Invoking     
    afterPropertiesSet() on bean with name 'home'^M
    DEBUG: org.springframework.web.servlet.DispatcherServlet - Rendering view   
    [org.springframework.web.servlet.view.JstlView: name 'home'; URL [/WEB- 
    INF/jsp/home.jsp]] in DispatcherServlet with name 'springiness'^M
    DEBUG: org.springframework.web.servlet.view.JstlView - Added model object 'serverTime'  
    of type [java.lang.String] to request in view with name 'home'^M
    DEBUG: org.springframework.web.servlet.view.JstlView - Forwarding to resource [/WEB-  
    INF/jsp/home.jsp] in InternalResourceView 'home'^M
    DEBUG: org.springframework.web.servlet.DispatcherServlet - Successfully completed  
    request^M
    DEBUG: org.springframework.web.servlet.DispatcherServlet - DispatcherServlet with name 
    'springiness' processing GET request for [/clientmanager/WEB- 
    INF/decorators/mainDecorator.html]^M
    DEBUG:  
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping -  
    Looking up handler method for path /WEB-INF/decorators/mainDecorator.html^M
    DEBUG:  
    org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping -  
    Did not find handler method for [/WEB-INF/decorators/mainDecorator.html]^M
    WARN : org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request 
    with URI [/clientmanager/WEB-INF/decorators/mainDecorator.html] in DispatcherServlet 
    with name 'springiness'^M
    DEBUG: org.springframework.web.servlet.DispatcherServlet - Successfully completed request^M
    

    I suspect it's a problem in my web.xml and how I've defined the mappings(URLs):

    <servlet>
        <servlet-name>springiness</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>/WEB-INF/spring/appServlet/servlet-context.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>springiness</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
    
    <filter>
        <filter-name>sitemeshfilter</filter-name>
        <filter-class>uk.co.hermes.filters.SitemeshFilter</filter-class>
    </filter>
    <filter-mapping>
        <filter-name>sitemeshfilter</filter-name>
        <!-- leaving SitemeshFilter class to decide which responses it should decorate -->
        <url-pattern>/*</url-pattern>
    </filter-mapping>
    

    My custom filter:

    public class SitemeshFilter extends ConfigurableSiteMeshFilter {
    
    private Logger log = LoggerFactory.getLogger(SitemeshFilter.class);
    
    /**
     * See http://wiki.sitemesh.org/display/sitemesh3/Configuring+SiteMesh+3
     */
    @Override
    protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
    log.debug("** hit the sitemesh filter");
            // apply this decorator (template) to the path defined...
            builder.addDecoratorPath("/*", "/WEB-INF/decorators/mainDecorator.html");
    
            // ... when the response type matches one of these 
            builder.setMimeTypes("text/html", "application/xhtml+xml", "application/vnd.wap.xhtml+xml");
    

    } }

    WEB-INF/ |-jsp |-home.jsp |-decorators |-mainDecorator.html

    And my super simple Controller:

        @RequestMapping(value = "/", method = RequestMethod.GET)
    public String home(Locale locale, Model model) {
        logger.info("Welcome home! The client locale is {}.", locale);
    
        Date date = new Date();
        DateFormat dateFormat = DateFormat.getDateTimeInstance(DateFormat.LONG, DateFormat.LONG, locale);
    
        String formattedDate = dateFormat.format(date);
    
        model.addAttribute("serverTime", formattedDate );
    
        return "home";
    }
    
  • toast38coza
    toast38coza almost 8 years
    This link is broken
  • Ariel Carrera
    Ariel Carrera over 7 years
    Thanks, I fixed it.