How to get request parameters from view scoped JSF bean?

落花浮王杯 提交于 2019-12-10 04:01:52

问题


I have the view scoped bean which should access on init (@PostConstruct) the values from request URL and store them within its lifetime.

I've learned, that in order to get the values from http request, I need the following code:

@ManagedProperty("#{param.x}")
private int x;

Which gives me the value of attribute X. However, I can do that trick only in request scoped bean. Injecting this bean via @ManagedProperty to my bean also will not work. So, how to get access to that bean in view scoped bean?


回答1:


Use <f:viewParam> in the view.

<f:metadata>
    <f:viewParam name="x" value="#{bean.x}" />
</f:metadata>

Additional advantage is that it allows fine grained conversion and validation.

Note that the set value is not available during postconstruct. So if you'd like to perform initialization based on the value, use either a converter or preRenderView listener.

See also:

  • ViewParam vs @ManagedProperty(value = "#{param.id}")



回答2:


I had the same issue, I've had success by retrieving the value programmatically from the FacesContext:

@PostConstruct
public void init() {
    String value = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get(key);
}


来源:https://stackoverflow.com/questions/13495797/how-to-get-request-parameters-from-view-scoped-jsf-bean

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