简体   繁体   English

f:bean jsf中的param为null

[英]f:param is null in bean jsf

My problem is that the value of the parameter is null in the bean 我的问题是bean中参数的值为null

In the xhtml I have this code: 在xhtml中我有这个代码:

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

In Navigation I redirect to news.xhtml 在导航中,我重定向到news.xhtml

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

This is the code in faces-config.xml 这是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>

I have a bean where I call method when I push a button in news.xhtml and I try to get param but it is null 当我在news.xhtml中按下一个按钮时,我有一个bean调用方法,我尝试获取param,但它是null

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

The <f:param> adds a request parameter. <f:param>添加一个请求参数。 So this parameter has a lifetime of exactly one HTTP request. 因此,此参数的生命周期恰好为一个HTTP请求。 You've a <redirect/> in your navigation case which basically instructs the webbrowser to send a new request on the given location. 您在导航案例中有一个<redirect/> ,它基本上指示webbrowser在给定位置发送请求。 This new request does not contain the parameter anymore. 此新请求不再包含该参数。

You've basically 2 options: 你基本上有两个选择:

  1. Get rid of <redirect /> in the navigation case. 摆脱导航案例中的<redirect />

  2. Make it a normal GET request instead. 改为使其成为正常的GET请求。 If you're on JSF2, use <h:link> instead. 如果您使用的是JSF2,请改用<h:link>

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

    Or if you're still on JSF 1.x (the usage of navigation cases less or more hints this as they are superfluous in JSF 2 thanks to the new implicit navigation feature; or you must be reading outdated tutorials/books/answers targeted on JSF 1.x; also the absence of JSF 2.0 tag on your question is suspicious), then use a normal <a> link instead. 或者如果你仍然使用JSF 1.x(由于新的隐式导航功能,导航案例的使用少了或多了一些,因为它们在JSF 2中是多余的;或者你必须阅读过时的教程/书籍/答案JSF 1.x;在你的问题上缺少JSF 2.0标签是可疑的),然后使用普通的<a>链接。

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

See also: 也可以看看:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM