JSF: Installation, Setup, & Getting Started: For Live Training On JSF 2, Primefaces, or Other
JSF: Installation, Setup, & Getting Started: For Live Training On JSF 2, Primefaces, or Other
Overview
8
What is JSF?
• A set of Web-based GUI controls and handlers?
– JSF provides many prebuilt HTML-oriented GUI controls, along
with code to handle their events on the server.
• A device-independent GUI control framework?
– JSF can be used to generate graphics in formats other than HTML,
using protocols other than HTTP.
• An MVC-based Web app framework?
– Like Apache Struts, JSF can be viewed as an Model-View-
Controller framework for building HTML forms, validating their
values, invoking business logic, and displaying results.
• An Ajax library?
– JSF 2 provides very easy-to-use Ajax support. So, JSF 2 can be
viewed as an alternative to jQuery or GWT.
• But which is the proper way to view JSF?
– The answer depends on what you are going to use it for, but 1 & 3
9
are the most common ways of looking at JSF.
Overview of JSF 2
• JSF is the official Java EE library for Web apps
– And JSF 2 (usually with a rich component library like PrimeFaces
or RichFaces) is the most popular choice in practice.
• JSF 2 adds many new features vs. JSF 1
– Smart defaults
– Annotations as alternatives to most faces-config.xml entries
– Integrated Ajax support
– Facelets instead of JSP
– Much simpler custom components
– Ability to bookmark results pages (view parameters)
• JSF 2.2 adds even more vs. JSF 2.1
– Flow scope
– Stateless views
– HTML 5 pass-through attributes
10
– View actions
Summary: Requirements for
Running JSF 2.2
• Java
– Java 7 or Java 8 strongly preferred, but Java 6 technically legal
– Java EE 7 servers run on top of Java 7, but Java 8 is great option
• This tutorial uses Tomcat 7 and Java 7, but code will run on any Java
server that supports servlets 3.0 or later and uses Java 7 or later. Some
Java 8 examples included, but always with Java 7 alternatives.
• A server
– Servlet engine supporting servlets 3.0 or later (by including JSF JAR file)
• E.g., Tomcat 7, Jetty 9. Servlets 2.5 legal except for file upload component.
– Using JSF on top of servlet engine instead of Java EE 7 server lets you use Java 8,
which is a huge advantage
– Any Java EE 7 server (no JSF JAR needed)
• E.g., Glassfish 4, JBoss 8. WebSphere and WebLogic coming soon
• An IDE
– Optional, but highly recommended
• This tutorial uses Eclipse, which has explicit JSF 2.2 support.
• NetBeans and IntelliJ IDEA also have good JSF 2 support.
11
Installing
Java and Tomcat
For even more detailed step-by-step instructions, see tutorials on using Eclipse with
Tomcat at http://www.coreservlets.com/Apache-Tomcat-Tutorial/
Installing Java SE
• Install Java SE 7 or 8
– http://www.oracle.com/technetwork/java/javase/downloads/
• Java 8 final available 3/2014, and is huge improvement. See
Java 8 tutorial at http://www.coreservlets.com/java-8-tutorial/
Installing Eclipse
For even more detailed step-by-step instructions, see tutorials on using Eclipse with
Tomcat at http://www.coreservlets.com/Apache-Tomcat-Tutorial/
Running Eclipse
• Unzip the downloaded file (no installer!)
– Call the folder you unzip into “installDir”
• Double click eclipse.exe
– From installDir/bin
• Click on
“Workbench” icon
– Next time you bring
up Eclipse, it will
come up in workbench automatically
• Shortcut
– Many developers put Eclipse link on their desktop
• 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)
• E.g., C:\Program Files\Java\jdk1.7.0_51
• 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 2.2 support in Eclipse
– Eclipse Luna and later support JSF 2.2
• Autocompletion in .xhtml and .java
• Visual editor for faces-config.xml Most recent version (Luna as of 1/2015) is best choice. Old
versions have JSF 2 support, but not explicit support for 2.2.
• Visual previews when editing .xhtml files
If you lose the “Servers” tab at the bottom of Eclipse, use
21 Window menu, Show View, and hunt for “Servers”.
Deploying Apps
from Eclipse
23
• Start a browser
– Eclipse also has builtin browser,
but I prefer to use Firefox,
Chrome, or IE separately
• Test base URL
– http://localhost/jsf-blank/
• If you see a form with fields and
a button, then app was deployed properly.
• Test buttons
– The fact that you see textfields and a button means that JSF is
working, since the fields and button were created with JSF-specific
tags. But, you can click on the button to verify that doing so takes
you to another page.
25
JSF Documentation
• JSF 2.2 Docs home page
– https://javaserverfaces.java.net/nonav/docs/2.2/
• JSF 2.2 Tags API
– https://javaserverfaces.java.net/nonav/docs/2.2/vdldocs/facelets/
• This is the reference you probably use the most
• JSF 2.2 Java API
– https://javaserverfaces.java.net/nonav/docs/2.2/javadocs/
• Java 7 API
– http://docs.oracle.com/javase/7/docs/api/
• Java 8 API
– http://docs.oracle.com/javase/8/docs/api/
30
© 2015 Marty Hall
33
35
web.xml: Optional-but-
Recommended Entries
<?xml version="1.0" encoding="UTF-8"?> Means that you are in the development
... (not deployment) phase, so JSF should
give extra error messages.
<context-param>
<param-name>javax.faces.PROJECT_STAGE</param-name>
<param-value>Development</param-value>
</context-param>
<context-param>
<param-name>javax.faces.STATE_SAVING_METHOD</param-name>
<param-value>client</param-value> How the Java objects that represent the
form elements are stored. Best choice is
</context-param> debatable, but client is slightly preferred.
<welcome-file-list> JSF 2.2 also has stateless views, where
this entry is irrelevant.
<welcome-file>index.jsf</welcome-file>
<welcome-file>welcome.jsf</welcome-file>
<welcome-file>index.html</welcome-file>
<welcome-file>index.jsp</welcome-file>
</welcome-file-list>
</web-app> If you go to http://host/project/ (with no file
name), it will try index.jsf first, welcome.jsf
next, and so forth.
43
Wrap-Up
Summary
• Setup
– Install Java 8 (or 7) and Eclipse
– Install recent Tomcat version (7 or 8) or Java EE 7 server
– Test by downloading and deploying jsf-blank.zip
• jsf-blank includes the JSF 2.2 JAR file
• Try your own JSF 2 project
– Use Eclipse wizard to make JSF 2.2 project
• But, when done, overwrite web.xml to get standard setup
– Or, better (?), copy/rename jsf-blank
• Due to Eclipse bug, you must then edit
workspace/projectName/.settings/…component and
change all occurrences of “jsf-blank” to new name
– Close Navigator window when done!
48
© 2015 Marty Hall
Questions?
More info:
http://www.coreservlets.com/JSF-Tutorial/jsf2/ – JSF 2.2 tutorial
http://www.coreservlets.com/JSF-Tutorial/primefaces/ – PrimeFaces tutorial
http://courses.coreservlets.com/jsf-training.html – Customized JSF and PrimeFaces training courses
http://coreservlets.com/ – JSF 2, PrimeFaces, Java 7 or 8, Ajax, jQuery, Hadoop, RESTful Web Services, Android, HTML5, Spring, Hibernate, Servlets, JSP, GWT, and other Java EE training