Latest Entries »

Showing posts with label JSF. Show all posts
Showing posts with label JSF. Show all posts

Tuesday, October 27, 2009

JavaServer Faces Interview Questions

1. What is JSF (or JavaServer Faces)?

A server side user interface component framework for Java™ technology-based web applications. JavaServer Faces (JSF) is an industry standard and a framework for building component-based user interfaces for web applications.

JSF contains an API for representing UI components and managing their state; handling events, server-side validation, and data conversion; defining page navigation; supporting internationalization and accessibility; and providing extensibility for all these features.




2. What are the advantages of JSF?

The major benefits of JavaServer Faces technology are:

  • JavaServer Faces architecture makes it easy for the developers to use. In JavaServer Faces technology, user interfaces can be created easily with its built-in UI component library, which handles most of the complexities of user interface management.
  • Offers a clean separation between behavior and presentation.
  • Provides a rich architecture for managing component state, processing component data, validating user input, and handling events.
  • Robust event handling mechanism.
  • Events easily tied to server-side code.
  • Render kit support for different clients
  • Component-level control over statefulness
  • Highly 'pluggable' - components, view handler, etc
  • JSF also supports internationalization and accessibility
  • Offers multiple, standardized vendor implementations

3. What are differences between struts and JSF?

In a nutshell, Faces has the following advantages over Struts:

· Eliminated the need for a Form Bean

· Eliminated the need for a DTO Class

· Allows the use of the same POJO on all Tiers because of the Backing Bean

The primary advantages of Struts as compared to JavaServer Faces technology are as follows:

· Because Struts is a web application framework, it has more sophisticated controller architecture than does JavaServer Faces technology. It is more sophisticated partly because the application developer can access the controller by creating an Action object that can integrate with the controller, whereas JavaServer Faces technology does not allow access to the controller. In addition, the Struts controller can do things like access control on each Action based on user roles. This functionality is not provided by JavaServer Faces technology.

· Struts includes a powerful layout management framework, called Tiles, which allows you to create templates that you can reuse across multiple pages, thus enabling you to establish an overall look-and-feel for an application.

· The Struts validation framework includes a larger set of standard validators, which automatically generate both server-side and client-side validation code based on a set of rules in a configuration file. You can also create custom validators and easily include them in your application by adding definitions of them in your configuration file.

The greatest advantage that JavaServer Faces technology has over Struts is its flexible, extensible UI component model, which includes:

· A standard component API for specifying the state and behavior of a wide range of components, including simple components, such as input fields, and more complex components, such as scrollable data tables. Developers can also create their own components based on these APIs, and many third parties have already done so and have made their component libraries publicly available.

· A separate rendering model that defines how to render the components in various ways. For example, a component used for selecting an item from a list can be rendered as a menu or a set of radio buttons.

· An event and listener model that defines how to handle events generated by activating a component, such as what to do when a user clicks a button.

· Conversion and validation models for converting and validating component data.


4. What are the available implementations of JavaServer Faces?

The main implementations of JavaServer Faces are:

  • Reference Implementation (RI) by Sun Microsystems.
  • Apache MyFaces is an open source JavaServer Faces (JSF) implementation or run-time.
  • ADF Faces is Oracle’s implementation for the JSF standard.





6. What typical JSF application consists of?

A typical JSF application consists of the following parts:

  • JavaBeans components for managing application state and behavior.
  • Event-driven development (via listeners as in traditional GUI development).
  • Pages that represent MVC-style views; pages reference view roots via the JSF component tree.

7. What Is a JavaServer Faces Application?

JavaServer Faces applications are just like any other Java web application. They run in a Servlet container, and they typically contain the following:

  • JavaBeans components containing application-specific functionality and data.
  • Event listeners.
  • Pages, such as JSP pages.
  • Server-side helper classes, such as database access beans.

In addition to these items, a JavaServer Faces application also has:

  • A custom tag library for rendering UI components on a page.
  • A custom tag library for representing event handlers, validators, and other actions.
  • UI components represented as stateful objects on the server.
  • Backing beans, which define properties and functions for UI components.
  • Validators, converters, event listeners, and event handlers.
  • An application configuration resource files for configuring application resources.

8. What is Managed Bean?

JavaBean objects managed by a JSF implementation are called managed beans. A managed bean describes how a bean is created and managed. It has nothing to do with the bean's functionalities.

9. What is Backing Bean?

Backing beans are JavaBeans components associated with UI components used in a page. Backing-bean management separates the definition of UI component objects from objects that perform application-specific processing and hold data.

The backing bean defines properties and handling-logics associated with the UI components used on the page. Each backing-bean property is bound to either a component instance or its value. A backing bean also defines a set of methods that perform functions for the component, such as validating the component's data, handling events that the component fires and performing processing associated with navigation when the component activates.

10. What are the differences between a Backing Bean and Managed Bean?

Backing Beans are merely a convention, a subtype of JSF Managed Beans which have a very particular purpose. There is nothing special in a Backing Bean that makes it different from any other managed bean apart from its usage.

What makes a Backing Bean is the relationship it has with a JSF page; it acts as a place to put component references and Event code.

Backing Beans

Managed Beans

A backing bean is any bean that is referenced by a form.

A managed bean is a backing bean that has been registered with JSF (in faces-config.xml) and it automatically created (and optionally initialized) by JSF when it is needed.


The advantage of managed beans is that the JSF framework will automatically create these beans, optionally initialize them with parameters you specify in faces-config.xml,

Backing Beans should be defined only in the request scope

The managed beans that are created by JSF can be stored within the request, session, or application scopes

Backing Beans should be defined in the request scope, exist in a one-to-one relationship with a particular page and hold the entire page specific event handling code. In a real-world scenario, several pages may need to share the same backing bean behind the scenes. A backing bean not only contains view data, but also behavior related to that data.

11. What is view object?

A view object is a model object used specifically in the presentation tier. It contains the data that must display in the view layer and the logic to validate user input, handle events, and interact with the business-logic tier. The backing bean is the view object in a JSF-based application. Backing bean and view object are interchangeable terms.





12. What is domain object model?

Domain object model is about the business object and should belong in the business-logic tier. It contains the business data and business logic associated with the specific business object.

13. What is the difference between the domain object model and a view object?

In a simple Web application, a domain object model can be used across all tiers, however, in a more complex Web application, a separate view object model needs to be used. Domain object model is about the business object and should belong in the business-logic tier. It contains the business data and business logic associated with the specific business object. A view object contains presentation-specific data and behavior. It contains data and logic specific to the presentation tier.

14. What do you mean by Bean Scope?

Bean Scope typically holds beans and other objects that need to be available in the different components of a web application.

15. What are the different kinds of Bean Scopes in JSF?

JSF supports three Bean Scopes. viz.,

  • Request Scope: The request scope is short-lived. It starts when an HTTP request is submitted and ends when the response is sent back to the client.
  • Session Scope: The session scope persists from the time that a session is established until session termination.
  • Application Scope: The application scope persists for the entire duration of the web application. This scope is shared among all the requests and sessions.

16. What is the difference between JSP-EL and JSF-EL?

JSP-EL

JSF-EL

In JSP-EL the value expressions are delimited by ${…}.

In JSF-EL the value expressions are delimited by #{…}.

The ${…} delimiter denotes the immediate evaluation of the expressions, at the time that the application server processes the page.

The #{…} delimiter denotes deferred evaluation. With deferred evaluation, the application server retains the expression and evaluates it whenever a value is needed.


note:As of JSF 1.2 and JSP 2.1 ,the syntax of both expression languages has been unified.

17. What are the main tags in JSF?

JSF application typically uses JSP pages to represent views. JSF provides useful special tags to enhance these views. Each tag gives rise to an associated component. JSF (Sun Implementation) provides 43 tags in two standard JSF tag libraries:

  • JSF Core Tags Library.
  • JSF Html Tags Library.

18. How do you declare the managed beans in the faces-config.xml file?

The bean instance is configured in the faces-config.xml file:

login

com.developersBookJsf.loginBean

request

This means: Construct an object of the class com.developersBookJsf.loginBean, give it the name login, and keep it alive for the duration of the request.

19. How to declare the Message Bundle in JSF?

We can declare the message bundle in two ways:
(Let’s assume com.developersBookJsf.messages is the properties file)

1. The simplest way is to include the following elements in faces-config.xml file:

com.developersBookJsf.messages

message


2. Alternatively, you can add the f:loadBundle element to each JSF page that needs access to the bundle:

20. How to declare the page navigation (navigation rules) in faces-config.xml file?

Navigation rules tells JSF implementation which page to send back to the browser after a form has been submitted. We can declare the page navigation as follows:

/index.jsp

login

/welcome.jsp



This declaration states that the login action navigates to /welcome.jsp, if it occurred inside /index.jsp.

21. What if no navigation rule matches a given action?

If no navigation rule matches a given action, then the current page is redisplayed.





22. What are the JSF life-cycle phases?

The six phases of the JSF application lifecycle are as follows (note the event processing at each phase):

1. Restore view
2. Apply request values; process events
3. Process validations; process events
4. Update model values; process events
5. Invoke application; process events
6. Render response

23. Explain briefly the life-cycle phases of JSF?

1. Restore View: A request comes through the FacesServlet controller. The controller examines the request and extracts the view ID, which is determined by the name of the JSP page.
2. Apply request values: The purpose of the apply request values phase is for each component to retrieve its current state. The components must first be retrieved or created from the FacesContext object, followed by their values.
3. Process validations: In this phase, each component will have its values validated against the application's validation rules.
4. Update model values: In this phase JSF updates the actual values of the server-side model, by updating the properties of your backing beans.
5. Invoke application: In this phase the JSF controller invokes the application to handle Form submissions.
6. Render response: In this phase JSF displays the view with all of its components in their current state.

24. What does it mean by render kit in JSF?

A render kit defines how component classes map to component tags that are appropriate for a particular client. The JavaServer Faces implementation includes a standard HTML render kit for rendering to an HTML client.

25. Is it possible to have more than one Faces Configuration file?

We can have any number of config files. Just need to register in web.xml.
Assume that we want to use faces-config(1,2,and 3),to register more than one faces configuration file in JSF, just declare in the web.xml file

javax.faces.CONFIG_FILES

/WEB-INF/faces-config1.xml,

/WEB-INF/faces-config2.xml,

/WEB-INF/faces-config3.xml



Saturday, July 11, 2009

Integrating JSF, Hibernate and Spring

Hello Folks,This post describes the steps to configure Spring Hibernate JSF application. Integration of these three technologies makes a solid framework for web application development.
Creating the application with out IDE:Download the jars for JSF (Ex: Icefaces).Download the jars for Hibernate library.
Download the jars for Spring Library.
Jar's need are
ant-1.6.5.jar, ant-antlr-1.6.5.jar, ant-junit-1.6.5.jar, ant-launcher-1.6.5.jar, ant-swing-1.6.5.jar, antlr-2.7.6.jar, asm-attrs.jar, asm.jar, c3po-0.9.1.jar, cglib-2.1.3.jar, checkstyle-all.jar, cleanimports.jar, commons-beanutils-1.7.0.jar, commons-codec-1.3.jar, commons-collections-2.1.1.jar, commons-collections-3.1.1.jar, commons.digester-1.6.jar, commons-el-1.0.jar, commons-fileupload-1.0.jar, commons-lang-2.1.jar, commons-logging-1.0.4.jar, commons-validator-1.3.1.jar, concurrent-1.3.2.jar, connector.jar, dom4j-1.6.1.jar, ehcache-1.2.3.jar, hibernate3.jar, jass.jar, jacc-1_0-fr.jar, javassist.jar, jaxen-1.1-beta-7.jar, jdbc2_0-stdext.jar, jgroups-2.2.8.jar, jstl-1.1.0.jar, jta.jar, junit-3.8.1.jar, log4j-1.2.11.jar, myfaces-api-1.1.5.jar, myfaces-impl-1.1.5.jar, mysql-connector-java-3.0.16-ga-bin.jar, mysqlconnector-java-3.1.6-bin.jar, oro-2.0.8.jar, oscache-2.1.jar, proxool-0.8.3.jar, spring.jar, swarmcache-1.0rc2.jar, syndiag2.jar, tomahawk-1.1.6.jar, versioncheck.jar, xerces-2.6.2.jar, xml-apis.jar



















Now create the web application and copy all the above lib files under the WEB-INF folder.

Creating the web project with the MyEclipse IDE:Create New Web Application, add JSF capabilities (you can add different kind of flavours of JSF to your application Ex: Icefaces, Richfaces). Now add the spring capabilities and hibernate capabilities.
Create hibernate POJO's and DAO's classes (Hibernate Annotaions library is required to build POJO's & DAO's). Register all the POJO & DAO classes in the spring application contect file, to create hibernate POJO & DAO classes select the MyEclipse Hibernate Prespective. Add the database driver and connect to the database using DB browser. Navigate to the database-->tables, select all the tables and right click go to hibernate reverse engineering option. MyEclipse will take you to the several screens, here you need to select create POJO and create DAO options which will generate POJO & DAO classes.Register all the POJO & DAO classes in the applicationContext.xml file
Sample Code :












Add this spring context file in web.xml




At this point our web application got the relationship between the spring and hibernate and we registered them in web.xml. Now the tedious part of the application is to integrate this part with the JSF.

To integrate JSF and Spring , Spring API provides a variable resolver called DelegatingVariableResolver (but dude it work's only with JSF1.1 and it is not compatible with JSF1.2)




If we create one custom funtion works as DelegatingVariableResolver, then our application will be independent of spring API.


Sample Code:
import javax.faces.context.Facescontext;
import javax.servlet.ServletContext;

import org.springframework.context.ApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

public class ServiceFinder {

public static object findBean(String beanName) {
FacesContext context = FacesContext.getCurrentInstance();

ServletContext servletContext = (ServletContext)context.getExternalContext().getContext();
ApplicationContext appContext = WebApplicationContextUtils.getWebApplicationContext(servletContext);

Object o = appContext.getBean(beanName);

return o;
}
}










And call this funtion in your managed bean like :

ProductionDAO productionDAO = (ProductionDAO) ServiceFinder
.findBean("ProductionDAO");

This completes the integration part of JSF, HIbernate and Spring.

Application Architechture:

Web application consists of 3 layers
1. Presentaion Layer
2. Business Layer
3. Data Access Layer


In our application the presentation layer, business layer and data access layer are physically located on the same JEE server. The different layers of the application are isolated from each other and connected through well defined interfaces.








Presentation Layer

JSF is used to build the presentation layer of the application. JFS allows us to create rich gui for web application. It resolves the technical challenges of creating rich gui web application. In this layer we have jsp pages and JSF components. All the request to the web server are passes through faces servlet.

Business Layer

The POJO classes and classes to process the business logic are used to create the Business Layer. The POJO classes with the help of spring framework creates an ideal solution to implement the Business Layer.

Data Access Layer

The data access layer handles all the logic to save and retrieve the data from data base. Hibernate O/R mapping tools is an ideal solution for enterprise application of any size. Hibernate handles all the logic to store and retrieve POJO objects. It also handles resource management and transaction management activities.