An Introduction to JavaServer Faces (JSF) 9

 

 

 

What do we have in this session 9?

  1. The response.xhtml File

 

 

The response.xhtml File

 

Because the project already contains a file named response.xhtml, and since you know what the template client file should look like now, modify the existing response.xhtml to become the template client file. (For purposes of this tutorial, just copy and paste the provided code.)

 

1.      Open response.xhtml in the editor. (If it is already opened, press Ctrl-Tab and choose it.) Replace the contents of the entire file with the code below.

 

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/
DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://java.sun.com/jsf/facelets"
      xmlns:h="http://java.sun.com/jsf/html">
 
    <body>
 
        <ui:composition template="./template.xhtml">
 
            <ui:define name="title">
                Response
            </ui:define>
 
            <ui:define name="box">
                <h4><h:outputText escape="false" value="#{UserNumberBean.response}"/></h4>
 
                <h:form prependId="false">
 
                    <h:commandButton id="backButton" value="Back" action="greeting" />
 
                </h:form>
            </ui:define>
 
        </ui:composition>
 
    </body>
</html>

 

Note that the file is identical to greeting.xhtml, except for the content specified between the <ui:define> tags for title and box.

 

2.      In the project's web.xml deployment descriptor, modify the welcome file entry so that greeting.xhtml is the page that opens when the application is run.

       In the Projects window, double-click Configuration Files > web.xml to open it in the editor. Under the Pages tab, change the Welcome Files field to faces/greeting.xhtml.

 

 

 

 

 

 

 

-----------------------------------------------------------------------------------------------

Modifying the welcome file entry so that greeting.xhtml is the page that opens when the application is run

 

3.      Run the project to see what it looks like in a browser. Press F6 (fn-F6 on Mac), or click the Run Project ( The NetBeans Run Project button use to run the project) button in the main toolbar. The project is deployed to the GlassFish server, and opens in a browser. Try guessing the number assumed by Duke and see the result!

 

The Hello Duke guessing number game in action ready for input

 

 

The Hello Duke guessing number game response file with the wrong guessed number

---------------------------------------------------------------

 

 

 

 

 

 

 

 

 

 

--------------------------------------------------------------------

The Hello Duke guessing number game response file with the correct guessed number

 

Using the Facelets template and template client files, the application behaves in exactly the same way as it did previously. By factoring out duplicated code in the application's welcome and response pages, you succeeded in reducing the size of the application and eliminated the possibility of writing more duplicate code, should more pages be added at a later point. This can make development more efficient and easier to maintain when working in large projects.

 

 

 

 

< JavaServer Faces 8 | Java and Friends >


JavaServer Faces 1 | JavaServer Faces 2 | JavaServer Faces 3 | JavaServer Faces 4 | JavaServer Faces 5 | JavaServer Faces 6 | JavaServer Faces 7 | JavaServer Faces 8 | JavaServer Faces 9 | Java and Friends