Step 2:- Declaring the Bean class in the faces-config.xml file
The most important part of using bean is to define the bean in the faces-config.xml file with <managed-bean></managed-bean> tag. The following mapping for the RegistrationBean.java file file is defined in the xml file.
<?xml version='1.0' encoding='UTF-8'?>
<!-- =========== FULL CONFIGURATION FILE ================================== -->
<faces-config version="1.2" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-facesconfig_1_2.xsd">
<managed-bean> <managed-bean-name>RegistrationBean</managed-bean-name> <managed-bean-class>com.visualbuilder.RegistrationBean</managed-bean-class> <managed-bean-scope>request</managed-bean-scope> </managed-bean>
</faces-config>
|
Step 3:- Binding the Form Elements with the Bean properties.
The last step is to map the form elements with the bean properties. So that when user submit the form then it will bind set and get the values from the appropriate setter and getter methods. The binding is done with the unified EL language. The #{} in the value attribute will bind the set the values from the bean objects. The following is the code for the Registraton.jsp after mapping the values from the bean.
<%@page contentType="text/html"%> <%@page pageEncoding="UTF-8"%>
<%@taglib prefix="f" uri="http://java.sun.com/jsf/core"%> <%@taglib prefix="h" uri="http://java.sun.com/jsf/html"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<%-- This file is an entry point for JavaServer Faces application. --%>
<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <h1 align="center"> Registration Page</h1> </head> <body>
<f:view> <h:outputLabel value="#{RegistrationBean.exampleText}"/> <h:form id="form1"> <table align="center"> <tr> <td>Name:</td> <td><h:inputText id="name" label="Name" value="#{RegistrationBean.name}"/></td> </tr> <tr> <td>Password:</td> <td><h:inputSecret id="password" label="Password" value="#{RegistrationBean.password}"/></td> </tr> <tr> <td>Retype Password:</td> <td><h:inputSecret id="password1" label="Retype Password" value="#{RegistrationBean.password1}"/></td> </tr> <tr> <td>Description</td> <td><h:inputTextarea id="description" rows="10" cols="30" value="#{RegistrationBean.description}"/></td> </tr> <tr> <td colspan="2" align="center"><h:commandButton id="submit" action="success" value="Submit" /></td> </tr> </table>
</h:form> </f:view> </body> </html>
|