0

i have page my.xhtml:

<f:metadata>
  <f:viewParam name="id"/>
</f:metadata>
...
<h:form>
  Current id is: "#{id}"

  <h:commandButton action="#{bean.doSomething}" value="Do some logic..">
     <f:param name="id" value="#{id}"/>
  </h:commandButton>
</h:form>

and Bean.java:

@ManagedBean
@ViewScoped
public class Bean {
   ....
   public void doSomething(){
     //do some logick, don't use id parameter
   }
}

When I get to page first time with id=10 I can see on page Current id is: "10".. When I click on button page is reload and I can see on page Current id is: "10". again.

But when I click on button third time I can see on page Current id is: ""., i lose id parameter and I don't understand why?

I know I can do this with save parameter value in bean (by add to f:viewParam:

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

), but can I do this without save parameter value in bean?

kuba44
  • 1,838
  • 9
  • 34
  • 62

1 Answers1

1

h:button works with f:param but h:commandButton does not. In this case, your best is to bind the view parameter to a bean property as you explain the last. The @ViewScoped bean will retain its state as long as you call void action methods.

You could also pass it as a parameter to the action method (there are several ways to do that), but this doesn't make sense for a method which has nothing to do with that value.

See also:

Community
  • 1
  • 1
Aritz
  • 30,971
  • 16
  • 136
  • 217
  • So it doesn't work without save parameter value in bean? – kuba44 Jan 03 '14 at 09:49
  • If your aim is to retain that value in the view, that's what the view scope is designed for. Being JSF an stateful framework, you don't need to keep passing parameters to the server across requests, as long as you remain in the same view, JSF will take care of that. Of course, in other cases you'll be interested in passing a parameter to a method (for example when you want to process a table row value), but that's not the case. – Aritz Jan 03 '14 at 09:54