首先,在处理我的问题之前,我将向您介绍我的测试用例.
我的基本Maven Web应用程序中有一些组件:
> page.xhtml:用于生成我的请求/回复(以启动过滤)
> Pretty Faces:用于根据客户的需要重新定义URL
> FirstFilter:在Pretty Faces之前执行(在此体育场内进行测试)
> ThirdFilter:在Pretty Faces之后执行(在此体育场内进行测试)
> web.xml:定义完整Filter Chain的行为
我将分享重要组件的代码.
漂亮-config.xml中
<url-mapping id="page">
<pattern value="/page" />
<view-id value="/page.xhtml" />
</url-mapping>
FirstFilter.java
@WebFilter
public class FirstFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("FirstFilter, request: " +
((HttpServletRequest)request).getRequestURL().toString());
chain.doFilter(request, response);
System.out.println("FirstFilter, response");
}
// override init and destroy
}
ThirdFilter.java
@WebFilter
public class ThirdFilter implements Filter {
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException {
System.out.println("ThirdFilter, request: " +
((HttpServletRequest)request).getRequestURL().toString());
chain.doFilter(request, response);
System.out.println("ThirdFilter, response");
}
// override init and destroy
}
web.xml中
<filter>
<filter-name>FirstFilter</filter-name>
<filter-class>nl.mhoogeveen.nl.rootapplication.FirstFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>FirstFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter>
<filter-name>Pretty Filter</filter-name>
<filter-class>testingapplications.filterchaining.PrettyFilter</filter-class>
<async-supported>true</async-supported>
</filter>
<filter-mapping>
<filter-name>Pretty Filter</filter-name>
<url-pattern>/*</url-pattern>
<dispatcher>FORWARD</dispatcher>
<dispatcher>REQUEST</dispatcher>
<dispatcher>ERROR</dispatcher>
<dispatcher>ASYNC</dispatcher>
</filter-mapping>
<filter>
<filter-name>ThirdFilter</filter-name>
<filter-class>testingapplications.filterchaining.ThirdFilter</filter-class>
</filter>
<filter-mapping>
<filter-name>ThirdFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
情况
调用localhost:8080 / page.xhtml(因此不激活Pretty Faces重定向)
INFO: FirstFilter, request: http://localhost:8080/page.xhtml
INFO: ThirdFilter, request: http://localhost:8080/page.xhtml
INFO: ThirdFilter, response
INFO: FirstFilter, response
调用localhost:8080 / page(从而激活Pretty Faces重定向)
INFO: FirstFilter, request: http://localhost:8080/page
INFO: FirstFilter, response
题
是什么原因导致我的链条不完整?它不会被切断,因为我仍然得到我对FirstFilter的回复.它似乎永远不会达到ThirdFilter.
我的web.xml有什么问题吗,我错过了一个调度员吗?
提前致谢.
解决方法:
您的调度程序设置不正确.让我解释一下会发生什么:
对/ page的请求进入并由FirstFilter首先处理.之后,PrettyFaces拦截请求并将其转发到/page.xhtml.此转发的请求作为新请求处理,因此再次考虑过滤器链.但是,您的过滤器没有任何调度程序设置,这与设置< dispatcher> REQUEST< / dispatcher>相同.在此配置中,过滤器仅应用于常规请求,但不应用于转发请求.
如果您还要对转发的请求应用过滤器,则必须添加< dispatcher> FORWARD< / dispatcher>到过滤器配置.
这也是您通常必须调整第三方过滤器(如MyFaces Tomahawk / PrimeFaces等)的调度程序设置的原因.请参阅常见问题解答中的问题2:
http://ocpsoft.org/prettyfaces/#section-16