【问题标题】:f:param is null in bean jsff:param 在 bean jsf 中为空
【发布时间】:2012-01-23 05:38:21
【问题描述】:

我的问题是bean中参数的值为null

在 xhtml 中我有这个代码:

<h:commandLink action="#{navigation.editNews}" value="#{new.title}">            
  <f:param name="newsId" value="#{new.id}" />      
</h:commandLink>

在导航中我重定向到 news.xhtml

public String editNews(){
    return "editNews";
}

这是 faces-config.xml 中的代码

<navigation-case>
  <from-action>#{navigation.editNews}</from-action>
  <from-outcome>editNews</from-outcome>
  <to-view-id>/news.xhtml</to-view-id>
  <redirect />
</navigation-case>

我有一个 bean,当我在 news.xhtml 中按下按钮时调用方法并尝试获取参数但它为空

FacesContext fc = FacesContext.getCurrentInstance();
Map<String,String> params = fc.getExternalContext().getRequestParameterMap();
String = params.get("newsId"); 

【问题讨论】:

    标签: java jsf redirect xhtml


    【解决方案1】:

    &lt;f:param&gt; 增加了一个请求参数。所以这个参数的生命周期正好是一个 HTTP 请求。您的导航案例中有一个&lt;redirect/&gt;,它基本上指示网络浏览器在给定位置发送 new 请求。此新请求不再包含该参数。

    你基本上有两个选择:

    1. 去掉导航箱中的&lt;redirect /&gt;

    2. 改为普通的 GET 请求。如果您使用的是 JSF2,请改用 &lt;h:link&gt;

      <h:link action="news" value="#{news.title}">            
          <f:param name="newsId" value="#{news.id}" />      
      </h:link>
      

      或者,如果您仍在使用 JSF 1.x(导航案例的使用或多或少暗示了这一点,因为它们在 JSF 2 中是多余的,这要归功于新的隐式导航功能;或者您必须阅读过时的教程/书籍/针对 JSF 1.x 的答案;您的问题中缺少 JSF 2.0 标记也是可疑的),然后改用普通的&lt;a&gt; 链接。

      <a href="news.xhtml?newsId=#{news.id}">#{news.title}</a>
      

    另见:

    【讨论】:

    • 这个答案+1。还有一种选择;如果在 JSF 2 上,请提供带有重定向的参数 - return "news.xhtml?faces-redirect=true&newsId=" + paramMap.get("newsId")(