Editing pages code sources
In this section, we will generate the source code of the register.jsp
and success.jsp
pages. You will also see how to develop a start page for the application.
Editing the register.jsp page
For editing the source code of a page that appears in the Diagram view, just double-click on it. For example, double-click on the register.jsp icon to open the page source in the Visual Page Editor as you can see in the following screenshot:
Now, we can use the JBoss Tools Palette (for details see Chapter 3) for editing the page source code in an easy manner. For example, for editing the register.jsp
source code, follow these steps:
- Place the cursor in source code, right after the
f:view
tag. - Type here the following code and place the cursor after it:
<h2>Registration form:</h2>
- Expand the JSF HTML section of the JBoss Tools Palette.
- Click on the form tag and wait until the Insert Tag window appears.
- Type success in the Value column, next to the id row, and click on Finish.
- Place the cursor inside the
h:form
tag, and click on the panelGrid tag from the JBoss Tools Palette. Wait until the Insert Tag window appears. - Type 3 in the Value column, next to the columns row, and click on Finish.
- Place the cursor inside the
h:panelGrid
tag. - Click on the outputLabel tag from the JBoss Tools Palette.
- In the Insert Tag window, type #{personBean.personName} in the Value column next to the for row. You can fill this field by manually typing or by browsing to the
personName
property (as shown in the following screenshot). - Switch to Advanced tab. Type personNameLabel in the Value column, next to the id row and click on Finish.
- Place the cursor inside the
h:outputLabel
tag and click on the outputText tag from the JBoss Tools Palette. - In the Insert Tag window, type Your Name: in the Value column, next to the value row, and then click on Finish.
Steps 9—13 will generate the following source code:
<h:outputLabel for="#{personBean.personName}" id="personNameLabel"> <h:outputText value="Your Name:"/> </h:outputLabel>
- Now, place the cursor after the
h:outputLabel
and repeat steps 9—13 for inserting a label with text for every property of thePersonBean
managed bean (personAge, personBirthDate, personPhone
). The generated source code should look like this:<h:outputLabel for="#{personBean.personAge}" id="personAgeLabel"> <h:outputText value="Your Age:"/> </h:outputLabel> <h:outputLabel for="#{personBean.personBirthDate}" id="personBirthDateLabel"> <h:outputText value="Your Birth Date (dd/MM/yyyy):"/> </h:outputLabel> <h:outputLabel for="#{personBean.personPhone}" id="personPhoneLabel"> <h:outputText value="Your Phone Number (x[x]-xxx-xxx-xxxx):"/> </h:outputLabel>
- Place the cursor between the first and the second
h:outputLabel
tags. - Click on the inputText tag from the JBoss Tools Palette.
- In the Insert Tag window, switch to Advanced tab. Type personName in the Value column, next to the id row.
- Also, type #{personBean.personName} in the Value column, next to the value row. You can fill in this field manually or by browsing to the
personName
property. Click Finish.Steps 15—17 generate the following source code:
<h:inputText id="personName" value="#{personBean.personName}"/>
Now, place the cursor after each
h:outputLabel
tag and repeat steps 15—17 for inserting a text field for each property of thePersonBean
managed bean (personAge, personBirthDate, personPhone
). The generated source code should look like this:<h:inputText id="personAge" value="#{personBean.personAge}"/> <h:inputText id="personBirthDate" value="#{personBean.personBirthDate}"/> <h:inputText id="personPhone" value="#{personBean.personPhone}"/>
- Place the cursor after the first
h:inputText
tag. - Click on the message tag from the JBoss Tools Palette.
- In the Insert Tag window, switch to Advanced tab. Type personName in the Value column, next to the for row.
- Type color: red; text-decoration: overline in the Value column, next to the style row.
- Type personNameError in the Value column, next to the id row. Click Finish.
Steps 19—22 generate the following source code:
<h:message for="personName" id="personNameError" style="color: red; text-decoration: overline"/>
Now, place the cursor after each h:inputText tag and repeat steps 19—22 for inserting a h:message tag for each property of the
PersonBean
managed bean (personAge, personBirthDate, personPhone
). The generated source code should look like this:<h:message for="personAge" id="personAgeError" style="color: red; text-decoration: overline"/> <h:message for="personBirthDate" id="personBirthDateError" style="color: red; text-decoration: overline"/> <h:message for="personPhone" id="personPhoneError" style="color: red; text-decoration: overline"/>
- Place the cursor after the
h:panelGrid
tag. - Click on the commandButton tag from the JBoss Tools Palette.
- In the Insert Tag window, type success in the Value column, next to the action row.
- Type Register in the Value column, next to the value row. Click on Finish.
- Place the cursor inside the
h:inputText
tag that corresponds to thepersonAge
property. Place here the following default converter and validator:<h:inputText id="personAge" value="#{personBean.personAge}"> <f:converter converterId="javax.faces.Integer"/> <f:validateLongRange maximum="150" minimum="0"/> </h:inputText>
- Place the cursor inside the
h:inputText
tag that corresponds to thepersonBirthDate
property. Place here the following default converter:<h:inputText id="personBirthDate" value="#{personBean.personBirthDate}"> <f:convertDateTime pattern="dd/MM/yyyy"/> </h:inputText>
- Place the cursor inside the
h:inputText
tag that corresponds to thepersonPhone
property. Place here the following custom converter:<f:converter converterId="phoneConverter" />
- Place the cursor inside the
h:inputText
tag that corresponds to thepersonPhone
property. Place here the following custom validator:<f:validator validatorId="phoneValidator" />
The complete source code of register.jsp
is:
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %> <html> <head> <title></title> </head> <body> <f:view> <h2>Registration form:</h2> <h:form id="success"> <h:panelGrid columns="3"> <h:outputLabel for="#{personBean.personName}" id="personNameLabel"> <h:outputText value="Your Name:"/> </h:outputLabel> <h:inputText id="personName" value="#{personBean.personName}"/> <h:message for="personName" id="personNameError" style="color: red; text-decoration: overline"/> <h:outputLabel for="#{personBean.personAge}" id="personAgeLabel"> <h:outputText value="Your Age:"/> </h:outputLabel> <h:inputText id="personAge" value="#{personBean.personAge}"> <f:converter converterId="javax.faces.Integer"/> <f:validateLongRange maximum="150" minimum="0"/> </h:inputText> <h:message for="personAge" id="personAgeError" style="color: red; text-decoration: overline"/> <h:outputLabel for="#{personBean.personBirthDate}" id="personBirthDateLabel"> <h:outputText value="Your Birth Date (dd/MM/yyyy):"/> </h:outputLabel> <h:inputText id="personBirthDate" value="#{personBean.personBirthDate}"> <f:convertDateTime pattern="dd/MM/yyyy"/> </h:inputText> <h:message for="personBirthDate" id="personBirthDateError" style="color: red; text-decoration: overline"/> <h:outputLabel for="#{personBean.personPhone}" id="personPhoneLabel"> <h:outputText value="Your Phone Number (x[x]-xxx-xxx-xxxx):"/> </h:outputLabel> <h:inputText id="personPhone" value="#{personBean.personPhone}"> <f:converter converterId="phoneConverter" /> <f:validator validatorId="phoneValidator" /> </h:inputText> <h:message for="personPhone" id="personPhoneError" style="color: red; text-decoration: overline"/> </h:panelGrid> <h:commandButton action="success" value="Register"/> </h:form> </f:view> </body> </html>
Editing the success.jsp page
Using the JBoss Tools Palette and the experience from the above section, you can easily develop a success.jsp
page that looks like this (here, we just display on screen the submitted information using a set of h:outputText
tags):
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> <%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %> <html> <head> <title></title> </head> <body> <f:view> <h2>Registration successfully done!</h2> <h:panelGrid columns="2"> <h:outputText value="Inserted Name:"/> <h:outputText value="#{personBean.personName}"/> <h:outputText value="Inserted Age:"/> <h:outputText value="#{personBean.personAge}"/> <h:outputText value="Inserted Birth Date:"/> <h:outputText value="#{personBean.personBirthDate}"/> <h:outputText value="Inserted Phone Number:"/> <h:outputText value="#{personBean.personPhone.allNumber}"/> <h:outputText value="Inserted Phone Number Country Code:"/> <h:outputText value="#{personBean.personPhone.countryCode}"/> <h:outputText value="Inserted Phone Number Area Code:"/> <h:outputText value="#{personBean.personPhone.areaCode}"/> <h:outputText value="Inserted Phone Number Prefix:"/> <h:outputText value="#{personBean.personPhone.prefixNumber}"/> </h:panelGrid> </f:view> </body> </html>
Editing a start page for the registerJSF project
Now, it is time to create a start page for the application. This can be done by following these steps:
- In the Package Explorer view navigate to your WebContent directory (registerJSF | WebContent).
- Right-click on the WebContent directory and select the New | JSP File from the contextual menu.
- In the New JSP File window, type index in the Name field and select JSPRedirect option from the Template field. Click on Finish (if you click on the Next button, you will be able to add tag libraries to your JSP—here it is not the case).
- A JSP editor will open the source code of the
index.jsp
. Type /pages/register.jsf between the quotes of thepage
attribute.
The start page will look like this:
<!doctype html public "-//w3c//dtd html 4.0 transitional//en"> <html> <head></head> <body> <jsp:forward page="/pages/register.jsf" /> </body> </html>
Save the project status by selecting the Save option from the File main menu.
Testing the registerJSF project
Finally, it is time to test our registerJSF
project to see how it works. For this, start the JBoss 4.2 Server server (do this from JBoss AS view) and deploy the application on this server (do this by , and then on the project name in Package Explorer view and select Run As | Run On Server option).
The application will start in Eclipse Internal Web Browser, but you may also open it in your own web browser by accessing the http://localhost:8080/registerJSF/
URL. Following are some screenshots of the application: