1

我正在使用 JSF 1.2 。我有一个小服务程序。当这个 servlet 被命中时,我从请求参数中获取数据,在 doPost 中,我需要在 bean 中设置它,以便我可以在 xhtml 页面上显示它。

我的代码如下所示。

userId= request.getParameter("userID");
MyBean myBean = new MyBean();
myBean.initialize(userId);

在 myBean 的初始化方法中,将 userId 值设置为 globalVariable。

在我的 bean 日志中,正在打印 globalVariable 值。但它没有显示在 xhtml 页面上。

在 doPost 方法中重定向到 xhtml 页面,如下所示,

RequestDispatcher dispatcher = request.getRequestDispatcher("/jsp/html/index.jsf");
dispatcher.forward(request, response);

在 index.xhtml 页面中,我有

<h:outputText value="#{myBean.globalVariable}"></h:outputText>

在我的相位监听器中,我没有做任何事情。我只有beforPhase 方法。

为什么我无法在 jsf 页面中打印值但能够在登录 bean 中打印值?

4

1 回答 1

1

在转发之前,您需要将 bean 放入作用域中,就在 JSF 期望它的位置。

如果它是请求范围的 bean,请使用HttpServletRequest#setAttribute().

request.setAttribute("myBean", myBean);
于 2012-07-20T12:05:22.290 回答