Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using SiteMesh with RequestDispatcher's forward()

I'm attempting to integrate SiteMesh into a legacy application using Tomcat 5 as my a container. I have a main.jsp that I'm decorating with a simple decorator.

In decorators.xml, I've just got one decorator defined:

<decorators defaultdir="/decorators">
  <decorator name="layout-main" page="layout-main.jsp">
    <pattern>/jsp/main.jsp</pattern>
  </decorator>
</decorators>

This decorator works if I manually go to http://example.com/my-webapp/jsp/main.jsp. However, there are a few places where a servlet, instead of doing a redirect to a jsp, does a forward:

getServletContext().getRequestDispatcher("/jsp/main.jsp").forward(request, response);

This means that the URL remains at something like http://example.com/my-webapp/servlet/MyServlet instead of the jsp file and is therefore not being decorated, I presume since it doesn't match the pattern in decorators.xml.

I can't do a <pattern>/*</pattern> because there are other jsps that do not need to be decorated by layout-main.jsp. I can't do a <pattern>/servlet/MyServlet*</pattern> because MyServlet may forward to main.jsp sometimes and perhaps error.jsp at other times.

Is there a way to work around this without expansive changes to how the servlets work? Since it's a legacy app I don't have as much freedom to change things, so I'm hoping for something configuration-wise that will fix this.

SiteMesh's documentation really isn't that great. I've been working mostly off the example application that comes with the distribution. I really like SiteMesh, and am hoping I can get it to work in this case.

like image 679
Rob Hruska Avatar asked Mar 06 '09 17:03

Rob Hruska


1 Answers

My understanding is that SiteMesh is integrated into the application as a Servlet filter. By default, servlet filters are only invoked against the original incoming request (in your case, the request to the servlet). Subsequent forward or include requests are not passed throuh the filter, and therefore will not be passed through sitemesh.

You can, however, instruct the filter to be invoked on forwards, using something like this:

<filter-mapping>
    <filter-name>sitemesh</filter-name>
    <servlet-name>MyServlet</servlet-name>
    <dispatcher>FORWARD</dispatcher>
</filter-mapping>

Which instructs the container to only operate on FORWARD requests. The other options are INCLUDE and REQUEST, you can have several elements.

So your options are to either change your filter config to specify FORWARD, or to change your filter-mapping to match the servlet path, rather than the JSP path. Either one should work.

like image 146
skaffman Avatar answered Nov 03 '22 07:11

skaffman