4

My problem is that the value of the parameter is null in the bean

In the xhtml I have this code:

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

In Navigation I redirect to news.xhtml

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

This is the code in 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

FacesContext fc = FacesContext.getCurrentInstance();
Map<String,String> params = fc.getExternalContext().getRequestParameterMap();
String = params.get("newsId"); 
Stefan Ivanov
  • 115
  • 2
  • 5

1 Answers1

5

The <f:param> adds a request parameter. So this parameter has a lifetime of exactly one HTTP request. You've a <redirect/> in your navigation case which basically instructs the webbrowser to send a new request on the given location. This new request does not contain the parameter anymore.

You've basically 2 options:

  1. Get rid of <redirect /> in the navigation case.

  2. Make it a normal GET request instead. If you're on JSF2, use <h:link> instead.

    <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.

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

See also:

BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • +1 for this answer. There's one more option; if on JSF 2, provide the parameter with the redirect - return "news.xhtml?faces-redirect=true&newsId=" + paramMap.get("newsId")( – Mike Braun Dec 19 '11 at 09:49