![]() |
![]() |
![]() |
![]() |
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.
Create new packages called beans and util under the folder web/WEB-INF/src/java by right clicking on the folder and choosing New > Package.
You have been provided ContactDetailsBean class. It is a very simple class that holds contact details information like address, city, state, country, zipcode, office and resident phone numbers. Copy the class ContactDetailsBean.java from the folder workshop-JSF-tutorial/web/Resources to beans package. Open and review the code.
private String userId =
null; private Integer password = null; private String name = null; private ContactDetailsBean contactDetails = null; |
From the workshop-JSF-tutorial/web/Resources folder, copy the class DataStoreBean.java to beans package and the class MessageFactory.java to util package.
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 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.
You will implement the method registerUser( ) in UserBean class. The method is responsible to add the user to the data store, retrieve FacesMessage instance for localized message with key thanksMsg_Registration, adds it to FacesContext and returns logical outcome success.
If, the user with the same userId already exists then the method retrieves FacesMessage instance for localized message with key userIdExists, adds it to FacesContext and returns null.
Type following lines of code for registerUser( ) method in UserBean class. You can also copy it from the file workshop-JSF-tutorial/web/Typing_Aid/UserBean_registerUser_Method.txt
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"; } } |
If the user already exists, the method adds localized message to FacesContext and returns null . This causes the JavaServer Faces implementation to re-render the jsp page with messages and without creating a new view of the page.
Import FacesContext, ExternalContext classes from the javax.faces.context package, Map class from the java.util package, FacesMessage class from the javax.faces.application and MessageFactory class from the util package.
Save the UserBean class.
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
Open the application.properties file in the WEB-INF/src/java/resources folder ; write following lines and save the file.
userIdExists=The userId {0}
already exists, pls. try another userId. thanksMsg_Registration=Dear {0}, Thank you for registering with Workshop |
To enable JSF framework for instantiating backing beans and store them in appropriate scope, you required to configure it in the application configuration resource file faces-config.xml using 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.
Click one of the following arrows to navigate through the tutorial:
![]() ![]() |