我的豆子有这个:
@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
bean。
对于@ViewScoped
bean,我发现我必须这样做FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id")
才能获取id
参数的值。
这是获取请求参数值的唯一方法@ViewScoped
吗?
有什么想法吗?