My JSF Books/Videos My JSF Tutorials OmniFaces/JSF PPTs
JSF 2.3 Tutorial
JSF Caching Tutorial
JSF Navigation Tutorial
JSF Scopes Tutorial
JSF Page Author Beginner's Guide
OmniFaces 2.3 Tutorial Examples
OmniFaces 2.2 Tutorial Examples
JSF Events Tutorial
OmniFaces Callbacks Usages
JSF State Tutorial
JSF and Design Patterns
JSF 2.3 New Features (2.3-m04)
Introduction to OmniFaces
25+ Reasons to use OmniFaces in JSF
OmniFaces Validators
OmniFaces Converters
JSF Design Patterns
Mastering OmniFaces
Reusable and less-verbose JSF code

My JSF Resources ...

Java EE Guardian
Member of JCG Program
Member MVB DZone
Blog curated on ZEEF
OmniFaces is an utility library for JSF, including PrimeFaces, RichFaces, ICEfaces ...

.

.

.

.

.

.

.

.


[OmniFaces Utilities] - Find the right JSF OmniFaces 2 utilities methods/functions

Search on blog

Petition by Java EE Guardians

Twitter

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

JSF BOOKS COLLECTION

Postări populare

OmniFaces/JSF Fans

Visitors Starting 4 September 2015

Locations of Site Visitors