簡體   English   中英

在JSF頁面中訪問SessionScoped Bean

[英]Accessing SessionScoped Beans in JSF pages

我在我的應用程序中使用SessionScoped Managed Beans:

@ManagedBean
@SessionScoped
public class SampleBean{

private String value;

//getters and setters

}

我有我的控制器:

@ManagedBean
@SessionScoped
public class SampleController{

@ManagedProperty(value = "#{sampleBean}")
private SampleBean sampleBean;

public String showConfirm() {

return "confirm";

}

public String showComplete() {

return "complete";

}

//getters and setters
}

邏輯是,我有一個輸入值的啟動頁面,然后進入確認頁面,然后最終進入完成頁面。 我必須在其余頁面中顯示在啟動頁面中輸入的數據。

啟動頁面如下:

startup.xhtml

<h:inputText value="#{sampleBean.value}">
<h:commandLink value="Confirm"  action="#{sampleController.showConfirm()}">

我要在確認頁面上顯示此數據。

Confirm.xhtml

<h:outputFormat value="#{sampleBean.value}">

但是,我沒有在這里顯示任何值。 我嘗試將這些值放入showConfirm()方法的sessionMap中。

public String showConfirm() {

FacesContext context = FacesContext.getCurrentInstance().getExternalContext().getSessionMap().put("sampleBean", sampleBean);

return "confirm";

}

但是然后,我也無法在confirm.xhtml中查看這些值。

僅當我使用<h:outputFormat value="#{sessionScope.sampleBean.value}"><h:outputFormat value="#{sessionScope.sampleBean.value}">顯示這些值。 另外,我只想使用SessionScope來做到這一點,因為所有這些都是具有會話的更大應用程序的一部分。 有替代方法嗎?

您可以通過以下方式通過控制器bean從視圖訪問sessionScoped bean:

<h:inputText value="#{sampleController.sampleBean.value}">

通過在控制器bean中添加此托管屬性的getter / setter。

會話范圍的bean應該實現Serializable接口才能正常工作看到

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM