Men*_*nno 3 java jsf chaining servlet-filters prettyfaces
首先,在处理我的问题之前,我将向您介绍我的测试用例.我的基本Maven Web应用程序中有一些组件:
我将分享重要组件的代码.
漂亮-config.xml中
<url-mapping id="page">
<pattern value="/page" />
<view-id value="/page.xhtml" />
</url-mapping>
Run Code Online (Sandbox Code Playgroud)
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
}
Run Code Online (Sandbox Code Playgroud)
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
}
Run Code Online (Sandbox Code Playgroud)
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>
Run Code Online (Sandbox Code Playgroud)
情况
调用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
Run Code Online (Sandbox Code Playgroud)
调用localhost:8080/page(从而激活Pretty Faces重定向)
INFO: FirstFilter, request: http://localhost:8080/page
INFO: FirstFilter, response
Run Code Online (Sandbox Code Playgroud)
题
是什么原因导致我的链条不完整?它不会被切断,因为我仍然得到我对FirstFilter的回复.它似乎永远不会达到ThirdFilter.
我有什么问题吗web.xml,我错过了dispatcher吗?
提前致谢.
您的调度程序设置不正确.让我解释一下会发生什么:
请求/page进入并首先处理FirstFilter.之后,PrettyFaces拦截请求并将其转发给/page.xhtml.此转发的请求作为新请求处理,因此再次考虑过滤器链.但是您的过滤器没有任何与设置相同的调度程序设置<dispatcher>REQUEST</dispatcher>.在此配置中,过滤器仅应用于常规请求,但不应用于转发请求.
如果要将过滤器也应用于转发请求,则必须添加<dispatcher>FORWARD</dispatcher>到过滤器配置.
这也是您通常必须调整第三方过滤器(如MyFaces Tomahawk/PrimeFaces等)的调度程序设置的原因.请参阅常见问题解答中的问题2:
http://ocpsoft.org/prettyfaces/#section-16
| 归档时间: |
|
| 查看次数: |
1394 次 |
| 最近记录: |