解决Spring错误时如何排除sitemesh过滤器?

发布于 2024-11-07 12:17:06 字数 546 浏览 0 评论 0原文

我有一个可以装饰页面的 Sitemesh 过滤器。我已经配置了 Spring 的 exceptionResolver ,以便所有错误都将转到名为 error 的视图,然后指向 WEB-INF/jsp/error.jsp< /code> 通过 InternalResourceViewResolver

现在错误页面由 sitemesh 装饰,我想将其从装饰中排除。使用 sitemesh decorator.xml 标签不起作用。因为传入的 url 可能与 /app/login.html 一样正常,并且 sitemesh 已经捕获它并装饰它。

我注意到,在 Spring 中,如果我有一个用于 ajax 请求的 @ResponseBody,它将绕过 Sitemesh 的装饰。我想知道它是如何工作的?我可以在 errorResolver 中做一些事情来绕过 sitemesh 吗?

I have a Sitemesh filter that will decorate pages. I have configured a Spring's exceptionResolver so that all the error will go to a view called error which is then pointed to WEB-INF/jsp/error.jsp through InternalResourceViewResolver.

Now the error page is decorated by sitemesh and I would like to exclude it from decoration. Using <exclude> tag of sitemesh decorator.xml does not work. Because the incoming url may be something as normal as /app/login.html and sitemesh already catch it and decorate it.

I notice that in Spring if I have a @ResponseBody for ajax request, it would by pass Sitemesh's decoration. I wonder how it works? Can I make something in the errorResolver to bypass sitemesh also?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

丢了幸福的猪 2024-11-14 12:17:06

可以通过实现自己的 exceptionResolver 来完成,手动流式输出并返回 null ModelAndView

public class MyExceptionResolver extends SimpleMappingExceptionResolver{
public ModelAndView resolveException(HttpServletRequest request,
        HttpServletResponse response, Object handler, Exception ex) {

    //other things like logging...
    RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/error.jsp");
    try {
        dispatcher.forward(request, response);
        response.getOutputStream().flush();
    } catch (ServletException e) {
        log.error(e);
    } catch (IOException e) {
        log.error(e);
    }
    return null;
}

It can be done by imlementing own exceptionResolver, streaming output manually and return null ModelAndView

public class MyExceptionResolver extends SimpleMappingExceptionResolver{
public ModelAndView resolveException(HttpServletRequest request,
        HttpServletResponse response, Object handler, Exception ex) {

    //other things like logging...
    RequestDispatcher dispatcher = request.getRequestDispatcher("/WEB-INF/jsp/error.jsp");
    try {
        dispatcher.forward(request, response);
        response.getOutputStream().flush();
    } catch (ServletException e) {
        log.error(e);
    } catch (IOException e) {
        log.error(e);
    }
    return null;
}
小猫一只 2024-11-14 12:17:06

至少在 SiteMesh 3 中,这种类型的排除有效(sitemesh3.xml)

<sitemesh>
  <mime-type>text/html</mime-type>

  <mapping path="/*" decorator="/WEB-INF/sitemesh/decorator.jsp" />
  <mapping path="/app/login.html" exclude="true"/>

</sitemesh>

这是在 Spring 3 中尝试的。希望这对您有所帮助。

At least in SiteMesh 3 this type of exclude works (sitemesh3.xml)

<sitemesh>
  <mime-type>text/html</mime-type>

  <mapping path="/*" decorator="/WEB-INF/sitemesh/decorator.jsp" />
  <mapping path="/app/login.html" exclude="true"/>

</sitemesh>

This is tried in Spring 3. I hope this helped you.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文