Spring, Hibernate, and Sitemesh
Posted January 11th, 2008 by paulThis week at work I’ve had the luxury of getting to sit down and write a sample application using a few of my favorite tools: Spring, and Hibernate-Annotations. New to this equation has been Sitemesh. I’ve been using Tiles for quite a while and have felt very comfortable with it, but at the same time a little annoyed with the amount of work and time it takes to use it. As I sat down to pour out as many best practices as possible into this sample application I figured I should use the opportunity to take the time necessary to explore a new tool. At first I had a hard time finding out just what it takes to use Sitemesh with Spring. I found some helpful hints here and there, but because this blog is meant to try to aggregate my searches into something useful for you (the reader), I will outline just what it takes to integrate Sitemesh into an application.
Sitemesh Integration
What do you have to do to integrate Sitemesh with your newly created Spring application? Well, here is the list of prerequisites:
- Download Sitemesh from OpenSymphony.
- Build the sitemesh-2.x.jar using ant.
- Copy the sitemesh-2.x.jar to the
WEB-INF/libdirectory of your web application
Now we can move on to integrating Sitemesh into your web application. To use sitemesh with your application only 1 file needs to be edited amazingly enough. Open up your web.xml file and add this snippet:
<!-- Sitemesh --> <filter> <filter-name>sitemesh</filter-name> <filter-class> com.opensymphony.module.sitemesh.filter.PageFilter </filter-class> </filter> <filter-mapping> <filter-name>sitemesh</filter-name> <url-pattern>/*</url-pattern> </filter-mapping>
So, now what? The next step is to setup the directory structure and files necessary to make things work. Refer to this page on the OpenSymphony site detailing the creation of your first decorator, and creating the WEB-INF/decorators.xml file. I’ve included a copy of my main.jsp file for reference:
<%@ taglib uri="http://www.opensymphony.com/sitemesh/decorator" prefix="decorator"%> <%@ taglib prefix="spring" uri="http://www.springframework.org/tags"%> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en"> <head> <title><spring:message code="application.title" /></title> <decorator:head /> <%@ include file="/WEB-INF/view/includes/style.jsp"%> <%@ include file="/WEB-INF/view/includes/js.jsp"%> </head> <body> <div id="container"> <%@ include file="/WEB-INF/view/includes/header.jsp"%> <div id="internalWrapper"> <%@ include file="/WEB-INF/view/includes/links.jsp"%> <div id="content"> <decorator:body /> </div> </div> <%@ include file="/WEB-INF/view/includes/footer.jsp"%> </div> </body> </html>
So, how do you get your Controllers to spit out decorated pages? Easy, just create a jsp that will be used as the content of the <decorator:body /> tag in the main.jsp file. When you’re returning your ModelAndView just refer to that jsp like the example below:
public ModelAndView list(HttpServletRequest request,
HttpServletResponse response) throws Exception {
List<DomainObject> userList = userService.findAll();
request.setAttribute("userList", userList);
return new ModelAndView("list.jsp", null);
}
Here is an example snippet of my url mapping for reference as to where the jsp is located:
<prop key="/users/*.html">userController</prop>
In this case my list.jsp is located in the users folder under my web root.
So, isn’t this easier than Tiles?
Leave a Reply