JSF 2.0: Installation, Setup, and Getting Started Started: For Live Training On JSF 1.x or 2.0, Please See THTT// LT
JSF 2.0: Installation, Setup, and Getting Started Started: For Live Training On JSF 1.x or 2.0, Please See THTT// LT
Overview
Installing
Java and Tomcat
For even more detailed step-by-step instructions, see tutorials on using Eclipse with
Tomcat 6 or Tomcat 7 at http://www.coreservlets.com/Apache-Tomcat-Tutorial/
Installing Java SE 6
• Minimum Java version
– Tomcat 7 (servlets 3.0) requires Java 6
– Tomcat 6 and other servlet 2.5 containers require Java 5+
• But
B JJava 6 recommended
d d ffor performance
f and
d ffeatures
• Downloading and installation
–F
Follow
ll directions
di ti att
http://java.sun.com/javase/downloads/
• Get basic Java SE, choose “JDK”, not “JRE”
– Not “with Java EE”, “with JavaFX”, or “with NetBeans”
12
Installing Java SE 6
• Install Java 6
– http://java.sun.com/javase/downloads/index.jsp
I t lli Eclipse
Installing E li
For even more detailed step-by-step instructions, see tutorials on using Eclipse with
Tomcat 6 or Tomcat 7 at http://www.coreservlets.com/Apache-Tomcat-Tutorial/
Installing Eclipse
• Overview
– Eclipse is a a free open source
IDE for Java. Support for Java,
HTML, CSS, JavaScript, C++,
PHP,, and more.
• http://eclipse.org/downloads/
• Choose “Eclipse IDE for Java EE Developers”
– Need version 3.6 (Helios) for JSF 2.0 and Tomcat 7 support
• Features
– Checks your syntax as you type
– Automatically compiles every
time you
o save
sa e file
– Many tools: refactoring,
debugging, server integration,
templates for common tasks,
tasks etc.
etc
• Low learning curve: beginners can use Eclipse without
knowing these tools
Reminder: step-by-step guide at http://www.coreservlets.com/ (click “Apache Tomcat 7” in top left).
Running Eclipse
• Unzip the downloaded file
– Call the folder you unzip into “installDir”
• Double click eclipse.exe
– From
F i
installDir/bin
llDi /bi
• Click on
Workbench icon
“Workbench”
– Next time you bring
up Eclipse, it will
come up in workbench automatically
• Shortcut
– Many
M ddevelopers
l putt Eclipse
E li link
li k on their
th i desktop
d kt
• R-click eclipse.exe, Copy, then go to desktop, R-click, and
Paste Shortcut (not just Paste!)
Configuring Eclipse
• Tell Eclipse about Java version
– Window Preferences Java
Installed JREs Press “Add”, choose
“Standard VM”, navigate to JDK folder
((not “bin” subdirectory)
y)
• E.g., C:\Program Files\Java\jdk1.6.0_21
• Tell Eclipse about Tomcat
– Click on Servers tab at bottom.
R-click in window.
– New, Server, Apache, Tomcat v7.0,
Next, navigate to folder, Finish.
• JSF
S 2.0 support
– Eclipse 3.6 has support for JSF 2.
• R-click and add Project Facet for JSF 2
• R-click .xhtml files and
Open With, Web Page Editor Tomcat v7.0 is choice only in Eclipse 3.6 (Helios). If you
prefer Tomcat 6, choose Tomcat v6.0 above instead. If
• Double-click faces-config.xml you lose the “Servers” tab at the bottom of Eclipse, use
Window, Show View, and hunt for “Servers”.
18
© 2010 Marty Hall
Deploying Apps
from Eclipse
20
Deploying App in Eclipse
• Deploy project
– Select “Servers” tab at bottom
– R-click on Tomcat
– Choose “Add
Add and Remove”
Remove
– Choose project
– Press “Add”
– Click “Finish”
• Start Server
–RR-click
click Tomcat at bottom
– Start (use “Restart” if
Tomcat already running)
• Test URL
– http://localhost/test-app/ in any Web browser
21
• Start a browser
– Eclipse also has builtin browser,
but I prefer to use Firefox,
IE, or Chrome separately
• Test base URL
– http://localhost/test-app/
• Test
T t Web
W b content
t t
– http://localhost/test-app/hello.html
– http://localhost/test-app/hello.jsp
http://localhost/test app/hello.jsp
• Test servlets
– http://localhost/test-app/hello
– http://localhost/test-app/test1
– http://localhost/test-app/test2
22
© 2010 Marty Hall
24
Making a JSF 2.0 Eclipse
Project: Alternatives
• Copy/rename jsf-blank
– Eclipse
E li project
j with
i h all
ll required
i d pieces
i already
l d included
i l d d
• Also has the Eclipse 3.6 JSF 2 facet already added
– Use as starting point for your JSF 2.0 projects.
• Or, build project from scratch
– JAR files
• Put two required and two recommended JAR files into
WebContent/WEB-INF/lib
– web.xml entries
• Two required and one recommended settings
– WEB-INF/faces-config.xml
• Body can be empty, but legal file is required
• Details on both approaches
– Given later in this tutorial
25
Installing Glassfish 3
• Download
– Start at https://glassfish.dev.java.net/, follow link to
“Downloads”.
– Choose the latest released 3.x
3 x version
• There are both completely open source and commercially-
supported versions. The completely open source version
is sufficient for everything in JSF 2
2.0
0
• Install
– Run installer
• I installed in C:\glassfishv3
• I chose anonymous admin login and changed HTTP port
from 8080 to 80
28
Install Java 6 and Eclipse
• Java 6 required
– Java EE 6 will not work with JDK 1.5
• Eclipse or another IDE strongly
recommended
– I use Eclipse 3.6 (Java EE Edition) in this tutorial
• Details
– See slides in previous section
29
31
Big Idea
• Start with a pre-made Eclipse project
– JAR fil
files:
• jsf-api.jar, jsf-impl.jar, jstl-1.2-api.jar, jstl-1.2-impl.jar
• You can delete these if you use a Java EE 6 server
– web.xml
eb ml
• servlet, servlet-mapping for FacesServlet
• PROJECT_STAGE set to Development
– faces-config.xml
faces config xml
• Legal start/end tags, empty body
– Super-simple test pages
• Making
M ki your own project
j t
– Copy/rename jsf-blank
• But due to an Eclipse bug, you must also manually edit an
E li
Eclipse fil
file on the
th fil
file system
t
– Or, make your own Dynamic Web project and copy top three
34
pieces above to the new project.
Importing the jsf-blank Project
• Grab jsf-blank.zip from tutorial site
– http://www.coreservlets.com/JSF-Tutorial/jsf2/
• Import into Eclipse
–SStart E
Eclipse
li andd go to Workbench
W kb h
– Use File Import General Existing Projects into
Wo sp ce Next
Workspace e Se Select
ec archive
c ve filee
– Then click Browse, navigate to jsf-blank.zip, and
continue
– You
Y should
h ld now see jsf-blank
j f bl k in
i project
j listli at left
l f
– Can run as is on Tomcat 6, Tomcat 7, or Glassfish 3
• Test locally (next page), or build WAR file in normal
manner to send to deployment server (R-click project,
Export WAR file).
35
38
Copying jsf-blank: Example
(Continued)
• .component file: before
<?xml version="1
version= 1.0
0" encoding=
encoding="UTF-8"?>
UTF 8 ?>
<project-modules id="moduleCoreId" project-version="1.5.0">
<wb-module deploy-name="jsf-blank">
<wb-resource deploy-path="/" source-path="/WebContent"/>
<wb-resource
<wb resource deploy-path="/WEB-INF/classes"
deploy path /WEB INF/classes source
source-path="/src"/>
path /src />
<property name="context-root" value="jsf-blank"/>
<property name="java-output-path"
value="/jsf-blank/build/classes"/>
</wb-module>
</project-modules>
• .component file: after
<?xml version="1.0" encoding="UTF-8"?>
<project-modules
<project modules id="moduleCoreId"
id= moduleCoreId project
project-version="1
version= 1.5.0
5 0">
>
<wb-module deploy-name="my-jsf-project">
<wb-resource deploy-path="/" source-path="/WebContent"/>
<wb-resource deploy-path="/WEB-INF/classes" source-path="/src"/>
<property
p p y name="context-root" value="my-jsf-project"/>
y j p j /
<property name="java-output-path"
value="/my-jsf-project/build/classes"/>
</wb-module>
39 </project-modules>
Project Layout
– Download files and drag/drop into proper locations
• src/coreservlets
– HealthPlanBean.java
(R-click on src and make package first)
• WebContent
– All .xhtml files, index.jsp
• WebContent/WEB-INF
– web.xml and faces-config.xml
• WebContent/css
– styles
styles.css
css
(R-click WebContent to
make folder)
• WebContent/WEB-INF/lib
– The four .jar files
44
Overview of Code
• Quick summary given here
– Very simple app
• Form data ignored
• Simplest possible action controller
• Simplest possible results pages
• Technical details in later sections
– More details on each construct
– More types of apps
• Reminder
– Individual files and complete project can be downloaded
from tutorial home page
p g
• http://www.coreservlets.com/JSF-Tutorial/jsf2/
45
JAR Files
O more simply,
Or, i l grabb jjsf-blank
f bl k from
f coreservlets.com
l t andd copy from
f there.
th
If you run on Tomcat 6 or another server that supports servlets 2.5 but not Java
EE 6, the first two JARs are always needed, and the second two JARs are
needed if you use any of the ui:repeat tags. If you run on Glassfish 3 or another
server that supports Java EE 6,
6 you can delete all four of these JARs.
JARs
46
web.xml
<?xml version="1.0" encoding="UTF-8"?>
pp … version="2.5">
<web-app Must be version 2.5 or later. Glassfish supports
pp servlets
version 3.0. This is an updated requirement from JSF 1.x.
<servlet>
<servlet-name>Faces Servlet</servlet-name>
<servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
</servlet>
/ Leave unchanged. This is the
same as in JSF 1.x.
<servlet-mapping>
<servlet-name>Faces Servlet</servlet-name>
u patte .js /u patte
<url-pattern>*.jsf</url-pattern> U JSF for
Use f URLs
URL that
th t endd in
i blah.jsf.
bl h j f Other
Oth
</servlet-mapping> popular options are .faces and /faces/*.
This is the same as in JSF 1.x.
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
/
</context-param>
<welcome-file-list> Give more and more detailed error
messages. For example, unknown
<welcome-file>index.jsp</welcome-file>
<welcome file>index.jsp</welcome file> outcomes are flagged this way (vs. (vs
<welcome-file>index.html</welcome-file> silently redisplaying input form when
in deployment mode). Optional.
</welcome-file-list> This is new in JSF 2.0.
47
</web-app>
faces-config.xml
<?xml version="1.0"?>
<faces-config
<faces config xmlns="http://java.sun.com/xml/ns/javaee"
xmlns http://java.sun.com/xml/ns/javaee
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-facesconfig_2_0.xsd"
_ _
version="2.0">
48
health-plan-signup-1.xhtml
<!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"
Same header as with facelets in
xmlns:h="http://java.sun.com/jsf/html"> JSF 1.x. But in JSF 2.0, facelets,
… not JSP, is the standard way of
<h:body>
:body It is not necessaryy to use h:bodyy and h:head in this example
p making JSF pages. Note that file is
(regular <body> and <head> are fine). However, when you blah.xhtml, but URL is blah.jsf
… use h:outputScript and especially f:ajax, you need those (assuming url-pattern of *.jsf in
tags. So, you might as well plan ahead and use them
<h:form> routinely.
web.xml).
<fieldset>
<legend>Health Insurance Plan Signup</legend>
First name: <h:inputText/><br/> The input elements are ignored in this simplistic
example. The next tutorial section will give ‘value’
Last name: <h:inputText/><br/> attributes corresponding to bean properties.
SSN: <h:inputText/><br/>
Complete medical history since the day you were born:<br/>
<h:inputTextarea/><br/>
<h:commandButton value="Sign Me Up!"
action="#{healthPlanBean.signup}"/>
</fieldset>
/
</h:form> Same format as in JSF 1.x. But name of bean is
… automatically derived from Java class name.
49 </h:body></html>
HealthPlanBean.java
package coreservlets;
@ManagedBean Since no name given, name is the class name with the first
letter changed to lower case (i.e., healthPlanBean).
public class HealthPlanBean {
public String signup() { Since no scope given, it is request scope.
50
accepted.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
p // g/ / / /
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"> I don’t actually have any dynamic
code in this simplistic example, but
<h:head> it is a good idea to plan ahead and
… always
l include
i l d ththese.
</h:head>
<h:body>
…
<table border="5">
<tr><th class="title">Accepted (Version 1)</th></tr>
</table>
<p/>
51
rejected.xhtml
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
p // g/ / / /
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"
Again, this part plus h:head and
xmlns:h="http://java.sun.com/jsf/html"> h:body are not strictly necessary in
<h:head> this simple example, but it is
recommended practice to include
… them routinely.
</h:head>
<h:body>
…
<table border="5">
<tr><th class="title">Rejected (Version 1)</th></tr>
</table>
<p/>
52
index.jsp
<% response
response.sendRedirect(
sendRedirect("health-plan-signup-1
health plan signup 1.jsf
jsf");
); %>
53
Results
54
© 2010 Marty Hall
Wrap-Up
Summary
• Setup
– Install Java 6 and Eclipse
– Install any recent Tomcat version (6 or 7) or Glassfish 3
– Test by downloading and deploying jsf-blank.zip
jsf blank zip
• Try your own JSF 2.0 project
– Copy/rename jsf
jsf-blank
blank
• Due to Eclipse bug, you must then edit
workspace/projectName/.settings/…component and
change all occurrences of “jsf-blank”
jsf blank to new name
– Or, make a new Dynamic Web Project and copy three
required pieces from jsf-blank
• Four
F JAR files
fil iin WEB
WEB-INF/lib
INF/lib
• WEB-INF/web.xml
56 • WEB-INF/faces-config.xml
© 2010 Marty Hall
Questions?