How to configure Templating Solution Engine SiteMesh with Spring boots

19 / May / 2016 by Sandeep Gupta 0 comments

SiteMesh is a very simple, lightweight & flexible Templating Solution framework for Java Web applications. SiteMesh can be used in lot more ways if we combine it with Spring boot framework. Configuring the SiteMesh with Spring Boot is little bit tricky but not complex, as we need to create a custom filter and then inject it into Filter Chain to work properly.

Here We will be refering to a Gradle based Spring Boot application, with which SiteMesh needs to be integrated. Detailed functionality of SiteMesh is not covered here. The focus area is SiteMesh integration with Spring Boot.

Following are the 4 Steps for integration:

1. Add gradle dependency in spring boot project’s gradle file

org.sitemesh:sitemesh:3.0.0'

2. Create one or more templates or decorators to apply

[java]
<!DOCTYPE html>
<html lang="en">
<head>
< sitemesh:write property="head" />
<%@ include file="/WEB-INF/includes/header.jsp" %>
</head>
<body>
<%@ include file="/WEB-INF/includes/topNavigation.jsp" %>
<main id="page-wrapper5">
<div class="container-fluid">
<sitemesh:write property="body" />
</div>
</main>
</body>
</html>
[/java]

3. Create SiteMesh filter. Register multiple decorators. We can also exclude paths if required.

[java]
public class CustomSiteMeshFilter extends ConfigurableSiteMeshFilter {
@Override
protected void applyCustomConfiguration(SiteMeshFilterBuilder builder) {
// Assigning default decorator if no path specific decorator found
builder.addDecoratorPath("/*", "/WEB-INF/sitemesh/MainDecorator.jsp")
// Map decorators to specific path patterns.
.addDecoratorPath("/login", "/WEB-INF/sitemesh/simpleDecorator .jsp")
// Exclude few paths from decoration.
.addExcludedPath("/javadoc/*")
.addExcludedPath("/brochures/*");
}
}
[/java]

4. Inject SiteMesh filter in SpringSecurityChain. Url Patterns identifies the candidates which should be wrapped with decorator. Here we are applying the decorators to all URLs and handling exclusions in SiteMeshFilter

[java]
@Configuration
public class ServletInit extends SpringBootServletInitializer {
@Bean
public FilterRegistrationBean siteMeshFilter() {
FilterRegistrationBean filterRegistrationBean = new FilterRegistrationBean();
filterRegistrationBean.setFilter(new CustomSiteMeshFilter()); // adding sitemesh filter ??
filterRegistrationBean.addUrlPatterns("/*");
return filter;
}
}
[/java]

FOUND THIS USEFUL? SHARE IT

Leave a Reply

Your email address will not be published. Required fields are marked *