2

我一直在阅读这个类似的 SO question,但建议的一些方法似乎对我不起作用。我的堆栈是JSF2.0 (+ PrimeFaces),我部署到JBoss 7 AS

有一个Servlet将请求分派到xhtml页面(在同一个war 中),但后者无法检索那里设置的属性值。

这是 Servlet 代码片段:

protected void doPost(HttpServletRequest request, HttpServletResponse response)
 throws ServletException, IOException {
    (...)
    request.setAttribute("foo", "foo test");
    RequestDispatcher dispatcher = getServletContext().getRequestDispatcher(url);
    dispatcher.forward(request, response);                                    
}

这是xhtml页面上的代码:

<p><h:outputText value="#{sessionScope['foo']}" /></p>   
<p><h:outputText value="#{param['foo']}" /></p>                          
<p><h:outputText value="#{request.getParameter('foo')}" /></p>

其中在这三种情况中的任何一种情况下都没有出现。

DID 的工作是使用支持 bean(如对参考 SO 文章的响应中所建议的那样),其中属性是在@PostConstruct方法中获得的,如下所示:

@PostConstruct
public void init() {
    HttpServletRequest request = (HttpServletRequest)FacesContext.getCurrentInstance()
                                               .getExternalContext().getRequest();
    message = (String) request.getAttribute("foo");
}  

...其中检索到的值随后在xhtml页面中可用。

但是为什么一种方法有效,而另一种无效?

4

1 回答 1

3

问题是您在 servlet 的请求范围内设置了一个属性,但在您正在编写request.getParameter("foo")sessionScope['foo']访问它的 xhtml 页面中。

在 xhtml 页面中写下:#{requestScope.foo}它会显示属性 foo 的值。

于 2012-08-28T16:16:30.050 回答