You are on page 1of 6

Day -1 JSF Basic Example: Managed Bean defined by annotation, Dependency Injection and Configuration file.

A JavaServer Faces (JSF) 2.0 hello world example, shows the JSF 2.0 dependencies, basic annotations and configurations. This JSF 2.0 example is build with following tools and technologies 1. 2. 3. 4. 5. JSF 2.1.7 Maven 3 Eclipse 3.7.2 JDK 1.6 Jboss 7.0

Managed Bean: In JSF 2.0, use @ManagedBean annotation to indicate this is a managed bean. HelloBean.java
package com.mkyong.common; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; import java.io.Serializable; @ManagedBean @SessionScoped public class HelloBean implements Serializable { private static final long serialVersionUID = 1L; private String name; public String getName() { return name; } public void setName(String name) { this.name = name; } }

JSF Page ,We used .xhtml page ,where we declare the TLD in XML fashion.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:f="http://java.sun.com/jsf/core" xmlns:h="http://java.sun.com/jsf/html"> <h:head> <title>JSF 2.0 Hello World</title> </h:head> <h:body> <h3>JSF 2.0 Hello World Example - hello.xhtml</h3> <h:form> <h:inputText value="#{helloBean.name}"></h:inputText> <h:commandButton value="Welcome Me" action="welcome"></h:commandButton> </h:form> </h:body> </html>

Note

In JSF 1.x, you had to declare the navigation rule in faces-config.xml, to tell which page to display when the button is clicked. In JSF 2.0, you can put the page name directly in the buttons action attribute. For simple navigation, its more than enough, but, for complex navigation, you are still advise to use the navigation rule in faces-config.xml. File : welcome.xhtml Display the submitted text box value.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml" xmlns:h="http://java.sun.com/jsf/html"> <h:head> <title>JSF 2.0 Hello World</title> </h:head> <h:body bgcolor="white"> <h3>JSF 2.0 Hello World Example - welcome.xhtml</h3> <h4>Welcome #{helloBean.name}</h4> </h:body> </html>

The #{} indicate this is a JSF expression language, in this case, #{helloBean.name}, when the page is submitted, JSF will find the helloBean and set the submitted textbox value via the setName() method. When welcome.xhtmlpage is display, JSF will find the same session helloBean again and display the name property value via thegetName() method.

Configuration file i.e Face-config.xml With XML configuration, you can use the old JSF 1.x mechanism to define the managed bean in a normal faces-config.xml file.
<?xml version="1.0" encoding="UTF-8"?> <faces-config xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd" version="2.0"> <managed-bean> <managed-bean-name>helloBean</managed-bean-name> <managed-bean-class>com.mkyong.common.HelloBean</managed-bean-class> <managed-bean-scope>session</managed-bean-scope> </managed-bean> </faces-config>

Best Practice

Its recommended to put the managed beans in a separate XML file because the faces-config.xml is used to set the application level configurations. So, you should create a new XML file and put the managed beans detail inside, and declared the XML file in the javax.faces.CONFIG_FILES initialize parameter, which is inside the WEBINF/web.xml file.
web.xml
... <context-param> <param-name>javax.faces.CONFIG_FILES</param-name> <param-value>WEB-INF/manage-beans.xml</param-value> </context-param> ...

Inject a one Managed Bean into another Managed bean. Its called as DI (Dependency Injection)
MessageBean.java A managed bean named message.
import java.io.Serializable; import javax.faces.bean.ManagedBean; import javax.faces.bean.SessionScoped; @ManagedBean(name="message") @SessionScoped public class MessageBean implements Serializable { //business logic and whatever methods...

HelloBean.java Inject the message bean into the messageBean property.


import import import import java.io.Serializable; javax.faces.bean.ManagedBean; javax.faces.bean.ManagedProperty; javax.faces.bean.SessionScoped;

@ManagedBean @SessionScoped public class HelloBean implements Serializable { @ManagedProperty(value="#{message}") private MessageBean messageBean; //must povide the setter method public void setMessageBean(MessageBean messageBean) { this.messageBean = messageBean; } //... }

In this example, it uses the @ManagedProperty annotation to DI the message bean (MessageBean.java) into the property (messageBean) of the hello bean (HelloBean.java) via setter method, setMessageBean().

Managed Bean
About managed bean configuration and injection in JSF 2.0 Configure Managed Beans in JSF 2.0 In JSF 2.0, Java bean that can be accessed from JSF page is called Managed Bean. The managed bean can be a normal Java bean, which contains the getter and setter methods, business logic or even a backing bean (a bean contains all the HTML form value). Injecting Managed bean In JSF 2.0, a new @ManagedProperty annotation is used to dependency injection (DI) a managed bean into the property of another managed bean. JSF Navigation and JSF Ajax Tags Implicit Navigation in JSF 2.0 Now, JSF 2 come out a new auto view page resolver mechanism named implicit navigation, where you dont need to declare the above navigation rule, instead, just

put the view name in the action attribute and JSF will find the correct view page automatically. JSF : Page Forward vs Page Redirect By default, JSF is perform a server page forward while navigating to another page. See following example to differentiate between the page forward and page redirect. JSF 2 internationalization example JSF 2.0 internationalization or multiple languages example. JSF 2.0 + Ajax hello world example In JSF 2.0, coding Ajax is just like coding a normal HTML tag, its extremely easy. In JSF 2.0 hello world example, when the button is clicked, it will make an Ajax request instead of submitting the whole form. (Asynchronous Javascript And Xml) Technique for dynamically updating web pages

Day -2 Validation Java Regex for Name, Phone number, SSN or UID and Email. Internationalization or i18N to support multiple languages.

Day-3 Integration with third party component suite or collection RichFace for Pagination , Pick List and dependency drop down . Day-4

Apache Http Connection core API Java Script Jquery Day-5-15 (10 days UI-Project) JSF +RichFace+Apache Http Connection or Consuming a Webservice. Day 15-30 (Service Project) Webservice + Hibernate (Annotation, Create Criteria only) + Spring DI and Security + Jboss CoCoon feature.

You might also like