Ajax event is very useful for asynchronous action JSF page,
The code for JSF is plotted below
Step 1 :
Create JSF Code in xhtml page
<p:panel id="panel" header="Login Panel" style="margin:0 auto;width:350px;margin-top:15%;"> <p:panelGrid columns="3" id="pgrid1" styleClass="theme" >
<h:outputLabel value="User Name:" />
<p:inputText id="name" value="#{loginBean.name}" required="true" >
<f:ajax event="blur" render="label" listener="#{loginBean.ajaxEvent}" />
</p:inputText>
<p:message for="name" style="color: red;" />
<p:outputLabel value="User Id:" />
<p:inputText id="id" value="#{loginBean.id}" required="true" />
<p:message for="id" style="color: red;" />
</p:panelGrid>
<p:commandButton type="Submit" value="Submit" action="#{loginBean.validate}" update="pgrid1">
</p:commandButton>
</p:panel>
Bean class Code is
public void ajaxEvent(AjaxBehaviorEvent e)
{
System.out.println("Ajax Value----->"+getName());
}
- Here we are going to see about AjaxBehaviorEvent , which is used to invoke the values from Bean class when the ajax xction begin
- In following method we just invoked the AjaxAction for <p:inputtext > when the values are entered in textbox ajax blur action begins.
The code for JSF is plotted below
Step 1 :
Create JSF Code in xhtml page
<p:panel id="panel" header="Login Panel" style="margin:0 auto;width:350px;margin-top:15%;"> <p:panelGrid columns="3" id="pgrid1" styleClass="theme" >
<h:outputLabel value="User Name:" />
<p:inputText id="name" value="#{loginBean.name}" required="true" >
<f:ajax event="blur" render="label" listener="#{loginBean.ajaxEvent}" />
</p:inputText>
<p:message for="name" style="color: red;" />
<p:outputLabel value="User Id:" />
<p:inputText id="id" value="#{loginBean.id}" required="true" />
<p:message for="id" style="color: red;" />
</p:panelGrid>
<p:commandButton type="Submit" value="Submit" action="#{loginBean.validate}" update="pgrid1">
</p:commandButton>
</p:panel>
Bean class Code is
public void ajaxEvent(AjaxBehaviorEvent e)
{
System.out.println("Ajax Value----->"+getName());
}
- Here ajaxEvent is an user defined event
- After ajax is called from JSF page the bean class `ajaxEvent` method is invoiked as i mentioned above in code.
- The System.out will print the inputText value where we entered JSF page
Comments
Post a Comment