Upload
robertok30
View
16
Download
0
Embed Size (px)
Citation preview
2014 Marty Hall
Customized Java EE Training: http://courses.coreservlets.com/Java 7, Java 8, JSF 2.2, PrimeFaces, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
JSF: Installation, Setup, & Getting Started
JSF 2.2 Version
Originals of Slides and Source Code for Examples:http://www.coreservlets.com/JSF-Tutorial/jsf2/
2014 Marty Hall
Customized Java EE Training: http://courses.coreservlets.com/Java 7, Java 8, JSF 2.2, PrimeFaces, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
For live training on JSF 2 and/or PrimeFaces, see http://courses.coreservlets.com/
or email [email protected] by the author of Core Servlets and JSP, this tutorial,
and JSF 2.2 version of Core JSF. Available at public venues, or customized versions can be held on-site at your organization.
Courses developed and taught by Marty Hall JSF 2.2, PrimeFaces, servlets/JSP, Ajax, jQuery, Android development, Java 7 or 8 programming, custom mix of topics Courses available in any state or country. Maryland/DC area companies can also choose afternoon/evening courses.
Courses developed and taught by coreservlets.com experts (edited by Marty) Hadoop, Spring, Hibernate/JPA, GWT, RESTful Web Services
Contact [email protected] for details
Topics in This Section
Getting required software Installing Java SE Installing Eclipse (Java EE version) Installing a server for JSF 2
Accessing documentation Testing projects
Importing and testing an existing JSF 2 project Deploying on servlet engine (e.g., Tomcat, Jetty) and on
Java EE 7 server (e.g., Glassfish 4, Jboss 7) Making your own JSF 2.2 project
Using Eclipse wizard By copying and renaming template project
5
2014 Marty Hall
Customized Java EE Training: http://courses.coreservlets.com/Java 7, Java 8, JSF 2.2, PrimeFaces, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Overview
Overview of JSF 2.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 alternative to many faces-config.xml entries Integrated Ajax support Facelets instead of JSP Much simpler custom components Ability to bookmark results pages (view parameters) Lots more
JSF 2.2 adds even more vs. JSF 2.1 Flow scope Stateless views HTML 5 pass-through attributes View actions
7
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 Kepler, which has explicit JSF 2.2 support. NetBeans and IntelliJ IDEA also have good JSF 2 support.
8
Software Needed: Summary (Details in Later Sections)
To run on Tomcat Install Java
Java 7 or 8 Install an IDE
I use Eclipse Kepler Download Tomcat 7
Or any server supporting servlets 3.0
Get JSF 2.2 JAR files Download from Oracle
Mojarra or Apache MyFaces
web.xml, faces-config.xml Required entries shown
later in tutorial
To run on Java EE 7 Install Java
Java 7 Install an IDE
I use Eclipse Kepler Download Glassfish 4
Or any server supporting Java EE 7
No extra JAR files needed Java EE 7 has built-in
support for JSF 2.2 web.xml, faces-config.xml
Required entries shown later in tutorial
9
Fast Start for Experts
If you already use Tomcat and Eclipse Grab jsf-blank.zip from online link
http://www.coreservlets.com/JSF-Tutorial/jsf2/ Import into Eclipse. Deploy to Tomcat
Eclipse Kepler or later recommended Run http://localhost/jsf-blank/ Build JSF 2.2 apps using the Eclipse wizard. Or, use
jsf-blank as starting point. App has: The JSF JAR file in WEB-INF/lib The needed entries in WEB-INF/web.xml A JSF 2.2 compliant WEB-INF/faces-config.xml file
Skip the rest of this tutorial And move on to sections on specific JSF 2 features.
10
2014 Marty Hall
Customized Java EE Training: http://courses.coreservlets.com/Java 7, Java 8, JSF 2.2, PrimeFaces, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Installing Java and Tomcat
11
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
Minimum Java version Technically, Java 6 legal, but Java 7 better
Java 8 is great alternative if using Tomcat or Jetty instead of a full Java EE 7 sever
Downloading and installation Follow directions at Oracle site
http://www.oracle.com/technetwork/java/javase/downloads/ Choose JDK, not JRE or Server JRE Install and accept all defaults
Bookmark the Java API (JavaDocs) http://docs.oracle.com/javase/7/docs/api/ or http://download.java.net/jdk8/docs/api/
12
Installing Java SE
Install Java SE 7 http://www.oracle.com/technetwork/java/javase/downloads/
Java 8 available March 2014, and is huge improvement. See Java 8 tutorial at http://www.coreservlets.com/java-8-tutorial/
Use this version. The JDK Java Development Kit includes compiler for .java files, whereas the JRE Java Runtime Environment is only for executing prebuilt .class files.
This tutorial uses Eclipse, but if you prefer the NetBeans IDE, it is very easy to adapt the instructions to that development environment. So, if you prefer NetBeans or your organization has standardized on it, use this download instead of (not in addition to) the one on the left.
Download and Unzip Tomcat
Start at http://tomcat.apache.org Choose download link on left, then ZIP version
Tomcat 7 or 8 (recommended) Tomcat 6 (if you need compatibility with older servers, but
will not support JSF 2.2 file upload component) Or, go to http://www.coreservlets.com/
Choose Tomcat tutorial from top left This is preconfigured version
Set for development, not deployment mode Port changed to 80, servlet reloading enabled, directory listings
turned on, etc. Otherwise unchanged
Either way, just unzip the file E.g., resulting in C:\apache-tomcat-7.0.3414
2014 Marty Hall
Customized Java EE Training: http://courses.coreservlets.com/Java 7, Java 8, JSF 2.2, PrimeFaces, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Installing Eclipse
15
For even more detailed step-by-step instructions, see tutorials on using Eclipse with Tomcat at http://www.coreservlets.com/Apache-Tomcat-Tutorial/
Installing Eclipse Overview
Eclipse is a free open source IDE. Support for Java, Android,HTML, CSS, JavaScript, C++, PHP, JSF, servlets, and more.
http://eclipse.org/downloads/ Choose Eclipse IDE for Java EE Developers
I use latest (Kepler). Older Helios supports JSF 2, but not 2.2 explicitly
Features Checks your syntax as you type Automatically compiles every
time you save file Many tools: refactoring,
debugging, server integration, templates for common tasks, 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 (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 Kepler has support for JSF 2.2 Autocompletion in .xhtml and .java Visual editor for faces-config.xml Visual previews when editing .xhtml files
18
Eclipse Kepler (latest as of 1/2014) is best choice. Earlier versions have JSF 2 support, but not explicit support for 2.2.
If you lose the Servers tab at the bottom of Eclipse, use Window menu, Show View, and hunt for Servers.
2014 Marty Hall
Customized Java EE Training: http://courses.coreservlets.com/Java 7, Java 8, JSF 2.2, PrimeFaces, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Deploying Apps from Eclipse
19
Download and Import Sample Project
Get jsf-blank.zip from coreservlets.com Start at JSF 2 tutorial
Go to http://www.coreservlets.com/JSF-Tutorial/jsf2/ or just go to http://www.coreservlets.com/ and click on JSF 2 in tutorials section in upper left
Expand Installation, Setup, and Getting Started section and then click on jsf-blank.zip
Then, import into Eclipse After downloading zip file:
File, Import, General, Existing Projects, Select archive file. Then click Browse and navigate to jsf-blank.zip.
20
Deploying App in Eclipse
Deploy project Select Servers tab at bottom R-click on Tomcat Choose Add and Remove Choose project Press Add Click Finish
Start Server R-click Tomcat at bottom Start (use Restart if
Tomcat already running) Test URL
http://localhost/test-app/ in any Web browser21
Testing Deployed App in Eclipse
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 anda 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.
22
2014 Marty Hall
Customized Java EE Training: http://courses.coreservlets.com/Java 7, Java 8, JSF 2.2, PrimeFaces, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Installing JSF 2.2
Main JSF 2.2 Implementations
Oracle Mojarra Main page: https://javaserverfaces.java.net/ Runs in any server supporting servlets 3.0 or later
Or servlets 2.5 if you do not use file upload component Also integrated into Glassfish 4
Apache MyFaces Main page: http://myfaces.apache.org/core22/ Runs in any server supporting servlets 3.0 or later
Or servlets 2.5 if you do not use file upload component Any Java EE 7 server
JSF 2.7 is an official part of Java EE 7 JBoss 8, Glassfish 4 available now (1/2014) WebLogic, WebSphere 8, etc. coming soon.
24
Making a JSF 2 Eclipse Project: Alternatives
Use Eclipse wizard to build project. Needed: JAR file
Put JSF 2.2 JAR into WebContent/WEB-INF/lib web.xml entries
Two required and one recommended settings TODO WEB-INF/faces-config.xml
Not required, but a blank one is good idea for later use JSF 2.2 project facet
Not needed for execution, but useful for development. Tells Eclipse what editors to use for .xhtml files, faces-config, etc.
Copy/rename jsf-blank project Eclipse project with all required pieces already included
Also has the Eclipse JSF 2 facet already added Use as starting point for your JSF 2 projects
Details on both approaches Given later in this tutorial
25
Downloading JSF 2.2 JAR Files
Oracle Mojarra https://javaserverfaces.java.net/nonav/2.2/download.html
Download a single JAR file (javax.faces-2.2.x.jar). Put it in WEB-INF/lib or register it with the Eclipse wizard that will add it for you later.
Note that this JAR file is already inside the jsf-blank sample project, so you can also copy it from there
Apache MyFaces http://myfaces.apache.org/download.html
Download ZIP file (myfaces-core-assembly-2.2.x-bin.zip), then grab JAR file (myfaces-bundle-2.2.x.jar) from the lib folder inside it.
Java EE 7 servers No extra JAR files needed. JSF 2.2 built in.
26
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://download.java.net/jdk8/docs/api/
27
2014 Marty Hall
Customized Java EE Training: http://courses.coreservlets.com/Java 7, Java 8, JSF 2.2, PrimeFaces, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Making JSF 2.2 Project with Eclipse Wizard
Options for Making JSF Project
Use Eclipse wizard Advantage
Moderately easy (after initial setup) Disadvantages
Makes non-standard web.xml Does not copy existing Java, HTML, CSS code
Copy and rename jsf-blank sample project Advantage
Copies Java, HTML, CSS code So better if you have existing project with lots of code
Makes standard web.xml Disadvantage
Requires tedious process to rename .component file due to Eclipse (not JSF!) bug in renaming projects29
One Time Only: Register JSF 2.2 JAR
Create JSF 2.2 user library Window Preferences
Java Build Path User Libraries New
Select new library and point at JSF 2.2 JAR file Click on your new library Click Add External JARs Point to JAR file from
wherever you saved it fromprevious section
30
Each Time: Make Dynamic Web Project with JSF 2.2
Create project File New Project Web Dynamic Web Project
Next time, you can do File New Dynamic Web Project
Specify JSF 2.2 for configuration You can also create
vanilla Dynamic WebProject, then add JSF2.2 later by R-clickingproject, selecting Properties,and going to Project Facets
31
Make Dynamic Web Project with JSF 2.2 (Continued)
Use defaults for src location and web.xml (1st and 2nd screens after last slide)
For JSF Capabilities (3rd screen) Add your new JSF 2.2 user library Remove *.faces as the URL pattern
Add *.jsf as the URL pattern
32
Make Dynamic Web Project with JSF 2.2 (Continued)
Clean up auto-generated web.xml The one made by Eclipse has some spurious entries
(localizationContext and ConfigureListener), lacks the very valuable PROJECT_STAGE setting, and fails to set any welcome-page URLs
Solution: copy web.xml from jsf-blank In principle, you could also edit by hand, but this is
tedious and error prone. Format of web.xml entries is discussed in Programming
Basics tutorial section You can download the more-standard web.xml from
Getting Started section of JSF 2.2 tutorial at coreservlets.com, but easier to download all of jsf-blank, then copy from there.
33
web.xml: Required Entries
Faces Servletjavax.faces.webapp.FacesServlet
Faces Servlet*.jsf
...
34
Behind-the-scenes servlet that runs JSF
This means that all URLs ending in .jsfwill be handled by JSF. There are other options besides *.jsf, but *.jsf is the most common.
Reminder: do not type this in by hand. Copy from jsf-blank instead.
web.xml: Optional-but-Recommended Entries
...
javax.faces.PROJECT_STAGEDevelopment
javax.faces.STATE_SAVING_METHODclient
index.jsfwelcome.jsfindex.htmlindex.jsp
35
Means that you are in the development (not deployment) phase, so JSF should give extra error messages.
If you go to http://host/project/ (with no file name), it will try index.jsf first, welcome.jsfnext, and so forth.
Reminder: do not type this in by hand. Copy from jsf-blank instead.
How the Java objects representing the form elements are stored. This is debatable, but client is slightly preferred. JSF 2.2 also has stateless views, where this entry is irrelevant.
2014 Marty Hall
Customized Java EE Training: http://courses.coreservlets.com/Java 7, Java 8, JSF 2.2, PrimeFaces, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Making JSF 2.2 Project by Copying jsf-blank
Options for Making JSF Project
Use Eclipse wizard Advantage
Moderately easy (after initial setup) Disadvantages
Makes non-standard web.xml Does not copy existing Java, HTML, CSS code
Copy and rename jsf-blank sample project Advantage
Copies Java, HTML, CSS code So better if you have existing project with lots of code
Makes standard web.xml Disadvantage
Requires tedious process to rename .component file due to Eclipse (not JSF!) bug in renaming projects37
Big Idea
Start with a pre-made Eclipse project JAR file
javax.faces-2.2.x.jar (not needed if using Java EE 7) web.xml
All entries from previous section faces-config.xml
Legal start/end tags, empty body samples folder with templates for .xhtml files
Copy these to WebContent, rename them to fit your app, and edit for your own code
Sample CSS style sheet Copy or use as a reference if you are rusty on CSS
Super-simple test code You will remove this for your own apps
38
Importing the jsf-blank Project
Grab jsf-blank.zip from tutorial site http://www.coreservlets.com/JSF-Tutorial/jsf2/
Import into Eclipse Start Eclipse and go to Workbench Use File Import General Existing Projects into
Workspace Next Select archive file Then click Browse, navigate to jsf-blank.zip, and
continue You should now see jsf-blank in project list at left 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).
39
Testing the jsf-blank Project
Deploy to server Click on Servers tab at bottom. R-click on Tomcat v7.0 Choose Add and Remove and select jsf-blank. R-click Tomcat again and choose Start.
Test Start browser and enter URL
http://localhost/jsf-blank/ Try the pushbutton
40
Making Your Own JSF 2 Project by Copying jsf-blanl
Copy the jsf-blank project R-click on jsf-blank at left. Copy. R-click again. Paste.
Use same process later if you have a JSF project you want to copy, and you want all the code to go with it.
Problem: Eclipse bug Eclipse leaves references to old name in new project.
One can be found by R-clicking project, then Properties Web Project Settings. But the other has to be changed manually, so you might as well replace all of them manually.
Solution Go to file system, edit eclipse-workspace/projName/
.settings/org.eclipse.wst.common.component You could also use Eclipse Navigator (not Proj Explorer)
Change all instances of old project name to new one R-click on project and choose Refresh Close the Navigator when done. Do not forget this step!
41
Copying jsf-blank: Example
First, copy project R-click on jsf-blank, choose Copy R-click in Project Explorer window, choose Paste
E.g., name it my-jsf-project Next, edit .component file
Navigate to Eclipse workspace/projectName/.settings
42
Open in normal text editor or Eclipse Navigator(not Eclipse Project Explorer)
If you edit on file system, when done editing, R-click on project in Eclipse, then choose Refresh. If you edit withEclipse Navigator, close Navigator window when finished.
This issue is already in the Eclipse known bugs list, so hopefully it will be fixed soon. But as of Jan. 2014, the bug still exists in all Eclipse versions, including Kepler.
Copying jsf-blank: Example (Continued)
.component file: before
.component file: after
43
2014 Marty Hall
Customized Java EE Training: http://courses.coreservlets.com/Java 7, Java 8, JSF 2.2, PrimeFaces, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Wrap-Up
Summary
Setup Install Java 7 (or 8) and Eclipse Kepler Install recent Tomcat version (7) or Java EE 7 server Test by downloading and deploying jsf-blank.zip
It also includes the JSF 2.2 JAR file
Try your own JSF 2 project Use Eclipse wizard to make JSF 2.2 project
But overwrite web.xml when done 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!45
2014 Marty Hall
Customized Java EE Training: http://courses.coreservlets.com/Java 7, Java 8, JSF 2.2, PrimeFaces, JSP, Ajax, jQuery, Spring, Hibernate, RESTful Web Services, Hadoop, Android.
Developed and taught by well-known author and developer. At public venues or onsite at your location.
Questions?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.
Also see JSF 2 tutorial and PrimeFaces tutorial.