I'm trying to use Sitemesh 2.4.2 to decorate some JSPs with a navbar, and also to extract my CSS references into this decorator. However, I can't make it work: depending on how I set up the pattern in my decorators.xml
, I'm either getting only the base JSP with no decorator, or the decorator (this is, the content of the <head>
tag, and the <navbar>
I'm adding at the beginning of my <body>
tag) with no content from the base JSP.
My application is using Glassfish 4 as the application server, hence using Jersey 2. Since I'm using JSPs, I'm using Jersey as a filter.
This is my web.xml
(I'm not entirely sure if I need this FORWARD dispatcher)
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<filter>
<filter-name>SiteMeshFilter</filter-name>
<filter-class>com.opensymphony.sitemesh.webapp.SiteMeshFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>SiteMeshFilter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
</filter-mapping>
<filter>
<filter-name>jersey</filter-name>
<filter-class>org.glassfish.jersey.servlet.ServletContainer</filter-class>
<init-param>
<param-name>jersey.config.servlet.filter.forwardOn404</param-name>
<param-value>true</param-value>
</init-param>
<init-param>
<param-name>javax.ws.rs.Application</param-name>
<param-value>com.xxx.jersey2.config.CustomResourceConfig</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>jersey</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
My decorators.xml
<?xml version="1.0" encoding="UTF-8"?>
<decorators defaultdir="/WEB-INF/decorators">
<decorator name="main" page="my_navbar.jsp">
<!--<pattern>/jsp/*</pattern>-->
<pattern>/*</pattern>
</decorator>
</decorators>
Here, if I change the pattern to <pattern>/jsp/*</pattern>
, then the decorator wouldn't be included at all — I would get just the base JSP.
By setting the pattern to <pattern>/*</pattern>
, I'm getting only the decorator, this is, I'm getting only the content of my_navbar.jsp
. Also, Sitemesh's tags on it, like <decorator:body />
, won't be rendered at all, and they would appear in the resulting webpage source code that can be seen from the browser.
This is the JSP that's going to be decorated, my_navbar.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Sandbox application</title>
<link rel="stylesheet" href="../../js/jquery-1.12.4.min.js">
<link rel="stylesheet" href="../../css/bootstrap.min.css">
<link rel="stylesheet" href="../../js/bootstrap.min.js">
<decorator:head />
</head>
<body>
<nav class="navbar navbar-light bg-faded">
<ul class="nav navbar-nav">
<li class="nav-item">
<a class="nav-link" href="/sample-jersey2/">Home</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/sample-jersey2/movies/list/">Movies</a>
</li>
<li class="nav-item">
<a class="nav-link" href="/sample-jersey2/genres/list/">Genres</a>
</li>
</ul>
</nav>
<decorator:body />
</body>
</html>
And finally, one of the JSPs I'm trying to get decorated with my navbar and styles
<%@page contentType="text/html"%>
<%@page pageEncoding="UTF-8"%>
<%@taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c"%>
<html>
<head>
<title></title>
</head>
<body>
<h2>Listing Movies</h2>
<table class="table table-striped">
<tr>
<th>Name</th>
<th>Starring</th>
<th>Genre</th>
</tr>
<c:forEach var="item" items="${it.movies}">
<tr>
<td>${item.name}</td>
<td>${item.starring}</td>
<td>${item.genre}</td>
</tr>
</c:forEach>
</table>
</body>
</html>
Any ideas on this would be greatly appreciated.
Regards
I was missing this line in my my_navbar.jsp
file:
<%@ taglib prefix="decorator" uri="http://www.opensymphony.com/sitemesh/decorator" %>
Now it works properly.