在JSF2中将类似逻辑的过滤器放在哪里 [英] where to put filter like logic in JSF2

查看:56
本文介绍了在JSF2中将类似逻辑的过滤器放在哪里的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我目前正在努力在Java EE 6 / JSF 2 Web应用程序中放置一些授权内容的通用逻辑:
我有一个要求,即特定cookie附带的所有请求都应重定向

I am currently banging my head where to put a common logic for some authorization stuff in my Java EE 6/JSF 2 webapp: I have the requirement, that all requests that come with a specific cookie should be redirected to another page.

我考虑了3种解决方案:

I considered 3 solutions:

1)使用Servlet 3.0过滤器( @WebFilter)
这行得通,我也可以将我的托管bean注入那里,但是托管bean需要访问faces externalContext,在过滤器调用时尚未设置它,所以我得到了NPE调用托管bean

1) use a servlet 3.0 filter (@WebFilter) this worked, i also could inject my managed beans there, but the managed beans require access to the faces externalContext, which at filter invocation time has not yet been set up, so i got NPE's calling the managed beans

2)使用阶段侦听器
感觉很尴尬,因为阶段侦听器不能是CDI组件,并且因此无法注入其他组件(通过el-evaluation除外);在我看来,phaseListener对我来说是技术上的,可以将导航逻辑放入其中。

2) use a phase listener this feels awkward, because a phase listener cannot be a CDI component and so cannot inject other components (except via el-evaluation); a phaseListener for me feels to technical to put navigation logic into it.

3)在Seam 2.0中,我可以使用页面操作 像这样,但是这个概念似乎并没有融入到JSF 2.0中。

3) in Seam 2.0 i could used "page actions" for things like this, but it seems that this concept didn't make it into JSF 2.0

在接缝中看起来像这样:

in seam this looked like:

<page view-id="/admin/*.jsf">
    <action execute="#{authenticator.checkAccess()}" />
</page>

是真的吗,JSF 2.0在渲染a之前没有执行控制器逻辑的概念

Is it really, that JSF 2.0 does not have a concept to execute "controller logic" before rendering a page?

推荐答案

在JSF 2.x中,您可以收听如下页面事件:

In JSF 2.x you can listen to page events like that:

<f:metadata>
   <f:event type="javax.faces.event.PreRenderViewEvent" listener="#authenticator.checkAccess()}" />
</f:metadata>

这或多或少等同于Seam 2页面操作(不过,您必须过滤回发) 。您可以使用接缝面孔。如果您查看文档并查看适合您的需求,这可能是个好主意...

That is more or less equivalent to Seam 2 page action (you will have to filter out postbacks though). You can further enhance the default behavior with CDI-extensions like Seam Faces. Probably it's a good idea if you have a look at the documentation and see what fits your needs...

这篇关于在JSF2中将类似逻辑的过滤器放在哪里的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆