amorfis
amorfis

Reputation: 15770

In JSF, how to make h:commandButton to send only one field?

In JSF2 I have a command button, which should send only one field:

<h:inputHidden id="dirty" value="#{bean.dirty}" />
<h:commandButton value="Back" immediate="true"
                 action="#{bean.backIfClean}">
  <f:ajax execute="dirty" />
</h:commandButton>

This code doesn't work. I change hidden field value by JavaScript, then I want to send it to server. However, it is not even set on bean (setDirty is not called).

If I remove immediate="true" validation is triggered, which I want to avoid in this case.

Can I somehow avoid validation and send dirty field value?

Upvotes: 2

Views: 2540

Answers (3)

JSS
JSS

Reputation: 2183

You can use the following approach to send/retrieve one value:

<h:commandButton action="#{bean.backIfClean}">
   <f:param name="dirty" value="#{bean.dirty}"/>       
</h:commandButton>

Backing Bean code:

public String backIfClean()
{
   String dirty = (String)FacesContext.getCurrentInstance().getExternalContext().getRequestParameterMap().get("dirty");
}

Upvotes: 1

Joeri Hendrickx
Joeri Hendrickx

Reputation: 17435

That won't work. If you put immediate on a command button, the values for the input components never get set. This is a major inconvenience in the framework if you ask me.

The only solution you have is to retrieve the value yourself from the request.

What I don't get is why yu can't do with just the execute. Normally, only 'dirty' should be validated in that case. Isn't that what you want?

Upvotes: 1

BalusC
BalusC

Reputation: 1108732

When you put immediate="true" on an UICommand component, then only the UIInput fields which also have immediate="true" will be processed. See also this summary.

So, add immediate="true" to the h:inputHidden.

(note: untested in combination with f:ajax, this is pure theory)

Upvotes: 1

Related Questions