A Web Module That Uses JavaServer Faces Technology: The hello1 Example
The hello1 application is a web module that uses JavaServer Faces
technology to display a greeting and response. You can use a text editor
to view the application files, or you can use NetBeans IDE.
The source code for this application is in the
tut-install/examples/web/jsf/hello1/ directory.
The following topics are addressed here:
To View the hello1 Web Module Using NetBeans IDE
To view the hello1 web module using NetBeans IDE:
-
From the File menu, choose Open Project.
-
In the Open Project dialog box, navigate to:
tut-install/examples/web/jsf -
Select the
hello1folder and click Open Project. -
Expand the Web Pages node and double-click the
index.xhtmlfile to view it in the editor.The
index.xhtmlfile is the default landing page for a Facelets application. In a typical Facelets application, web pages are created in XHTML. For this application, the page uses simple tag markup to display a form with a graphic image, a header, a field, and two command buttons:<!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"> <h:head> <title>Facelets Hello Greeting</title> </h:head> <h:body> <h:form> <h:graphicImage url="#{resource['images:duke.waving.gif']}" alt="Duke waving his hand"/> <h2>Hello, my name is Duke. What's yours?</h2> <h:inputText id="username" title="My name is: " value="#{hello.name}" required="true" requiredMessage="Error: A name is required." maxlength="25" /> <p></p> <h:commandButton id="submit" value="Submit" action="response"> </h:commandButton> <h:commandButton id="reset" value="Reset" type="reset"> </h:commandButton> </h:form> ... </h:body> </html>The most complex element on the page is the
inputTextfield. Themaxlengthattribute specifies the maximum length of the field. Therequiredattribute specifies that the field must be filled out; therequiredMessageattribute provides the error message to be displayed if the field is left empty. Thetitleattribute provides the text to be used by screen readers for the visually disabled. Finally, thevalueattribute contains an expression that will be provided by theHellomanaged bean.The web page connects to the
Hellomanaged bean through the Expression Language (EL) value expression#{hello.name}, which retrieves the value of thenameproperty from the managed bean. Note the use ofhelloto reference the managed beanHello. If no name is specified in the@Namedannotation of the managed bean, the managed bean is always accessed with the first letter of the class name in lowercase.The Submit
commandButtonelement specifies the action asresponse, meaning that when the button is clicked, theresponse.xhtmlpage is displayed. -
Double-click the
response.xhtmlfile to view it.The response page appears. Even simpler than the greeting page, the response page contains a graphic image, a header that displays the expression provided by the managed bean, and a single button whose
actionelement transfers you back to theindex.xhtmlpage:<!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"> <h:head> <title>Facelets Hello Response</title> </h:head> <h:body> <h:form> <h:graphicImage url="#{resource['images:duke.waving.gif']}" alt="Duke waving his hand"/> <h2>Hello, #{hello.name}!</h2> <p></p> <h:commandButton id="back" value="Back" action="index" /> </h:form> </h:body> </html> -
Expand the Source Packages node, then the
javaeetutorial.hello1node. -
Double-click the
Hello.javafile to view it.The
Helloclass, called a managed bean class, provides getter and setter methods for thenameproperty used in the Facelets page expressions. By default, the expression language refers to the class name, with the first letter in lowercase (hello.name).package javaeetutorial.hello1; import javax.enterprise.context.RequestScoped; import javax.inject.Named; @Named @RequestScoped public class Hello { private String name; public Hello() { } public String getName() { return name; } public void setName(String user_name) { this.name = user_name; } }If you use the default name for the bean class, you can specify
@Modelas the annotation instead of having to specify both@Namedand@RequestScoped. The@Modelannotation is called a stereotype, a term for an annotation that encapsulates other annotations. It is described later in Using Stereotypes in CDI Applications. Some examples will use@Modelwhere it is appropriate. -
Under the Web Pages node, expand the WEB-INF node and double-click the
web.xmlfile to view it.The
web.xmlfile contains several elements that are required for a Facelets application. All of the following are created automatically when you use NetBeans IDE to create an application.-
A context parameter specifying the project stage:
<context-param> <param-name>javax.faces.PROJECT_STAGE</param-name> <param-value>Development</param-value> </context-param>A context parameter provides configuration information needed by a web application. An application can define its own context parameters. In addition, JavaServer Faces technology and Java Servlet technology define context parameters that an application can use.
-
A
servletelement and itsservlet-mappingelement specifying theFacesServlet. All files with the.xhtmlsuffix will be matched:<servlet> <servlet-name>Faces Servlet</servlet-name> <servlet-class>javax.faces.webapp.FacesServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>Faces Servlet</servlet-name> <url-pattern>*.xhtml</url-pattern> </servlet-mapping> -
A
welcome-file-listelement specifying the location of the landing page:<welcome-file-list> <welcome-file>index.xhtml</welcome-file> </welcome-file-list>
-
Introduction to Scopes
In the Hello.java class, the annotations javax.inject.Named and
javax.enterprise.context.RequestScoped identify the class as a managed
bean using request scope. Scope defines how application data persists
and is shared.
The most commonly used scopes in JavaServer Faces applications are the following:
-
Request (
@RequestScoped): Request scope persists during a single HTTP request in a web application. In an application likehello1, in which the application consists of a single request and response, the bean uses request scope. -
Session (
@SessionScoped): Session scope persists across multiple HTTP requests in a web application. When an application consists of multiple requests and responses where data needs to be maintained, beans use session scope. -
Application (
@ApplicationScoped): Application scope persists across all users' interactions with a web application.
Packaging and Deploying the hello1 Web Module
A web module must be packaged into a WAR in certain deployment scenarios
and whenever you want to distribute the web module. You can package a
web module into a WAR file by using Maven or by using the IDE tool of
your choice. This tutorial shows you how to use NetBeans IDE or Maven to
build, package, and deploy the hello1 sample application.
You can deploy a WAR file to GlassFish Server by:
-
Using NetBeans IDE
-
Using the
asadmincommand -
Using the Administration Console
-
Copying the WAR file into the
domain-dir/autodeploy/directory
Throughout the tutorial, you will use NetBeans IDE or Maven for packaging and deploying.
To Build and Package the hello1 Web Module Using NetBeans IDE
To build and package the hello1 web module using NetBeans IDE:
-
Start GlassFish Server as described in To Start GlassFish Server Using NetBeans IDE, if you have not already done so.
-
From the File menu, choose Open Project.
-
In the Open Project dialog box, navigate to:
tut-install/examples/web/jsf -
Select the
hello1folder. -
Click Open Project.
-
In the Projects tab, right-click the
hello1project and select Build. This command deploys the project to the server.
To Build and Package the hello1 Web Module Using Maven
To build and package the hello1 web module using Maven:
-
Start GlassFish Server as described in To Start GlassFish Server Using the Command Line, if you have not already done so.
-
In a terminal window, go to:
tut-install/examples/web/jsf/hello1/ -
Enter the following command:
This command spawns any necessary compilations and creates the WAR file in
tut-install/examples/web/jsf/hello1/target/. It then deploys the project to the server.
Viewing Deployed Web Modules
GlassFish Server provides two ways to view the deployed web modules: the
Administration Console and the asadmin command. You can also use
NetBeans IDE to view deployed modules.
To View Deployed Web Modules Using the Administration Console
To view deployed web modules using the Administration Console:
-
Open the URL
http://localhost:4848/in a browser. -
Select the Applications node.
The deployed web modules appear in the Deployed Applications table.
To View Deployed Web Modules Using the asadmin Command
Enter the following command:
asadmin list-applications
To View Deployed Web Modules Using NetBeans IDE
To view deployed web modules using NetBeans IDE:
-
In the Services tab, expand the Servers node, then expand the GlassFish Server node.
-
Expand the Applications node to view the deployed modules.
Running the Deployed hello1 Web Module
Now that the web module is deployed, you can view it by opening the
application in a web browser. By default, the application is deployed to
host localhost on port 8080. The context root of the web application
is hello1.
To run the deployed hello1 web module:
-
Open a web browser.
-
Enter the following URL:
http://localhost:8080/hello1/ -
In the field, enter your name and click Submit.
The response page displays the name you submitted. Click Back to try again.
Dynamic Reloading of Deployed Modules
If dynamic reloading is enabled, you do not have to redeploy an
application or module when you change its code or deployment
descriptors. All you have to do is copy the changed pages or class files
into the deployment directory for the application or module. The
deployment directory for a web module named context-root is
domain-dir/applications/context-root. The server checks for changes
periodically and redeploys the application, automatically and
dynamically, with the changes.
This capability is useful in a development environment because it allows code changes to be tested quickly. Dynamic reloading is not recommended for a production environment, however, because it may degrade performance. In addition, whenever a reload takes place, the sessions at that time become invalid, and the client must restart the session.
In GlassFish Server, dynamic reloading is enabled by default.
Undeploying the hello1 Web Module
You can undeploy web modules and other types of enterprise applications by using either NetBeans IDE or Maven.
To Undeploy the hello1 Web Module Using NetBeans IDE
To undeploy the hello1 web module using NetBeans IDE:
-
In the Services tab, expand the Servers node, then expand the GlassFish Server node.
-
Expand the Applications node.
-
Right-click the
hello1module and select Undeploy. -
To delete the class files and other build artifacts, go back to the Projects tab, right-click the project, and select Clean.
To Undeploy the hello1 Web Module Using Maven
To undeploy the hello1 web module using Maven:
-
In a terminal window, go to:
tut-install/examples/web/jsf/hello1/ -
Enter the following command:
-
To delete the class files and other build artifacts, enter the following command: