0

I have AAA.jsf page with link:

<h:link value="To BBB" outcome="BBB">
  <f:param name="id" value="10"/>
</h:link>

BBB.jsf:

<html>
 <h:body>
   <f:metadata>
      <f:viewParam name="id"/>
   </f:metadata>
   <h:form>
     <h:outputText value="#{bean.id}"/> 

     <h:commandButton value="Make some logic.." action="bean.doSomething"/>
   </h:form>
 </h:body>
</html>

Bean.class:

@ManagedBean
@ViewScoped
class Bean{
  public String getId(){
    Map<String,String> param = FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap();
    return param.get("id");
  }

  public void doSomething(){
    //do some logic....
    //I don't use id parameter
  }
}

After I get from AAA page to BBB page and click on "Make some logic.." button all code from doSomething() method is run. When this method end and i return from I get NullPointerException because i have to get id param in getId() method. This mean that i lose viewParam id. Why?

Edit

Related issue: lose view page parameter

Community
  • 1
  • 1
kuba44
  • 1,838
  • 9
  • 34
  • 62

1 Answers1

1

When you click on the commandButton, the form is submitted using HTTP POST and if you check the URL, it doesn't contain the parameter id in it.

In your f:metadata tag, specify a value attribute to bind the param to a bean attribute. You can then use the value in your beans using the same bean attribute.

<f:metadata>
  <f:viewParam name="id" value="#{bean.value}/>
</f:metadata>

In your bean, define a new variable to set the value of Id and also provide its getter and setter methods.

OR

Use the f:param in your commandButton to have access to the parameter after submit.

<h:commandButton value="Make some logic.." action="bean.doSomething">
  <f:param name="id" value="10"/>
<h:commandButton>
Adarsh
  • 3,613
  • 2
  • 21
  • 37