7. Creating the JSP Pages
In this section we are going to focus
on creating the JSP pages for our example JSF application,
which will mimic a simple website login screen. As a
result, we will only need 2 JSP pages, one to prompt the
user to login and the other to indicate that login was
successful. We will call these pages loginUser.jsp
and loginUserSuccess.jsp, respectively. For
simplicity, if there is an authorization error during the
login attempt, we will redirect the user back to the loginUser.jsp
page. To avoid confusion, we are not using any
validation in this demo, but you can easily add validators
to the two inputText/Secret JSF components. We
will use these fields to validate the user's entry for
length and additionally display an error message if the
login was incorrect.
MyEclipse also provides tools that make it simple to create the web pages for our JSF
application by editing the faces-config.xml
file in the MyEclipse JSF Editor. So make sure that
file is still open and we can get started creating our userLogin.jsp page.
To create our userLogin.jsp page we
are going to first click the JSP button, then click on our
canvas. When the new JSP wizard comes up, type in the
File Name and select the JSF template as shown in Figure
7.1:
Figure 7.1: Creating
userLogin.jsp using the faces-config.xml editor
We can also create our userLoginSuccess.jsp
now in the same manner:
Figure 7.2: Creating
userLoginSuccess.jsp using the faces-config.xml editor
Let's start working on our application
now by opening up the userLogin.jsp
page:
Figure 7.3: Begin editing
the userLogin.jsp page
Now, what we need to do now on this
page is:
- Add an h:inputText component for the user name
- Add an h:inputSecret component for the password
- Add an h:outputLabel for the user name
inputText
- Add an h:outputLabel for the password
First thing we need to do is remove the default
template text as well as type in our bundle basename so our
page can use our MessageBundle. We will end up with
JSP that looks like Figure
7.4:
Figure 7.4: Remove
template text and add our MessageBundle to the JSP page

Figure
7.4a: Create the new form
In Figure 7.4a we create the new HTML form element
that will contain our login controls. Now we need to
actually create the input text boxes!
Now let's create our h:inputText component for the
user name, this is shown in Figures
7.6 and 7.7:
Figure 7.6: Adding new
inputText component
Figure 7.7: Adding new
inputText component continued
Now let's add our h:inputSecret
component (no labels yet):
Figure 7.8 Adding new
inputSecret component
Figure 7.9 Adding new
inputSecret component continued
Now let's add the outputLabel's for
both of our input components, starting with our userLabel:
Figure 7.10: Adding
outputLabel component to our userName component
We will also need to add a label for
our h:inputSecret component in the same manner. After we are
done, as mentioned above, let's manually change our
h:outputLabel components to wrap h:outputText components
that are bound to our MessageBundle so we can see them in
the designer, it will look something like this:
Figure 7.11: Adding
outputText components to our labels
Be sure to make the modification to
both the userName label and password label.
Now it is time to add our login
button, we will do that almost identically to how we have
added the other components so far, as shown in Figures 7.12 and 7.13:
Figure 7.12: Adding a new
commandButton component
Figure 7.13: Adding a new
commandButton component continued
Now we have a page that looks
something like this:
Figure 7.14: Our
almost-complete userLogin.jsp page
You might notice that everything looks
pretty ugly on 1 line, so let's add some space to put things
on separate lines:
Figure 7.15: Nicely laid
out userLogin.jsp page
Now that our userLogin.jsp
page is done, let's quickly do our userLoginSuccess.jsp
page which is much simpler. Open that file up and edit it,
simply adding a line to print out the name of the user that
logged in as shown in Figure
7.16:
Figure 7.16: Making
userLoginSuccess.jsp page print out the user's name
Now that we have created our two
pages, the only thing left for us to do is hook them
together with proper Navigation Cases, that is done by
visual editing our faces-config.xml
file, so open that file. After the file is open for editing,
perform the following steps to create the navigation
case:
-
Click the Navigation Case tool
-
Click your userLogin.jsp
file
-
Then click on your userLoginSuccess.jsp
file
-
You will be prompted with a
wizard to create the navigation case.
Follow Figures 7.17 and 7.18 for
creating the success
navigation case. In order to create the failure navigation
case, we simply do the same steps as before but click twice
on the userLogin.jsp
file, in order to create a circular navigation case.
Figure 7.17: Creating the
success navigation case
Figure 7.18: Creating the
success navigation case continued
After we have created both navigation
cases, our file will look something like this:
Figure 7.19: Reviewing
navigation cases for our app
Now that we have created all our JSP pages and added our
Navigation Cases correctly, the only thing left is to run
our application!
|