我一直在阅读这个类似的 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页面中可用。
但是为什么一种方法有效,而另一种无效?