Internals of JSF


Once jsf project set up properly i.e entries in the web.xml,jsf related jars in lib folder of your web app(setting up jsf project) we have to create a faces-config.xml to make our set up into a full fledged jsf application.

Creating Faces-config.xml ::

In the name itself telling us that it contains all the configurations needed for all jsf pages.In fact we don't need to create the file with the same name.We can create it with any name and we have to mention same as a context parameter in the WEB.xml (setting up jsf project).Now its the time to jump into the detailed view. 

 Fallowing are the two main components that we use frequently.

1)Managed Beans

It represents a Java bean that will be instantiated dynamically at the run time by JSF container.We can achive inversion of control in JSF.

Ex:
Class User{
private String name;
private String Address;

//setters and getters for both properties

}
Declaration in faces-config.xml

 
<managed-bean> 
       <managed-bean-name>Userbean</managed-bean-name>
       <managed-bean-class>User</managed-bean-class> 
       <managed-bean-scope>request</managed-bean-scope> 
</managed-bean>


Now I can use the bean named "Userbean" in my JSF application at any time. This is done by using JSF's Expression Language. For example to bind a JSF user interface component (UI Component) to one of the properties of my Userbean, I use the expression #{Userbean.name} as a value argument to a JSF UI Component. Here is an example of binding the firstName property of the bean to a JSF inputText (UIInput) UI Component.
The scope specifies the lifetime that bean.
 
 <h:inputText value="#{Userbean.name}"/>

So now upon any form submissions the value entered into the input text field will be applied to the property of my Java Bean.

2)Navigation rule

It is a place where all the navigation's in the jsf application to be declared like to what jsp has to be rendered  for  a specified output.

Ex:
<navigation-rule>
     <from-view-id>/pages/login.jsp</from-view-id>
     <navigation-case>
       <from-outcome>success</from-outcome>
       <to-view-id>/pages/gretting.jsp</to-view-id>
     </navigation-case>
     <navigation-case>
       <from-outcome>failure</from-outcome>
       <to-view-id>/pages/login.jsp</to-view-id>
     </navigation-case>
   </navigation-rule>  
In the above rule login.jsp is a jsp page which can contain a button
or command link or any thing whose action parameter is bind with a method.
 
 <h:commandButton action="#{Userbean.validateUser}"  value="login" />
 
If we click on the above button in the jsp page then it will search for the 
validateUser() method in the UserBean i.e User class and execute that method.
If the returned value is "success" then greeting.jsp is displayed.If it is "failure" 
then the same page gets displayed.Of course the same page gets rendered if the returned 
value of the method doesn't match with any form-outcome.
 
 
  


Comments

Popular posts from this blog

What's New in JAVA 8

Singleton Design Pattern