Advertise with us

Our eBooks

JSF 2.0 tutorial on JBoss AS

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 you need a JBoss AS release which is compatible with JSF 2.0 API  ( JBoss AS 6/7 run by default a JSF 2 API implementation).

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")

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) { = 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.

  Here is a sample tutorial which shows how to create a CRUD applications using JSF and @ViewScoped.

Now Let's add a basic JSP page which will be used in our example:

<!DOCTYPE html> <html xmlns=""       xmlns:f=""       xmlns:h="">

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


What happened to JSP Tags?

One thing you might have noticed is the absence of standard JSP tags. As a matter of fact, JSP technology is considered to be a deprecated presentation technology for Java Server Faces 2.0. That simply means, that JSF preferred view technology is Facelets, for an introduction to Facelets have a look at this tutorial.
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=""
    xmlns="" xmlns:web=""
    id="WebApp_ID" version="2.5">

        <servlet-name>Faces Servlet</servlet-name>

        <servlet-name>Faces Servlet</servlet-name>
And the faces-config.xml just needs:
<?xml version="1.0" encoding="UTF-8"?>
<faces-config xmlns="" 
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.
© 2021 All Rights Reserved.

Please publish modules in offcanvas position.