where to put filter like logic in JSF2

可紊 提交于 2019-12-11 05:34:21

问题


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.

I considered 3 solutions:

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) 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) 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>

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


回答1:


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>

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...



来源:https://stackoverflow.com/questions/10008709/where-to-put-filter-like-logic-in-jsf2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!