The AS7 book!

JBoss Tuning

JBoss 5 book

No Registration required. Use your FB account to add comments to articles.

Twitter Button

Poll

Which is your favourite JSF library?
 
Top Programming Sites
Home Java EE 1.6 articles JSF 2.0 tutorial on JBoss AS

JSF 2.0 tutorial on JBoss AS

Article Index
JSF 2.0 tutorial on JBoss AS
Validation and Navigation with JSF 2.0
All Pages
  
jsf 2 2.0 jboss tutorial wenJSF 2.0 is a major upgrade over JSF 1.2. The major additions include ManagedBeans annotations, simplified navigation and Ajax Support. In this first article we will look at the first two additions, using the milestone 3 of JBoss AS 6.
At first download the latest binaries of JBoss AS 6. If you have got an earlier milestone of JBoss 6, it will work as well.

JSF Managed Beans Annotations


Let's start. One of the most useful additions of JSF 2.0 is the ability to add annotations for your Beans instead of using the file faces-config.xml. You might think there it's going on a religious war against XML configuration files- it's not so.
Actually using annotations simplifies the management of your projects because you don't need to lock/share a file which is used by all your developers, every time you need to add a bean or a navigation rule.

Let's show an example:
  package sample;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;

@ManagedBean(name="person", eager="true")
@RequestScoped

public class PersonBean {

    String personName;
    String prompt;
    String email;
    int amount;
    
     public PersonBean() {
        this.prompt="Enter your name please";
    }

    public int getAmount() {
        return amount;
    }
    public void setAmount(int amount) {
        this.amount = amount;
    }
    public String getEmail() {
        return email;
    }

    public void setEmail(String email) {
        this.email = email;
    }


    public String getPrompt() {
        return prompt;
    }

    public void setPrompt(String prompt) {
        this.prompt = prompt;
    }


    public String getPersonName() {
        return personName;
    }

    public void setPersonName(String name) {
        personName = name;
    }
    public void doSomething() {
        personName= personName.toUpperCase();
    }
     
}

In this example we have tagged our Bean as ManagedBean by using the annotation @ManagedBean.

We have set the attribute name to define how we will reference the bean in our Jsp pages and the attribute eager = true, which means that the Bean will be instantiated at application startup and not when it's first referenced.
If you don't specify the name attribute, the Bean will be bound using the Class name, with the first letter in lowercase. If you don't specify the eager attribute it will default to false.

The other annotation we have added is @RequestScoped which means the Bean is bound in the Request.

All possible alternatives are:

  • @NoneScoped 
  • @RequestScoped 
  • @ViewScoped 
  • @SessionScoped 
  • @ApplicationScoped 
  • @CustomScope

What is the View Scope ? View Scope has been introduced in JSF 2.0 and it's particularly useful when you are editing some objects while staying in the same page. In other words it's something broader then request but smaller then session, since this scope terminates if you navigate to other pages.



Now Let's add a basic JSP page which will be used in our example:
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h"%>

<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
 

<html>
 <body>
   <f:view>   
      
     <h:form id="helloForm">
     <h:outputText value="#{person.prompt}" />
          
     <h:inputText id="name" value="#{person.personName}" />
     <h:commandButton value="go" action="#{person.doSomething}" />

     </h:form>
   </f:view>
 </body>
</html>
By running this trivial example, the command button will trigger the action doSomething which will uppercase the attribute. Let's complete the example adding a basic web.xml and faces.config.

Here's the web.xml:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <context-param>
        <param-name>com.sun.faces.enableRestoreView11Compatibility</param-name>
        <param-value>true</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>*.jsf</url-pattern>
    </servlet-mapping>
</web-app>
And the faces-config.xml just needs:
<?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"> 
</faces-config>
Notice the attribute version="2.0". If you use by mistake a faces-config.xml with the attribute set to "1.2" or "1.0", the code will run with the earlier JSF release.