Servlet Filter无法访问基于JSF的应用程序中的css,资源和主要表面

时间:2017-01-19 15:45:54

标签: css jsf servlets primefaces

我有一个ServletFilter,我想通过转发将我的应用程序置于维护模式,我甚至尝试包括一个facelet页面,但无论如何我都无法获得facelet页面以包含资源(images,css)

我知道这个问题Browser can't access/find relative resources like CSS, images and links when calling a Servlet which forwards to a JSP,但答案是使用jsp页面并且不使用h:head h:body,所以我从那篇文章中尝试过的东西还没有得到它的工作

public class OfflineFilter implements Filter {

    @Override
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
    LocalTime now = LocalTime.now();
    LocalTime start = LocalTime.parse("06:45:00");
    LocalTime stop = LocalTime.parse("07:00:00");

    // Want to go into maintenance mode every morning between 6:45 and 7:00 am
        if (now.isAfter(start) && now.isBefore(stop)) {
        // ((HttpServletResponse) response).sendRedirect(((HttpServletRequest) request).getContextPath() + "/maintenance.xhtml");
           request.getRequestDispatcher("maintenance.xhtml").forward(request, response);
        // request.getRequestDispatcher("maintenance.xhtml").include(request, response);
        } else {
           chain.doFilter(request, response);
        }
    }
}

maintenance.xhtml

<h:head>
    <title>Maintenance</title>
    <h:outputStylesheet name="#{pageContext.request.contextPath}/css/screen.css" />
</h:head>

<h:body>
    <h:outputStylesheet name="#{pageContext.request.contextPath}/css/screen.css" />
    test 0<img src="/AppName/resources/gfx/bug.png" />
    test 1<h:graphicImage styleClass="bug_logo" name="gfx/bug.png" />
    test 2<h:graphicImage styleClass="bug_logo" name="/gfx/bug.png" />
    test 3<h:graphicImage styleClass="bug_logo" name="resources/gfx/bug.png" />
    test 4<h:graphicImage styleClass="bug_logo" name="#{pageContext.request.contextPath}/gfx/bug.png" />
    <p:clock pattern="HH:mm:ss a" mode="server" />

即使只使用简单的HTML我也无法让事情发挥作用。

另外,我甚至认为我的时钟小部件都没有加载Primefaces样式表。

<html xmlns="http://www.w3.org/1999/xhtml" xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:h="http://java.sun.com/jsf/html" xmlns:f="http://java.sun.com/jsf/core"
xmlns:p="http://primefaces.org/ui" xmlns:o="http://omnifaces.org/ui"
xmlns:of="http://omnifaces.org/functions">

修改

查看我看到<img src="/AppName/javax.faces.resource/gfx/bug.png.xhtml"

的网页来源

但是我的过滤器匹配*.xhtml原文我在简单的html页面中将网址/*更改为*.xhtml固定的内容,但仍然无法使facelet工作。

<filter-mapping>
    <filter-name>OfflineFilter</filter-name>
    <url-pattern>*.xhtml</url-pattern>
</filter-mapping>

所以有问题,<h:graphicImage正在将.xhtml添加到我的所有资源中,并且它们被排除在外。

1 个答案:

答案 0 :(得分:4)

请记住排除资源!!

但是,我要去重定向:

/*@ngInject*/

请注意private static final String MAINTENANCE_PAGE = "/maintenance.jsf"; @Override public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException { HttpServletRequest request = (HttpServletRequest) req; HttpServletResponse response = (HttpServletResponse) res; String servletPath = request.getServletPath(); LocalTime now = LocalTime.now(); LocalTime start = LocalTime.parse("06:45:00"); LocalTime stop = LocalTime.parse("07:00:00"); // Want to go into maintenance mode every morning between 6:45 and 7:00 am if(now.isAfter(start) && now.isBefore(stop) && !servletPath.startsWith(MAINTENANCE_PAGE) && !servletPath.startsWith(ResourceHandler.RESOURCE_IDENTIFIER)) { response.sendRedirect(request.getContextPath() + MAINTENANCE_PAGE); return; } chain.doFilter(req, res); } 应在客户端模式下解决;换句话说,你在Faces Servlet之前,所以要注意它的映射(MAINTENANCE_PAGE/faces/*或其他)。