将 SiteMesh 与 RequestDispatcher 的forward() 结合使用
我正在尝试使用 Tomcat 5 作为容器将 SiteMesh 集成到旧应用程序中。 我有一个 main.jsp
,我用一个简单的装饰器来装饰它。
在decorators.xml
中,我刚刚定义了一个装饰器:
<decorators defaultdir="/decorators">
<decorator name="layout-main" page="layout-main.jsp">
<pattern>/jsp/main.jsp</pattern>
</decorator>
</decorators>
如果我手动转到http://example.com/my-webapp/jsp/main.jsp<,则该装饰器可以工作/代码>。 然而,有一些地方 servlet 不是重定向到 jsp,而是执行 forward:
getServletContext().getRequestDispatcher("/jsp/main.jsp").forward(request, response);
这意味着 URL 仍为 http://example.com /my-webapp/servlet/MyServlet
而不是 jsp 文件,因此没有被装饰,我推测是因为它与 decorators.xml
中的模式不匹配。
我无法执行
因为还有其他 jsps 不需要通过 layout-main.jsp
进行装饰。 我无法执行
因为 MyServlet
有时可能会转发到 main.jsp
其他时候可能还有 error.jsp
。
有没有一种方法可以解决这个问题,而无需对 servlet 的工作方式进行大量更改? 由于它是一个遗留应用程序,我没有太多的自由来改变事物,所以我希望有一些配置方面的东西可以解决这个问题。
SiteMesh 的文档确实不是那么好。 我主要使用发行版附带的示例应用程序进行工作。 我真的很喜欢 SiteMesh,并且希望我能让它在这种情况下发挥作用。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我的理解是SiteMesh作为Servlet过滤器集成到应用程序中。 默认情况下,仅针对原始传入请求(在您的情况下是对 servlet 的请求)调用 servlet 过滤器。 后续转发或包含请求不会通过过滤器,因此不会通过 sitemesh 传递。
但是,您可以指示过滤器在转发时调用,使用如下所示:
这指示容器仅对 FORWARD 请求进行操作。 其他选项是 INCLUDE 和 REQUEST,您可以有多个元素。
因此,您的选择是更改过滤器配置以指定 FORWARD,或者更改过滤器映射以匹配 servlet 路径,而不是 JSP 路径。 任何一个都应该有效。
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:
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.