私の豆にはこれがあります:
@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>
テストを通じて、私はそれに気づき、Bean@ManagedProperty
で@PostConstruct
のみ動作することに気付きました。@RequestScoped
Bean の場合、パラメーターの値を取得する@ViewScoped
にはこれを行う必要があることがわかりました。FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("id")
id
これは、リクエスト パラメータの値を取得する唯一の方法@ViewScoped
ですか?
何かご意見は?