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.backIfC开发者_JAVA百科lean}">
<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?
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?
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)
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");
}
精彩评论