type=page
status=published
title=The simplegreeting CDI Example
next=cdi-basicexamples003.html
prev=cdi-basicexamples001.html
~~~~~~
= The simplegreeting CDI Example

[[GJBJU]]

[[the-simplegreeting-cdi-example]]
The simplegreeting CDI Example
------------------------------

The `simplegreeting` example illustrates some of the most basic features
of CDI: scopes, qualifiers, bean injection, and accessing a managed bean
in a JavaServer Faces application. When you run the example, you click a
button that presents either a formal or an informal greeting, depending
on how you edited one of the classes. The example includes four source
files, a Facelets page and template, and configuration files.

The following topics are addressed here:

* link:#GJCQS[The simplegreeting Source Files]
* link:#GJDOJ[The Facelets Template and Page]
* link:#GJCYM[Running the simplegreeting Example]

[[GJCQS]]

[[the-simplegreeting-source-files]]
The simplegreeting Source Files
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

The four source files for the `simplegreeting` example are:

* The default `Greeting` class, shown in
link:cdi-basic005.html#GIZKS[Beans as Injectable Objects]
* The `@Informal` qualifier interface definition and the
`InformalGreeting` class that implements the interface, both shown in
link:cdi-basic006.html#GJBCK[Using Qualifiers]
* The `Printer` managed bean class, which injects one of the two
interfaces, shown in full in link:cdi-basic010.html#GJBBP[Adding Setter
and Getter Methods]

The source files are located in the
`_tut-install_/examples/cdi/simplegreeting/src/main/java/javaeetutorial/simplegreeting`
directory.

[[GJDOJ]]

[[the-facelets-template-and-page]]
The Facelets Template and Page
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

To use the managed bean in a simple Facelets application:

1.  Use a very simple template file and `index.xhtml` page.
+
The template page, `template.xhtml`, looks like this:
+
[source,oac_no_warn]
----
<?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 lang="en"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:h="http://xmlns.jcp.org/jsf/html"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets">
    <h:head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"/>
        <h:outputStylesheet library="css" name="default.css"/>
        <title><ui:insert name="title">Default Title</ui:insert></title>
    </h:head>

    <body>
        <div id="container">
            <div id="header">
                <h2><ui:insert name="head">Head</ui:insert></h2>
            </div>

            <div id="space">
                <p></p>
            </div>

            <div id="content">
                <ui:insert name="content"/>
            </div>
        </div>
    </body>
</html>
----
2.  To create the Facelets page, redefine the title and head, then add a
small form to the content:
+
[source,oac_no_warn]
----
<?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 lang="en"
      xmlns="http://www.w3.org/1999/xhtml"
      xmlns:ui="http://xmlns.jcp.org/jsf/facelets"
      xmlns:h="http://xmlns.jcp.org/jsf/html">
    <ui:composition template="/template.xhtml">

        <ui:define name="title">Simple Greeting</ui:define>
        <ui:define name="head">Simple Greeting</ui:define>
        <ui:define name="content">
            <h:form id="greetme">
               <p><h:outputLabel value="Enter your name: " for="name"/>
                  <h:inputText id="name" value="#{printer.name}"/></p>
               <p><h:commandButton value="Say Hello"
                                   action="#{printer.createSalutation}"/></p>
               <p><h:outputText value="#{printer.salutation}"/> </p>
            </h:form>
        </ui:define>

    </ui:composition>
</html>
----
+
The form asks the user to enter a name. The button is labeled *Say Hello*,
and the action defined for it is to call the `createSalutation` method
of the `Printer` managed bean. This method in turn calls the `greet`
method of the defined `Greeting` class.
+
The output text for the form is the value of the greeting returned by
the setter method. Depending on whether the default or the `@Informal`
version of the greeting is injected, this is one of the following, where
`name` is the name entered by the user:
+
[source,oac_no_warn]
----
Hello, name.

Hi, name!
----
+
The Facelets page and template are located in the
`_tut-install_/examples/cdi/simplegreeting/src/main/webapp/` directory.
+
The simple CSS file that is used by the Facelets page is in the
following location:
+
[source,oac_no_warn]
----
tut-install/examples/cdi/simplegreeting/src/main/webapp/resources/css/default.css
----

[[GJCYM]]

[[running-the-simplegreeting-example]]
Running the simplegreeting Example
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

You can use either NetBeans IDE or Maven to build, package, deploy, and
run the `simplegreeting` application.

The following topics are addressed here:

* link:#GJCXP[To Build, Package, and Run the simplegreeting Example
Using NetBeans IDE]
* link:#GJCZT[To Build, Package, and Deploy the simplegreeting Example
Using Maven]
* link:#GJCZE[To Run the simplegreeting Example]

[[GJCXP]]

[[to-build-package-and-run-the-simplegreeting-example-using-netbeans-ide]]
To Build, Package, and Run the simplegreeting Example Using NetBeans IDE
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1.  Make sure that GlassFish Server has been started (see
link:usingexamples002.html#BNADI[Starting and Stopping GlassFish
Server]).
2.  From the *File* menu, choose *Open Project*.
3.  In the *Open Project* dialog box, navigate to:
+
[source,oac_no_warn]
----
tut-install/examples/cdi
----
4.  Select the `simplegreeting` folder.
5.  Click *Open Project*.
6.  To modify the `Printer.java` file, perform these steps:
a.  Expand the *Source Packages* node.
b.  Expand the `greetings` node.
c.  Double-click the `Printer.java` file.
d.  In the editor, comment out the `@Informal` annotation:
+
[source,oac_no_warn]
----
@Inject
//@Informal
Greeting greeting;
----
e.  Save the file.
7.  In the *Projects* tab, right-click the `simplegreeting` project and
select *Build*.
+
This command builds and packages the application into a WAR file,
`simplegreeting.war`, located in the `target` directory, and then
deploys it to GlassFish Server.

[[GJCZT]]

[[to-build-package-and-deploy-the-simplegreeting-example-using-maven]]
To Build, Package, and Deploy the simplegreeting Example Using Maven
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1.  Make sure that GlassFish Server has been started (see
link:usingexamples002.html#BNADI[Starting and Stopping GlassFish
Server]).
2.  In a terminal window, go to:
+
[source,oac_no_warn]
----
tut-install/examples/cdi/simplegreeting/
----
3.  Enter the following command to deploy the application:
+
[source,oac_no_warn]
----
mvn install
----
+
This command builds and packages the application into a WAR file,
`simplegreeting.war`, located in the `target` directory, and then
deploys it to GlassFish Server.

[[GJCZE]]

[[to-run-the-simplegreeting-example]]
To Run the simplegreeting Example
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

1.  In a web browser, enter the following URL:
+
[source,oac_no_warn]
----
http://localhost:8080/simplegreeting
----
+
The *Simple Greeting* page opens.
2.  Enter a name in the field.
+
For example, suppose that you enter `Duke`.
3.  Click *Say Hello*.
+
If you did not modify the `Printer.java` file, then the following text string
appears below the button:
+
[source,oac_no_warn]
----
Hi, Duke!
----
+
If you commented out the `@Informal` annotation in the `Printer.java`
file, then the following text string appears below the button:
+
[source,oac_no_warn]
----
Hello, Duke.
----
