简体   繁体   English

如何从视图范围的JSF bean获取请求参数?

[英]How to get request parameters from view scoped JSF bean?

I have the view scoped bean which should access on init (@PostConstruct) the values from request URL and store them within its lifetime. 我有视图scoped bean,它应该在init(@PostConstruct)上访问请求URL中的值并将它们存储在其生命周期内。

I've learned, that in order to get the values from http request , I need the following code: 我已经了解到,为了从http请求中获取值,我需要以下代码:

@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. 这给了我属性X的值。但是,我只能在请求范围的 bean中做这个技巧。 Injecting this bean via @ManagedProperty to my bean also will not work. 通过@ManagedProperty将这个bean注入我的bean也行不通。 So, how to get access to that bean in view scoped bean? 那么,如何在视图范围内访问bean?

Use <f:viewParam> in the view. 在视图中使用<f:viewParam>

<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. 请注意,在postconstruct期间,设置值不可用。 So if you'd like to perform initialization based on the value, use either a converter or preRenderView listener. 因此,如果您想基于该值执行初始化,请使用转换器或preRenderView侦听器。

See also: 也可以看看:

I had the same issue, I've had success by retrieving the value programmatically from the FacesContext : 我遇到了同样的问题,通过FacesContext以编程方式检索值,我获得了成功:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM