Q: What is JSF architecture?
Ans. JSF was developed using
MVC (a.k.a Model View Controller) design pattern
so that applications can be scaled better with
greater maintainability. It is driven by Java
Community Process (JCP) and has become a standard.
The advantage of JSF is that it’s
both a Java Web user – interface
and a framework that fits well with the MVC.
It provides clean separation between presentation
and behavior. UI (a.k.a User Interface) can
be created by page author using reusable UI
components and business logic part can be implemented
using managed beans.
Q: How JSF different from conventional JSP /
Servlet Model?
Ans. JSF much more plumbing
that JSP developers have to implement by hand,
such as page navigation and validation. One
can think of JSP and servlets as the “assembly
language� under the hood of the
high-level JSF framework.
Q: How the components of JSF are rendered?
An Example
Ans. In an application add
the JSF libraries. Further in the .jsp page
one has to add the tag library like:
<%@ taglib uri="http://java.sun.com/jsf/core"
prefix="f"%>
<%@ taglib
uri="http://java.sun.com/jsf/html"
prefix="h"%>
Or one can try XML style as well:
<?xml version="1.0"?>
<jsp:root version="2.0"
xmlns:jsp="http://java.sun.com/JSP/Page"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:h="http://java.sun.com/jsf/html">
Once this is done, one can access the JSF components
using the prefix attached. If working with an
IDE (a.k.a Integrated Development Environment)
one can easily add JSF but when working without
them one also has to update/make the faces-config.xml
and have to populate the file with classes i.e.
Managed Beans between
<faces-config> </faces-config> tags
Q: How to declare
the Navigation Rules for JSF?
Ans. Navigation rules tells
JSF implementation which page to send back to
the browser after a form has been submitted.
For ex. for a login page, after the login gets
successful, it should go to Main page, else
to return on the same login page, for that we
have to code as:
<navigation-rule>
<from-view-id>/login.jsp</from-view-id>
<navigation-case>
<from-outcome>login</from-outcome>
<to-view-id>/main.jsp<to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>fail</from-outcome>
<to-view-id>/login.jsp<to-view-id>
</navigation-case>
</navigation-rule>
from-outcome to be match with action attribute
of the command button of the login.jsp as:
<h:commandbutton value="Login"
action="login"/>
Secondly, it should also match with the navigation
rule in face-config.xml as
<managed-bean>
<managed-bean-name>user</managed-bean-name>
<managed-bean-class>core.jsf.LoginBean</managed-bean-class>
<managed-bean-scope>session</managed-bean-scope>
</managed-bean>
In the UI component, to be declared / used as:
<h:inputText value="#{user.name}"/>
value attribute refers to name property of the
user bean.
Q: How do I configure the configuration
file?
Ans. The configuration file
used is our old web.xml, if we use some IDE
it will be pretty simple to generate but the
contents will be something like below:
<?xml version="e;1.0"e; encoding="e;UTF-8"e;?>
<web-app
version="e;2.4"e; xmlns="e;http://java.sun.com/xml/ns/j2ee"e;
xmlns:xsi="e;http://www.w3.org/2001/XMLSchema-instance"e;
xsi:schemaLocation="e;http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"e;>
<context-param>
<param-name>com.sun.faces.verifyObjects</param-name>
<param-value>false</param-value>
</context-param>
<context-param>
<param-name>com.sun.faces.validateXml</param-name>
<param-value>true</param-value>
</context-param>
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value>
</context-param>
<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>
<session-config>
<session-timeout>
30
</session-timeout>
</session-config>
<welcome-file-list>
<welcome-file>
index.jsp
</welcome-file>
</welcome-file-list>
</web-app>
The unique thing about this file is ?servlet
mapping?. JSF pages are processed by a servlet
known to be part of JSF implementation code.
In the example above, it has extension of .faces.
It would be wrong to point your browser to http://localhost:8080/MyJSF/login.jsp,
but it has to be http://localhost:8080/MyJSF/login.faces.
If you want that your pages to be with .jsf,
it can be done with small modification :-) ,
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
<servlet-mapping>
Q: What
is JSF framework?
Ans. JSF framework can be explained
with the following diagram:
JSF interacts with Client Devices which ties
together with presentation, navigation and event
handling and business logic of web tier model.
Hence JSF is limited to presentation logic /
tier. For Database tier i.e. Database and Web
services one has to rely on other services.