2

I have a h:commandButton on a jsf page. And it needs to be rendered based on a condition. The property on the condition is a hidden input to the page. The action method on the button is not called when the rendering condition is specified.

Any ideas?

here is the sample code:

<h:commandButton value="Button" 
action="#{bean.method}"
rendered="#{bean.conditon}"
type="submit"/>

<h:inputHidden value="#{bean.condition}" />
JavaBeginner
  • 81
  • 1
  • 3
  • 7

1 Answers1

4

I understand that your bean is request scoped, otherwise you wouldn't have this problem. This is a timing problem.

The rendered attribute is also determined during "apply request values" phase of JSF lifecycle. However, the submitted values are only been set in the model during "update model values" phase of JSF lifecycle, which is later. Thus, when rendered attribute is evaluated, it doesn't get the submitted value from the hidden input, but instead the property's default value.

If it's not an option to change the request scope to the view scope, then you'd need to salvage this problem differently. One of the simplest ways changing the <h:inputHidden> to be a <f:param> and inject the value via @ManagedProperty on #{param} map:

<h:commandButton value="Button" 
    action="#{bean.method}"
    rendered="#{bean.conditon}"
>
    <f:param name="condition" value="#{bean.condition}" />
</h:commandButton>

(note that I omitted type="submit" as it's the default already)

with

@ManagedProperty("#{param.condition}")
private boolean condition;

See also:

f_puras
  • 2,521
  • 4
  • 33
  • 38
BalusC
  • 1,082,665
  • 372
  • 3,610
  • 3,555
  • It does not seem to work that way either. I dont know what I am missing. – JavaBeginner Jan 31 '13 at 15:55
  • It's hard to answer without seeing an SSCCE or a detailed description of the problem (which you concluded yourself while tracking the whole process from A to Z, but right now you're making observations like an enduser instead of as a developer, so that part is perhaps hard for you, thus an SSCCE should help us both). – BalusC Jan 31 '13 at 16:00
  • Thanks much. I got it to work. used @Value("#request.getParameter('condition')}". I'm not sure what I did wrong with @ManagedProperty("#{param.condition}"). – JavaBeginner Feb 01 '13 at 14:41
  • Apparently your bean is not managed by JSF's `@ManagedBean`. JSF's `@ManagedProperty` works only on beans managed by JSF's `@ManagedBean`. Unless explicitly otherwise mentioned, we will always assume that you're managing beans by JSF's own annotations. If you're using e.g. CDI's `@Named` or Spring's `@Controller`, then you should mention that explicitly, because those are not the standard ways. – BalusC Feb 01 '13 at 14:45