JSF 中的拦截器 [英] Interceptor in JSF
问题描述
我想知道JSF中是否有拦截器(就像我们在Spring中使用的一样),我们如何实现它?
I want to know if there is an interceptor in JSF (like we use in Spring), and how to do we implement it?
推荐答案
您可以实施 PhaseListener
为此.您可以将它们编程为在 特定的 JSF 阶段 您在覆盖的 getPhaseId()
方法.您可以通过 beforePhase()
和 afterPhase()
方法.
You could implement a PhaseListener
for this. You could program them to listen on a specific JSF phase which you specify in the overridden getPhaseId()
method. You can intercept on the before and after phase events by beforePhase()
and afterPhase()
methods.
以下示例监听渲染响应阶段:
The below example listens on the render response phase:
public class RequestInterceptor implements PhaseListener {
@Override
public PhaseId getPhaseId() {
return PhaseId.RENDER_RESPONSE;
}
@Override
public void beforePhase(PhaseEvent event) {
// Do your job here which should run before the render response phase.
}
@Override
public void afterPhase(PhaseEvent event) {
// Do your job here which should run after the render response phase.
}
}
要让它运行,您需要在的
文件.您可以有多个
部分将其注册为
faces-config.xml
.
To get it to run, you need to register it as a <phase-listener>
in the <life-cycle>
section of the faces-config.xml
file. You can have multiple <phase-listener>
s.
<lifecycle>
<phase-listener>com.example.RequestInterceptor</phase-listener>
</lifecycle>
您可以指定PhaseId.
让阶段侦听器在每个 JSF 阶段上运行(注意不一定 所有 总是会被执行,这取决于请求类型).如有必要,您可以通过 getPhaseId()
中的 ANY_PHASEPhaseEvent#getPhaseId()
.
You can specify PhaseId.ANY_PHASE
in getPhaseId()
to let the phase listener run on every single JSF phase (note that not necessarily all of them will always be executed, that depends on the request type). You can if necessary get the current phase ID in the before and after phase methods by PhaseEvent#getPhaseId()
.
public class PhaseDebugger implements PhaseListener {
@Override
public PhaseId getPhaseId() {
return PhaseId.ANY_PHASE;
}
@Override
public void beforePhase(PhaseEvent event) {
System.out.println("Before phase " + event.getPhaseId());
}
@Override
public void afterPhase(PhaseEvent event) {
System.out.println("After phase " + event.getPhaseId());
}
}
或者,Filter
<如果您想要一个更全局的钩子,/a> 应该同样有效(因此您对 JSF 请求/响应并不完全感兴趣,并且您不需要 FacesContext
中的任何内容).
Alternatively, a Filter
should work equally good if you want a more global hook (and thus you're not exactly interested in JSF requests/responses and you do not need anything from the FacesContext
).
@WebFilter("/*")
public class RequestInterceptor implements Filter {
@Override
public void init(FilterConfig config) {
// Initialize global variables if necessary.
}
@Override
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws ServletException, IOException {
// Do your job here which should run before the request processing.
chain.doFilter(request, response);
// Do your job here which should run after the request processing.
}
@Override
public void destroy() {
// Cleanup global variables if necessary.
}
}
这篇关于JSF 中的拦截器的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!