Sunday, April 8, 2012

My First application of JSF2.0 with CDI



In this specific tutorial will list the steps of JSF2.0 that uses CDI. Used Jboss 6.1 application server which ships with the CDI implementation - Weld.

Step1:  We shall create a JSF page that shall  have 3 input text and bind to a managed bean.


<!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:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html">


<ui:composition>
  <h:body>
     <h:form>
<h:inputText value="#{employeeBean.name}"></h:inputText>
<h:inputTextarea value="#{employeeBean.id}"></h:inputTextarea>
<h:inputTextarea value="#{employeeBean.departmentBean.department}"></h:inputTextarea>
<h:commandButton value="submit" action="#{employeeBean.submit}"></h:commandButton>
     </h:form>
   </h:body>
</ui:composition>
</html>


Step2:  Now lets create a managed bean “EmployeeBean” and “DepartmentBean” which shall be a CDI managed java bean. You could notice there are new annotations @javax.enterprise.context.RequestScoped, @Dependent, @Named, @inject


package com.test;


import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.inject.Named;


@RequestScoped
@Named
public class EmployeeBean {
private String name;
private int id;

@Inject
private DepartmentBean departmentBean;


public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String submit(){
System.out.println("submit action called");
return "welcome";
}
public DepartmentBean getDepartmentBean() {
return departmentBean;
}
public void setDepartmentBean(DepartmentBean departmentBean) {
this.departmentBean = departmentBean;
}
}

Now lets take a look at the use of these new annotations:

@ javax.enterprise.context.RequestScoped: A typical web request scope. There are new scopes added apart from the typical (request, session, application) such as @conversationScoped, @Dependent. Shall cover the new scopes in a separate post.

@Named: This annotation lets the managed beans to be accessed through EL.  If there is no value attribute been used then default value is been assigned according to the naming convention. In this case it is “employeeBean”

@Dependent: you could note yet another new scoped bean called @Dependent which is also a default scope if none specified and will serve exactly one client (bean) and has the same life cycle of the client (bean).

@inject:  This annotation is used to inject one bean in to the other. In this example you could see the dependent scope departmentBean is injected to the request scoped employeeBean.



package com.test;

import javax.enterprise.context.Dependent;

@Dependent
public class DepartmentBean {
private String department;

public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
}


Step3:  Now lets create a simple new page where the values got from the user are being displayed.

<!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:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html">

<h:body>
<h:form>
<h:outputText value="#{employeeBean.name}"/>
<h:outputText value="#{employeeBean.id}"/>
<h:outputText value="#{employeeBean.departmentBean.department}"/>
<h:commandButton value="back" action="Test?faces-redirect=true"></h:commandButton>
</h:form>
</h:body>
</html>

Step4: Apart from annotations you could have noticed there is nothing much difference in this application for CDI. The last small step is the addition of beans.xml. This xml is must required even if there is no configurations to be made. This shall allow the app server (jboss here) to use the CDI implementation (weld) to scan through for CDI beans and provide services.

This xml is to be placed in the web-inf folder.

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://java.sun.com/xml/ns/javaee" 
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
       xmlns:weld="http://jboss.org/schema/weld/beans" 
       xsi:schemaLocation="
          http://java.sun.com/xml/ns/javaee http://docs.jboss.org/cdi/beans_1_0.xsd
          http://jboss.org/schema/weld/beans http://jboss.org/schema/weld/beans_1_1.xsd">
 </beans>

Now deploy the application in App server(jboss)







No comments:

Post a Comment