vineri, 20 februarie 2015

[OmniFaces utilities (2.0)] Get whether the given component has invoked the form submit


[OmniFaces utilities] The hasInvokedSubmit() method get whether the given component has invoked the form submit. In non-ajax requests, that can only be an UICommand component. In ajax requests, that can also be among others an UIInput component.

Method:
OmniFaces 2.4OmniFaces 2.0
Is pretty easy to understand how this method works as long as you know how to identify the component that invoked the form submit. You just have to know that its clientId appears in the request parameter map as:
·         the value of the javax.faces.sources request parameter for AJAX request
·         a request parameter name for non-AJAX request

Below, you can see several cases:
1
<h:form id="myForm">
 Player Name:
 <h:inputText id="nameId" value="#{playersBean.name}"/>
 <h:commandButton id="submitButtonId" value="Save" action="#{playersBean.save()}"/>
</h:form>

2
<h:form id="myForm">
 Player Name:
 <h:inputText id="nameId" value="#{playersBean.name}"/>
 <h:commandLink id="submitLinkId" value="Save" action="#{playersBean.save()}"/>
</h:form> 
3
<h:form id="myForm">
 Player Name:
 <h:inputText id="nameId" value="#{playersBean.name}"/>
 <h:commandButton id="submitButtonId" value="Save" action="#{playersBean.save()}">
  <f:ajax execute="@form" render="@form"/>
 </h:commandButton>
</h:form>
4
<h:form id="myForm">
 Player Name:
 <h:inputText id="nameId" value="#{playersBean.name}">
  <f:ajax event="keyup" execute="@form" render="@form" listener="#{playersBean.save()}"/>
 </h:inputText>         
</h:form>
5
<h:form id="myForm">
 <h:selectOneMenu id="playerId" value="#{playersBean.name}">
  <f:selectItems value="#{playersBean.players}" var="t" itemLabel="#{t.player}" itemValue="#{t.player}" />
  <f:ajax event="change" execute="@form"  render="@form" listener="#{playersBean.save}" />
 </h:selectOneMenu>
</h:form>
Basically, the OmniFaces hasInvokedSubmit() method follows three steps:
·         ensure that this is a postback request (forms can be submitted only on postback)
·         check if this was an AJAX request, and if it was, check the value of javax.faces.source and return accordingly
·         if this is a non-AJAX request, and the passed component in an UICommand, then try to find its clientId in the request parameter map

Usage

Let's see some use cases based on the above examples:
1 //return false
UIComponent  comp = Faces.getViewRoot().findComponent("myForm:nameId");
boolean hasInvokedSubmit = Components.hasInvokedSubmit(comp);

1 //return true
UIComponent  comp = Faces.getViewRoot().findComponent("myForm:submitButtonId");
boolean hasInvokedSubmit = Components.hasInvokedSubmit(comp);

2 //return true
UIComponent  comp = Faces.getViewRoot().findComponent("myForm:submitLinkId");
boolean hasInvokedSubmit = Components.hasInvokedSubmit(comp);

3 //return true
UIComponent  comp = Faces.getViewRoot().findComponent("myForm:submitButtonId");
boolean hasInvokedSubmit = Components.hasInvokedSubmit(comp);

4 //return true
UIComponent  comp = Faces.getViewRoot().findComponent("myForm:nameId");
boolean hasInvokedSubmit = Components.hasInvokedSubmit(comp);

5 //return true
UIComponent  comp = Faces.getViewRoot().findComponent("myForm:playerId");
boolean hasInvokedSubmit = Components.hasInvokedSubmit(comp);

Niciun comentariu:

Trimiteți un comentariu