Servlet Filter 无法访问基于 JSF 的应用程序中的 css、资源和素数



我有一个Servlet Filter,我想用它来通过转发将我的应用程序置于维护模式,我甚至尝试过包含一个facelet页面,但无论哪种方式,我都无法使facelet页面包含资源(图像、css)

我知道这个问题浏览器可以';当调用转发到JSP的Servlet时,我无法访问/查找相关资源,如CSS、图像和链接,但答案是使用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);
}
}
}

维护.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匹配。最初,我将url/*更改为*.xhtml,修复了普通html页面中的内容,但仍然无法使用facelet。

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

所以问题来了,<h:graphicImage正在将.xhtml附加到我的所有资源中,而它们却被排除在外。

记住要排除资源!!

然而,我会选择重定向:

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);
}

请注意,MAINTENANCE_PAGE应在客户端模式中寻址;换句话说,您在FacesServlet之前,所以要注意它的映射(/faces/**.jsf或其他什么)。

最新更新