![]() |
![]() |
![]() |
![]() |
In this step, we will create and configure Managed Beans for the web application. We will create a JavaBean called UserBean that holds information like userId, password, name and contact details.
private String userId = null; private Integer password = null; private String name = null; private ContactDetailsBean contactDetails = null; |
The DataStoreBean maintains a list of registered users. Open the class DataStoreBean and review the code. The DataStoreBean constructor initializes instance of java.util.Hashtable to maintain list of registered user. It has methods to add / remove user and get user information. For each user the Hashtable maps userId as a key and a UserBean instance as a value.
The MessageFactory class retrieves localized messages from the application resource bundle, i.e. application.properties file. Open the class and review the code. It has method getMessage(String, Object[ ]) that returns instance of FacesMessage for localized messages. The String argument is the key that corresponds to the message in the ResourceBundle. The Object instance typically contains the substitution parameters that are embedded in the message. It uses the FacesContext to access the Application instance on which the messages are registered.
In this step you will implement the method registerUser( ) in UserBean class. The method is responsible for adding the user to the data store, retrieving FacesMessage instances for localized messages with the key thanksMsg_Registration, adding it to FacesContext and returning the logical outcome success.
If, the user with the same userId already exists then the method retrieves a FacesMessage instance for a localized message with key userIdExists, adds it to FacesContext and returns null.
public String registerUser( ) { FacesContext context = FacesContext.getCurrentInstance( ); ExternalContext extContext = context.getExternalContext( ); Map appMap = extContext.getApplicationMap( ); DataStoreBean dataStore = (DataStoreBean)appMap.get(DataStoreBean.DB_NAME); if(dataStore.getUser(userId) != null) { Object[ ] objArr = new Object[ ] { userId }; FacesMessage message = MessageFactory.getMessage(context, "userIdExists", objArr); context.addMessage(null, message); return null; } else { dataStore.addUser(this); Object[ ] objArr = new Object[ ] { name }; FacesMessage message = MessageFactory.getMessage(context, "thanksMsg_Registration", objArr); String msg = message.getDetail( ); extContext.getRequestMap( ).put("thanksMsg_Registration", msg); return "success"; } } |
import javax.faces.context.FacesContext; import javax.faces.context.ExternalContext; import java.util.Map; import javax.faces.application.FacesMessage; import util.MessageFactory;
To make the application support multiple languages, we use the internationalization feature. We define text constants, component labels / titles and messages into properties files that are used by the JSF components and Java classes.
The registerUser( ) method of UserBean gets localized messages for the keys userIdExist and thanksMsg_Registration from the application resource bundle file application.properties
userIdExists=The userId {0} already exists, pls. try
another userId. thanksMsg_Registration=Dear {0}, Thank you for registering with Workshop |
To enable the JSF framework for instantiating backing beans and storing them in the appropriate scope, you must configure it in the application configuration resource file faces-config.xml using the managed-bean XML element. The faces-config.xml file is processed at application startup time. When a page references a bean, the JavaServer Faces implementation initializes it according to its configuration in the application configuration resource file.
The UserBean has a property contactDetails of bean type ContactDetailsBean. Hence, we first define managed-bean configurations for ContactDetailsBean and then for the UserBean.
This adds Managed Bean configuration
for ContactDetailsBean
to faces-config.xml file.
This will add managed property contactDetails for managed
bean user in Managed Bean Tag dialog.
This
adds Managed Bean configuration for UserBean
to faces-config.xml file.
The user managed-bean declaration creates a UserBean
in Session scope. This bean has property contactDetails
that references the contactDetails managed-bean.
The contactDetails managed-bean declaration
will not create an instance of ContactDetailsBean
because it is defined with the scope none.
Click one of the following arrows to navigate through the tutorial:
![]() ![]() |