Web Technologies
|
|
JSF on Tomcat
JSF on Tomcat
1. Introduction
JSF stands for Java Server Faces. This technology, based on MVC
architecture is the used as an interface framework for J2EE applications.
To create a JSF application, we perform the seven following steps:
1. Define and implement the application Model classes,
2. Describe the Model to the framework,
3. Create application Views using JSP pages,
4. Define data validation rules,
5. Define View navigation for the Controller,
6. Define Web descriptor, and
7. Run the application over the server.
1. Define and implement the application Model classes (JavaBeans).
The application requests three values from the user: a string, an integer,
and a real number. The application models in JSF are implemented as server-side
JavaBeans. The application Model class can take one or several classes. The general
code for a such class is
as follows
2. Describe the Model to the framework: faces-config.xml
The configuration file named faces-config.xml is used to configure the
JSF controller. It is placed in the WEB-INF directory, alongside the Web deployment
descriptor file: web.xml. Here is the simple code for tag:
<managed-bean>
<managed-bean-name>nameBean</managed-bean-name>
<managed-bean-class>APackage.ExampleBean
</managed-bean-class>
<managed-bean-scope>session or request</managed-bean-scope>
</managed-bean>
The Controller names the bean "nameBean", and stores this bean in session scope.
Once an application component accesses the name "nameBean", the Controller
creates one and stores it in session scope for later use.
3. Create application Views using JSP pages : Page.jsp
JSF defines two sets of standard tags:
The first is called core. It
handles the functions as converting between types, listens for user events,
and validates user inputs.
The second set of tags is called html. It is the
input model used for generating HTML views. A JSF application begins with
with the two declaration related to the two tag libraries as in
the following example code
The JSF application is wrapped by the tag . Inside this tag, the tag
defines an input form. In a traditional HTML form, we have to set the METHOD and ACTION
attributes; but in JSF they are already predefined.
Inside the form, we find the tag called . It defines
a textual input element. It is identified, on the page, by its attribute id.
The attribute assignment: value="#{adata.inputWord}" binds the contents of this input
field "inputWord" to the value of the "adata" bean's string property.
4. Define data validation rules
It is defined, in the JSP page above, two data validation rules. The first is just to limit
the entry to a sentence with a nomber of characters between 10 and 25; the second validation
will print an error message if the variable's value fails validation. this validations rules
are set for the server-side; for the client-side validation runs as JavaScript in the Web
browser. The data valiadtion rules states for the Controller what to do when errors occur.
The previous section explained what the Controller does when it finds input validation errors. when
the inputs are valid, the Controller uses the action to perform the next task.
The inputText tag's "required" attribute set to "true" means that this field must
be filled. The "for" attribute (here inputWord) must match the id attribute of one of the
other components on the page. If a message is rendered, it will match element whose id is inputWord.
We can also use style (here color) inside the tag. Since JSF supports CSS, we can use class attributes.
5. Define View navigation for the Controller
In response to the user inputs, the Controller uses the event "action" to know which views to display.
The application (JSP page) includes also a <h:commandButton> tag in its form, that is the button
used to post the form to the Controller; for example:
<h:commandButton id="submit" action="validated" value="Submit"/>
The related rules are written in the page navigation "faces-config.xml". They are:
<navigation-rule>
<from-view-id>/jsp/firstPage.jsp</from-view-id>
<navigation-case>
<from-outcome>validated</from-outcome>
<to-view-id>/jsp/followingPage.jsp</to-view-id>
</navigation-case>
</navigation-rule>
If the controller receives valid inputs from a form in the page /jsp/firstPage.jsp,
and the action is 'validated', then it goes to page /jsp/followingPage.jsp.
6. Define the web descriptor: web.xml
A JSF application is a Web application. It requires then a web.xml deployment descriptor.
This descriptor needs a servlet mapping to map the JSF controller to the URL /faces/.
This directory /faces/ is replaced by *.jsf (all the files.jsp) when the controller
serves the .jsp files. The servlet mapping in the web.xml deployment descriptor could be:
<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>/faces/* or *.jsf </url-pattern>
</servlet-mapping>
The related used WAR (Web Archive) files in JSF Reference Implementation include the
two libraries, jsf-api.jar and jsf-impl.jar, in its directory /WEB-INF/lib.
7. Run the application over the server
The application is started by running the /jsp/firstPage.jsp. The related server is
the Tomcat's sever. On a browser, the related URL is :
http://localhost:port#/jsp/firstPage.jsp, when \jsp\ is the immediate subdirectory of
\webapps\directory.
2. Installation
The steps:
1. Download the version of tomcat that fits the related JFS libraries. We will use
JFS-1.2, then Apache 6.0.13. Go to
>archive.apache.org
and download: the binary: apache-tomcat-6.0.13.zip, and the wizard apache-tomcat-6.0.13.exe
Unzip the file apache-tomcat-6.0.13.zip in a directory. Inside this same
directory place apache-tomcat-6.0.13.exe binary and double-click on it to
install Tomcat. Choose the port (here 8055). Tomcat is installed in the
directory: C:\Program Files\Apache Software Foundation\Tomcat 6.0
2. Go to
jakarta-taglibs
and download : jakarta-taglibs-standard-1.0.6.zip.
Unzip this file to have the directory
jakarta-taglibs-standard-1.0.6 that contains \lib directory which contains among the others
the two jar files jstl.jar and standard.jar
3. Go to
jsk12 Glassfish
and download : jsf-1.2_04-b10-p01.zip.
Unzip this file to have the directory
jsf-1.2_04-b10-p01 that contains \lib directory which contains among the others
the two jar files jsf-api.jar and jsf-impl.jar
4. Now, go to Tomcat directory, in : C:\Program Files\Apache Software Foundation\Tomcat 6.0\webapps
and create there a director (here JSF). Inside \JSF directory, create \WEB-INF directory. Inside this
\WEB-INF directory create \lib directory. Copy the four jar files: jstl.jar, standard.jar,
jsf-api.jar, and jsf-impl.jar in this \lib directory.
Now, the necssary programs are in place.
3. First example:
The steps:
1. Inside the directory \JSF, create two files:
index.jsp
<jsp:forward page="luck.jsf"/>
and luck.jsp:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<%@taglib uri="http://java.sun.com/jsf/html" prefix="h"%>
<%@taglib uri="http://java.sun.com/jsf/core" prefix="f"%>
<html>
<body>
<f:view>
<h:outputText value="Regards and all the best of luck."/>
</f:view>
</body>
</html>
2. Inside the directory \JSF\WEB-INF, create two files:
faces-config.xml
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE faces-config PUBLIC
"-//Sun Microsystems, Inc.//DTD JavaServer Faces Config 1.1//EN"
"http://java.sun.com/dtd/web-facesconfig_1_1.dtd">
<faces-config>
</faces-config>
and web.xml:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd">
<web-app>
<!-- Faces Servlet -->
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
<load-on-startup> 1 </load-on-startup>
</servlet>
<!-- Faces Servlet Mapping -->
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
<url-pattern>*.jsf</url-pattern>
</servlet-mapping>
</web-app>
3. Lanch the application in the server. The output looks like this
first example with JSF
4. Second example:
In this example, we want just send a word and receive it via the server.
We construct a subdirectory \CONTACT\ under \webapps directory; then two
subdirectorie \jsf\ which contains :
contact.jsp (send file)
and
greetings.jsp (receive file);
and WEB-INF subdirectory that contains the files :
faces-config.xml;
and
>web.xml;
and \lib and \src subdirectories.
The \lib directory contains the jar files jsf-api.jar, jsf-impl.jar, jstl.jar,
standard.jar, and password.jar. The password.jar file is build by ant. The \src
directory contains \java directory which contains the
PasswordBean.java.
The directory \classes is built by ant and contains
PasswordBean.class.
The build.xml file compile PasswordBean.java and place PasswordBean.class
in the \class directory; build Passprd.jar and place it in the \lib\ directory.
To have a BuildFile built.xml file, go to
ant.apache.org.
and define aa example. Here is the BuidFile used here:
build.xml.
Now set the command-line at the directory where build.xml is present and run ant
to get this output:
build.xml on command line.
Run the application on the server to get:
>sending word,
and
receiving the related word.
5. Third example:
In this example we want to login in an account with parmission.
The steps are the same as in the second example, except that we need
also the servlet-api.jar file in the \WEB-INF\lib directory. Bring
it from Tomcat 6.0\lib directory.
Construct the directory
ExampleLogin.
under the \webapps directory.
\ExampleLogin contains:
login.jsp,
success.jsp,
and
failure.jsp,
and WEB-INF subdirectory, that contains:
faces-config.xml,
and
web.xml.
\lib contains ExampleLogin.jar, jsf-api.jar, jsf-impl.jar,
jstl.jar, loginExample.jar (by built.xml), servlet-api.jar, and
standard.jar.
\src\ contains:
build.xml.
\java with
ExampleLogin.java file.
and \classes with ExampleLogin.class (by buil.xml)
Compile with ant and browse the application to get:
The log in,
the case of succes:.
or the case of :
failure.
|
|
|