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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以实现
PhaseListener
< /a> 为此。您可以将它们编程为侦听特定的 JSF 阶段 您在重写的getPhaseId()
方法。您可以通过beforePhase()
和 <代码> afterPhase() 方法。下面的示例监听渲染响应阶段:
要使其运行,您需要将其注册为
中的faces-config.xml
文件的 > 部分。您可以有多个
。您可以指定
PhaseId.ANY_PHASE 中的 code>
让阶段侦听器在每个 JSF 阶段上运行(请注意,不一定所有都将始终执行,这取决于于请求类型)。如果需要,您可以通过
PhaseEvent#getPhaseId()
。或者,
Filter
如果您想要一个更全局的钩子(因此您对 JSF 请求/响应并不完全感兴趣,并且您不需要 FacesContext 中的任何内容),那么应该同样有效。You could implement a
PhaseListener
for this. You could program them to listen on a specific JSF phase which you specify in the overriddengetPhaseId()
method. You can intercept on the before and after phase events bybeforePhase()
andafterPhase()
methods.The below example listens on the render response phase:
To get it to run, you need to register it as a
<phase-listener>
in the<life-cycle>
section of thefaces-config.xml
file. You can have multiple<phase-listener>
s.You can specify
PhaseId.ANY_PHASE
ingetPhaseId()
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 byPhaseEvent#getPhaseId()
.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 theFacesContext
).