【问题标题】:Accessing SessionScoped Beans in JSF pages在 JSF 页面中访问 SessionScoped Bean
【发布时间】:2013-09-25 21:36:19
【问题描述】:

我在我的应用程序中使用 SessionScoped 托管 Bean:

@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()}">

在确认页面,我想显示这个数据。

确认.xhtml

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

但是,我没有在这里显示任何值。 我尝试将这些值放入 showConfirm() 方法中的 sessionMap 中。

public String showConfirm() {

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

return "confirm";

}

但是,我也无法在 confirm.xhtml 中查看这些值。

仅当我使用 &lt;h:outputFormat value="#{sessionScope.sampleBean.value}"&gt; 时,才会显示这些值。 此外,我只想使用 SessionScope 来执行此操作,因为所有这些都是带有会话的更大应用程序的一部分。 有没有替代方法?

【问题讨论】:

  • 您忘记使用&lt;h:form&gt; 标签了吗?
  • 谢谢,但我使用了&lt;h:form&gt; 标签。我相信我在控制器中做错了什么。

标签: session jsf-2 managed-bean facescontext


【解决方案1】:

您可以通过控制器 bean 从视图中访问 sessionScoped bean:

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

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

【讨论】:

  • 我尝试在confirm 页面中提供&lt;h:outputFormat value="#{sampleController.sampleBean.value}"&gt;,但它仍然没有显示任何值。
  • 也许视图中有些东西想念你。你能发布视图的代码吗?
  • 我尝试使用 &lt;h:outputFormat value="#{sessionScope}"&gt; 打印 sessionScope 我的 SampleController 未显示在 sessionscope 中。我相信我在 SampleController 中做错了什么。
  • 托管bean构造函数可以吗?我试过你的精确例子,它运行良好,你能把你的两个托管bean的整个代码+视图吗?
【解决方案2】:

您的会话范围 bean 应该实现 Serializable 接口才能正常工作,请参阅this

【讨论】:

  • 问题的所有者没有指出检测到的任何异常;此外,该示例在我没有任何Serializable 的实现的情况下运行良好。
  • 我尝试使用Serializable 接口实现,但它不起作用。
猜你喜欢
  • 1970-01-01
  • 2022-01-25
  • 1970-01-01
  • 1970-01-01
  • 2016-08-02
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多