#{param} 上的 @ManagedProperty 在 @ViewScoped 中不起作用
我的 bean 有这个:
@ManagedBean
@ViewScoped
public class BookBean implements Serializable
{
@ManagedProperty(value = "#{param.id}") // does not work with @ViewScoped
private String id;
public void init()
{
id = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id")
if (id != null) {
System.out.println("ID: " + id);
currentBook = bookService.find(id);
}
}
@PostConstruct
public void post()
{
// does not work with @ViewScoped
System.out.println("ID: " + id);
currentBook = bookService.find(id);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
目标 Facelet 有这个:
<f:metadata>
<f:viewParam name="id" value="#{bookBean.id}">
<f:event type="preRenderView" listener="#{bookBean.init}" />
</f:viewParam>
</f:metadata>
通过测试,我注意到 @ManagedProperty
和 @PostConstruct
仅适用于 @RequestScoped
豆。
对于 @ViewScoped
bean,我发现我必须执行 FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id")
才能获取id
参数的值。
这是使用 @ViewScoped 获取请求参数值的唯一方法吗?
有什么想法吗?
My bean has this:
@ManagedBean
@ViewScoped
public class BookBean implements Serializable
{
@ManagedProperty(value = "#{param.id}") // does not work with @ViewScoped
private String id;
public void init()
{
id = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id")
if (id != null) {
System.out.println("ID: " + id);
currentBook = bookService.find(id);
}
}
@PostConstruct
public void post()
{
// does not work with @ViewScoped
System.out.println("ID: " + id);
currentBook = bookService.find(id);
}
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
}
And the destination Facelet has this:
<f:metadata>
<f:viewParam name="id" value="#{bookBean.id}">
<f:event type="preRenderView" listener="#{bookBean.init}" />
</f:viewParam>
</f:metadata>
Through testing, I've noticed that @ManagedProperty
and @PostConstruct
only work with @RequestScoped
bean.
For @ViewScoped
bean, I found that I had to do this FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id")
to get the value of the id
parameter.
Is this the only way to get a request parameter's value with @ViewScoped
?
Any thoughts?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
视图范围比请求范围更广泛。
@ManagedProperty
只能设置与托管 bean 范围相比具有相同或更宽范围的属性。只需继续使用
与。你不应该将它们嵌套在一起。
<
f:viewParam> 将设置请求参数,
将在设置这些参数后执行侦听器方法。@PostConstruct
在视图作用域 bean 上也能正常工作,但它仅在 bean 构造并且设置所有依赖项注入后直接运行(例如@ManagedProperty
、@EJB
、@Inject
、@Resource
等)。然而
之后会设置该属性,因此它在@PostConstruct
中不可用。The view scope is broader than the request scope. The
@ManagedProperty
can only set properties which have the same or broader scope as compared to the managed bean's scope.Just keep using
<f:viewParam>
with<f:event>
. You should only not nest them in each other.with
The
<f:viewParam>
will set the request parameter and the<f:event>
will execute the listener method after setting of those parameters.The
@PostConstruct
works fine on view scoped beans as well, but it only runs directly after bean's construction and all dependency injections are been set (such as@ManagedProperty
,@EJB
,@Inject
,@Resource
, etc). The<f:viewParam>
however sets the property thereafter, so it's not available in@PostConstruct
.这是在 ViewScoped bean 中获取请求参数的另一种方法。这就是 #{param.device} 在 RequestScoped bean 中得到的内容。这样做的优点是在表示层中不需要任何标签。
Here is another method for getting a request parameter inside a ViewScoped bean. This would be what #{param.device} would get in a RequestScoped bean. This has the advantage of not requiring any tags in the presentation layer.