Java Server Faces (JSF) is a web-based framework that makes the UI development in web-based applications easy. JSF provides various ways to connect UI components with data sources and server-side event handlers. It helps in making easy data transfer between UI components.
Architecture of JSF
A JSF application is similar to any Java-based web application. It runs in a Java Servlet container and it follows the MVC (Model, View and Controller) architecture.
The diagram below shows the architecture of a JSF application:
How a JSF works
JSF applications run inside a servlet container. All the processing related to JSF is done inside the container. The procedure said below explains how a JSF application works in a scenario where a user clicks on some button or performs any other event and gets the desired output.
- When a user fires an event, it goes to a FacesServlet class that is an engine of a JSF application. A servlet-mapping element is used in a deployment descriptor (web.xml) to map a specific pattern to invoke the FacesServlet class. The following code is a part of the web.xml used to run a JSF application.
- <Servlet>
- <servlet-name>Faces Servlet</servlet-name>
- <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
- <load-on-startup>1</load-on-startup>
- </servlet>
- <Servlet-mapping>
- <servlet-name>Faces Servlet</servlet-name>
- <url-pattern>/faces/*</url-pattern>
- </servlet-mapping>
- All the processing is done inside the FacesServlet class. Each JSF application inside the same web container has its own FacesServlet class.
- Each FacesServlet class has a FacesContext class object that has all the necessary information of the current request.
- Now the request goes to the called ManagedBean and it returns the output to the called UI page.
- There are two ways by which you can map your JSF/JSP page with the required managedbean class.
- Using Faces-Config.xml file: Faces-Config XML file has all the information of mapping between JSP/JSF pages and managedbean class. An example of a Faces-Config XML file is shown below:
- <faces-config>
- <managed-bean>
- <managed-bean-name>someName</managed-bean-name>
- <managed-bean-class>
- somePackage.SomeClass
- </managed-bean-class>
- </managed-bean>
- </faces-config>
- Using Annotations: Another way to map your JSP/JSF file with the managedbean class is to use Annotations in the managedbean. An example of annotation is shown below:
- Import javax.faces.bean.ManagedBean;
-
-
- @ManagedBean(name="JSFTut",eager=true)
- public class JSFTutorials {
- public String callMe()
- {
- return "Hello JSF ";
- }
- }
In future articles, I will be describing more about the JSF framework. I hope this article was useful to you and thank you for reading it.