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}" />
4

1 回答 1

3

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:

于 2013-01-30T21:06:26.087 回答