1
\$\begingroup\$

I might finally start to be getting a glimmer of a notion of how to use facelets! I'm just trying to work on the navigation, flow and organization for the sample "guess number" application. It seems as though it's possible to just create small composite component files in the includes directory -- very easy to include them once you know how to do so. However, proper usage of templates looks like it can really help with the organization. When is a template called for? Or, when can you just include a component on the fly?

Currently, everything works: guess a number, get a yes/no response, keep trying until correct.

I'm just interested in the structure and organization. Anything I'm doing right or wrong here? I've tried to put as much into WEB-INF as possible.

Tree:

NetBeansProjects/WebApplicationFacelets/
├── build.xml
├── LICENSE
├── nbproject
│   ├── ant-deploy.xml
│   ├── build-impl.xml
│   ├── genfiles.properties
│   ├── private
│   │   ├── private.properties
│   │   └── private.xml
│   ├── project.properties
│   └── project.xml
├── README.md
├── src
│   ├── conf
│   │   └── MANIFEST.MF
│   └── java
│       ├── hibernate.cfg.xml
│       └── javaeetutorial
│           └── guessnumber
│               └── UserNumberBean.java
└── web
    ├── greeting.xhtml
    ├── index.xhtml
    ├── resources
    │   ├── css
    │   │   ├── cssLayout.css
    │   │   ├── default.css
    │   │   └── tableLayout.css
    │   └── images
    │       └── duke.gif
    ├── response.xhtml
    └── WEB-INF
        ├── facelets
        │   └── template.xhtml
        ├── includes
        │   ├── answer.xhtml
        │   ├── duke.xhtml
        │   └── question.xhtml
        └── web.xml

14 directories, 25 files

template.xhtml:

<?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://xmlns.jcp.org/jsf/facelets"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
        <h:outputStylesheet name="./css/default.css"/>
        <h:outputStylesheet name="./css/tableLayout.css"/>
        <title>Guess Number Facelets Application</title>
    </h:head>
    <h:body>
        <table cellspacing="10px">
            <tr>
                <td id="left">
                    <ui:insert name="left">Left</ui:insert>
                </td>
                <td id="content">
                    <ui:insert name="content">Content</ui:insert>
                </td>
            </tr>
        </table>
    </h:body>
</html>

greeting.xhtml:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
                template="./WEB-INF/facelets/template.xhtml">
    <ui:define name="left">
        <ui:include src="/WEB-INF/includes/duke.xhtml" />
    </ui:define>
    <ui:define name="content">
        <ui:include src="/WEB-INF/includes/question.xhtml" />
    </ui:define>
</ui:composition>

answer.xhtml:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
                template="./WEB-INF/facelets/template.xhtml">
    <ui:define name="left">
        <ui:include src="/WEB-INF/includes/duke.xhtml" />
    </ui:define>
    <ui:define name="content">
        <ui:include src="/WEB-INF/includes/answer.xhtml" />
    </ui:define>
</ui:composition>

question.xhtml:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
                xmlns:h="http://xmlns.jcp.org/jsf/html"
                xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:form>
        <ui:include src="/WEB-INF/includes/duke.xhtml" />
        <h2>
            Hi, my name is Duke. I am thinking of a number from
            #{userNumberBean.minimum} to #{userNumberBean.maximum}.
            Can you guess it?
        </h2>
        <p><h:inputText id="userNo"
                        title="Enter a number from 0 to 10:"
                        value="#{userNumberBean.userNumber}">
                <f:validateLongRange minimum="#{userNumberBean.minimum}"
                                     maximum="#{userNumberBean.maximum}"/>
            </h:inputText>
            <h:commandButton id="submit" value="Submit"
                             action="response"/>
        </p>
        <h:message showSummary="true" showDetail="false"
                   style="color: #d20005;
                   font-family: 'New Century Schoolbook', serif;
                   font-style: oblique;
                   text-decoration: overline"
                   id="errors1"
                   for="userNo"/>
    </h:form>
</ui:composition>

answer.xhtml:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns="http://www.w3.org/1999/xhtml"
                xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
                xmlns:h="http://xmlns.jcp.org/jsf/html"
                xmlns:f="http://xmlns.jcp.org/jsf/core">
    <h:form>  
        <ui:include src="/WEB-INF/includes/duke.xhtml" />
        <h2>
            <h:outputText id="result" value="#{userNumberBean.response}"/>
        </h2>
        <h:commandButton id="back" value="Back" action="greeting"/>
    </h:form>
</ui:composition>

duke.xhtml:

<?xml version='1.0' encoding='UTF-8' ?>
<!DOCTYPE composition PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<ui:composition xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
                xmlns="http://www.w3.org/1999/xhtml"
                xmlns:h="http://xmlns.jcp.org/jsf/html">
    <h:graphicImage value="#{resource['images:duke.gif']}" alt="Duke waving his hand"/>
</ui:composition>
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.