JBoss Eclipse – How to create a JSF 2.0 application

eclipse jbossEclipse is the most used software development environment. Lots of specific plugins are available for JBoss products, however also with the minimal Eclipse Enterprise set you can easily build Java EE 6 applications.

In this tutorial we will show how to configure and create a JSF 2.0 application on JBoss AS 6 using Eclipse 3.6.0 IDE (Helios) in a matter of minutes.

JBoss Eclipse – building a JSF 2.0 application with JBoss 6.

In order to run this example you need to download a copy of Eclipse Java Enterprise edition from http://www.eclipse.org

A JSF application can be created from a Dynamic Web Project. Reach the Menu: New | Other | Web | Dynamic Web Project

 

jboss eclipse tutorial jsf 2

Select the Target runtime pointing to your JBoss 6 installation (JBoss 6 is not included in the server list, however by choosing JBoss 5 will work as well). Now click on the “Modify” button, near the Configuration option.

In the next screen you have to enable the JSF 2.0 facet so that Eclipse will add JSF configuration file and will enable JSF facilities (code completion in JSF pages).

 

eclipse-jsf2

If you have got an earlier version of Eclipse you will find just Java Server Faces 1.1 available. That’s not a big issue- the only thing you have to change is the faces-config.xml heading which needs to specify you are using JSF 2.0

Accept the next screen settings. Finally in the JSF Capabilities window you have to point out where JSF libraries are located. Choose the “Manage Library” button and create a New User Library containing JSF 2.0 libs.

 

jboss eclipse jsf 2.0 tutorial


Where are located JSF libraries on JBoss AS 6 ?

 

They are placed in your <server>\deployers\jsf.deployer. There you will
find several JSF implementation- basically you can opt between Mojarra-1.2 (JSF 1.2) and Mojarra-2.0 (JSF 2.0). By default JBoss 6 uses Mojarra-2.0.

Here’s a sample HelloWorld application which can be used to test your JSF 2.0 environment.
Add a sample.xhtml page to your Web content.

<!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"
 xmlns:f="http://java.sun.com/jsf/core">

<h:body>
 <h:form>
 <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>
 </h:form>
</h:body>
</html>

An this is the corresponding Managed Bean (notice we declare it using a simple @ManagedBean annotation). The expected result is to uppercase the text contained in the field personName.

package sample;

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

@ManagedBean(name="person")
@RequestScoped

public class PersonBean {

 String personName;
 String prompt;

 public PersonBean() {
 this.prompt="Enter your name please";
 }

 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();
 }

}

And now some specific questions about JSF and JBoss.

Question: How can I use JSF 1.2 with JBoss 6 ?

You can at any time switch to JSF 1.2 implementation by adding in your web.xml file

<context-param>
 <param-name>org.jboss.jbossfaces.JSF_CONFIG_NAME</param-name>
 <param-value>Mojarra-1.2</param-value>
 </context-param>

Question: do I need to specify anything related to JSF in my web.xml ?
It’s not required anymore in JBoss 6. Since JBoss 6 a Web application is JSF 2.0 compliant if you have either:

  • A faces-config.xml file is found in WEB-INF
  • A faces-config.xml file is found in META-INF of some jar in WEB-INF/lib

Or if you set to true the following property into <server>\deployers\jsf.deployer\META-INF\jsf-integration-deployer-jboss-beans.xml

<property name="alwaysAddJSF">true</property>

Question: Should I deploy my Web application with JSF libs ?
No, you shouldn’t. However there are some scenarios where you might want to bundle JSF libs with your WAR.  For instance, you might have an old JSF application that uses MyFaces and you don’t want to retest your app on a new JSF implementation.  In that case, you can bundle the JSF implementation with your WAR and add this to your web.xml:

<context-param>
 <param-name>org.jboss.jbossfaces.WAR_BUNDLES_JSF_IMPL</param-name>
 <param-value>true</param-value>
</context-param> 

This will cause JBoss to ignore the built-in JSF implementation as long as you use the default classloader settings

 

References: http://docs.jboss.org/jbossas/6/JSF_Guide/en-US/html/index.html