- JSF concept
- MVC architecture
- Navigation Rules
- XML configuration file
- What is backing bean?
- Life cycle
- How to do rendering?
What is JSF?
- JSF stands for Java Server Faces.
- JSF is user interface (UI) framework for Java web applications. By this it means complex components are pre-coded and can be used with ease.
- It is event-driven programming model. By that it means that JSF has all necessary code for event handling and component organization.
- Application programmers can concentrate on application logic rather sending effort on these issues.
- It has component model that enables third-party components to be added like AJAX.
What is required for JSF to get started?
Following things required for JSF:
- JDK (Java SE Development Kit)
- JSF 1.2
- Application Server (Tomcat or any standard application server)
- Integrated Development Environment (IDE) Ex. Netbeans 5.5, Eclipse 3.2.x, etc.
If IDE is used, it will make things very smooth and will save your time.
JSF Architecture
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 has both a Java Web user and 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.
How the components of JSF are rendered? An Example
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
How to declare the Navigation Rules for JSF?
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
face-config.xml
<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.
How do I configure the configuration file?
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>
How does JSF depict the MVC (Model View Controller) model?
The data that is manipulated in form or the other is done by model. The data presented to user in one form or the other is done by view. JSF is connects the view and the model. View can be depicted as shown by:
<h:inputText value="#{user.name}"/>
JSF acts as controller by way of action processing done by the user or triggering of an event. For ex.
<h:commandbutton value="Login" action="login"/>
this button event will triggered by the user on Button press, which will invoke the login Bean as stated in the faces-config.xml file.
Hence, it could be summarized as below: User Button Click -> form submission to server ->; invocation (救助) of Bean class ->; result thrown by Bean class caught be navigation rule ->; navigation rule based on action directs to specific page.
How to get current page URL from backing bean?
You can get a reference to the HTTP request object via FacesContext like this:
FacesContext fc = FacesContext.getCurrentInstance();
HttpServletRequest request = (HttpServletRequest) fc.getExternalContext().getRequest();
and then use the normal request methods to obtain path information. Alternatively,
context.getViewRoot().getViewId();
will return you the name of the current JSP (JSF view IDs are basically just JSP path names).
Discuss life cycle in JSF.
The life cycle of a JavaServer Faces page is similar to that of a JSP page: The client makes an HTTP request for the page, and the server responds with the page translated to HTML. However, because of the extra features that JavaServer Faces technology offers, the life cycle provides some additional services to process a page.
JSF application lifecycle consists of six phases which are as follows
- Restore view phase
- Apply request values phase; process events
- Process validations phase; process events
- Update model values phase; process events
- Invoke application phase; process events
- Render response phase
Restore View Phase:
When a request for a JavaServer Faces page is made, such as when a link or a button is clicked, the JavaServer Faces implementation begins the restore view phase.
Apply Request Values Phase:
After the component tree is restored, each component in the tree extracts its new value from the request parameters by using its decode method. The value is then stored locally on the component. If the conversion of the value fails, an error message associated with the component is generated and queued on FacesContext. This message will be displayed during the render response phase, along with any validation errors resulting from the process validations phase. '
Process Validations Phase:
During this phase, the JavaServer Faces implementation processes all validators registered on the components in the tree. It examines the component attributes that specify the rules for the validation and compares these rules to the local value stored for the component.
Update Model Values Phase:
After the JavaServer Faces implementation determines that the data is valid, it can walk the component tree and set the corresponding server-side object properties to the components' local values. The JavaServer Faces implementation will update only the bean properties pointed at by an input component's value attribute. If the local data cannot be converted to the types specified by the bean properties, the life cycle advances directly to the render response phase so that the page is rerendered with errors displayed. This is similar to what happens with validation errors.
Invoke Application Phase:
During this phase, the JavaServer Faces implementation handles any application-level events, such as submitting a form or linking to another page.
Render Response Phase:
During this phase, the JavaServer Faces implementation delegates authority for rendering the page to the JSP container if the application is using JSP pages. If this is an initial request, the components represented on the page will be added to the component tree as the JSP container executes the page. If this is not an initial request, the components are already added to the tree so they needn't be added again. In either case, the components will render themselves as the JSP container traverses the tags in the page.
JSF與Struts雖然都是Web MVC架構的實現,但兩者所偏重的並不相同,Struts著重的是控制物件的設計;JSF則著重於頁面流程的設計,也就是定義在何種條件成立下,上一個頁 面與下一個頁面之間是如何連結,您可以在faces-config.xml中定義頁面流程
Source:
http://www.withoutbook.com/Technology.php?tech=37&page=5&subject=JSF%20Interview%20Questions%20and%20Answers
http://openhome.cc/Gossip/SpringGossip/FirstJSF.html