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 |
Page 1 of 2
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:
Let's show an example:
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:
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:
Here's the web.xml:
And the faces-config.xml just needs:
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.

