问题描述
我需要将一个参数(POST)传递给@managedBean,我使用了这样的托管属性:
@ManagedProperty (value =#{param.id})
private int id;
Bean的范围是ViewScope
我该怎么办?
arjan看看:
我的页面:
Facelet标题
< form method =postaction =faces / index.xhtml>
< input name =idvalue =4/>
< input type =submitvalue =submit/>
< / form>
< h:form>
< h:commandLink value =clickaction =index>
< f:param id =idname =idvalue =20/>
< / h:commandLink>
< / h:form>
两种方式:
-
使bean请求作用域并将视图范围注入另一个
@ManagedProperty
。@ManagedBean
@RequestScoped
public class RequestBean {
@ManagedProperty(value =#{param.id})
private Integer id;
@ManagedProperty(value =#{viewBean})
private ViewBean viewBean;
}
视图范围bean在
@PostConstruct 和请求范围bean的操作方法。您只需要记住,在没有参数的情况下回发到同一视图时,
id
可能会丢失。 -
或者,在bean初始化期间从请求参数映射中手动获取它。
@ManagedBean
@ViewScoped
公共类ViewBean {
private Integer id;
@PostConstruct
public void init(){
id = Integer.valueOf(FacesContext.getCurrentInstance()。getExternalContext()。getRequestParameterMap()。get(id) );
}
}
这样初始
id
在整个视图范围内可用。
I need to pass a parameter (POST) to a @managedBean, I used managed properties like this:
@ManagedProperty(value = "#{param.id}")
private int id;
And the scope of the Bean is ViewScope
I end up with this error:
What can I do?
arjan take a look:
My page: Facelet Title
<form method="post" action="faces/index.xhtml">
<input name="id" value="4" />
<input type="submit" value="submit" />
</form>
<h:form>
<h:commandLink value="click" action="index">
<f:param id="id" name="id" value="20"/>
</h:commandLink>
</h:form>
Two ways:
Make the bean request scoped and inject the view scoped one as another
@ManagedProperty
.@ManagedBean @RequestScoped public class RequestBean { @ManagedProperty(value="#{param.id}") private Integer id; @ManagedProperty(value="#{viewBean}") private ViewBean viewBean; }
The view scoped bean is available during
@PostConstruct
and action methods of request scoped bean. You only need to keep in mind that theid
can get lost when you do a postback to the same view without the parameter.Or, grab it manually from the request parameter map during bean's initialization.
@ManagedBean @ViewScoped public class ViewBean { private Integer id; @PostConstruct public void init() { id = Integer.valueOf(FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id")); } }
This way the initial
id
is available during the entire view scope.
这篇关于非请求范围Bean中的@ManagedProperty(value ="#{param.id}")的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!