CDI params in @PostConstruct

送分小仙女□ 提交于 2019-12-06 08:52:09

The lifecycle doesn't allow your approach.

At first, the bean is created ( constructor ). After that, there is executed dependency injection which is followed by @PostConstruct method and after that the JSF file is evaluated. And the viewParam is in that file. So you have to register another listener which is called after filling view params.

I have a solution for @RequestScope beans, but if the bean's scope is longer ( like View ) then this method is executed after each request ( including AJAX ) which is not probably desired.

Use this for request scope beans:

<f:metadata>
   <f:viewParam id="id" name="id" value="#{detailsBean.id}"/>
   <f:event type="preRenderView" listener="#{detailsBean.onLoad}" />
</f:metadata>

For @ViewScope beans I am using this "hack" which works but probably is not best practise. It does same thing but probably it isn't the correct approach.

#{detailsBean.onLoad()}
<f:metadata>
   <f:viewParam id="id" name="id" value="#{detailsBean.id}"/>
</f:metadata>

I hope that this is helpful for you.


EDIT:

you are using a lot of AJAX here. This calls have to land in at least ViewScoped beans. View Scope is similar to RequestScope, but it takes a quite longer - til the page is left.

But I haven't read it all, there is a lot of code and if the ViewScope doesn't help then maybe you should provide the small piece of problematic code to be chance there to find and focus on the real problem.

I encountered the exact same problem than you, and solved it by using the external context (containing GET parameters) instead of f:viewParam.

In your @PostConstruct method, just get your parameter with something like

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