JSF2 Getting Started
JSF2 Getting Started
JSF 2.0: Installation, Setup, and Getting Started for Examples: Originals of Slides and Source Code
http://www.coreservlets.com/JSF Tutorial/jsf2/ http://www.coreservlets.com/JSF-Tutorial/jsf2/
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
For live training on JSF 2.x, please see courses at htt // t http://courses.coreservlets.com/. l t /
Taught by the author of Core Servlets and JSP, More Servlets and JSP and this tutorial. Available at public JSP, tutorial venues, or customized versions can be held on-site at your organization.
C Courses d developed and t l d d taught b M t H ll ht by Marty Hall Courses developed and taught by coreservlets.com experts (edited by Marty)
JSF, servlets/JSP, Ajax, jQuery, Android development, Java 6 programming, custom mix of topics Ajax courses can concentrate on 1EE Training: http://courses.coreservlets.com/several Customized Java library (jQuery, Prototype/Scriptaculous, Ext-JS, Dojo, etc.) or survey
Servlets, JSP, Hibernate/JPA, EJB3, GWT, jQuery, GWT, RESTful Web Services RESTful Web Services, Android. Spring, JSF 2.0, Java 6, Ajax, SOAP-based and Spring, Hibernate, Contact [email protected] for details Developed and taught by well-known author and developer. At public venues or onsite at your location.
Testing projects
Importing and testing an existing JSF 2.0 project
Deplo ing on Tomcat and Glassfish Deploying
Overview
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Rectifying this is the main point of this section Later sections give detailed tutorial on JSF 2.0 features
A server
Servers that just support servlets 2.5 or later (e.g., Tomcat 6 or ) (jsf-api.jar j p j ) 7) need two JAR files (j p j and jsf-impl.jar)
In addition, JSTL 1.2 JAR files needed if you use ui:repeat tag JSF 2.0 also runs on the Google cloud server (which uses Jetty)
Servers that support Java EE 6 (e.g., Glassfish 3, JBoss 6, WebLogic 11g) have built-in support for JSF 2.0 & JSTL 1.2
All tutorial examples run on Tomcat 6, Tomcat 7, & Glassfish 3
An IDE
Optional, but highly recommended.
This tutorial uses Eclipse 3.6, which has explicit JSF 2 support.
8
To run on Glassfish
Install Java
Java 6 or later
Install an IDE
I use Eclipse 3 6 E li 3.6
Install an IDE
I use Eclipse 3.6
Download Tomcat 6 or 7
Or any server supporting servlets 2.5 25
Download Glassfish 3
Or any server supporting Java EE 6
web.xml, faces-config.xml
Required entries shown later in tutorial
web.xml, faces-config.xml
9
Run http://localhost/jsf-blank/ Use jsf-blank as starting point for your own JSF 2.0 apps. App already has:
The two needed JAR files in WEB-INF/lib
Also the two optional but recommended JSTL 1.2 JAR files
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
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+
B J But Java 6 recommended f performance and f d d for f d features
12
Installing Java SE 6
Install Java 6
http://java.sun.com/javase/downloads/index.jsp
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 environment, 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.
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 80 enabled, turned on, etc.
Otherwise unchanged
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Installing Eclipse
Overview
Eclipse is a a free open source IDE for Java. Support for Java, HTML, CSS, JavaScript, C++, PHP, and more. ,
Features
Checks your syntax as you type Automatically compiles every time you save file o sa e Many tools: refactoring, debugging, server integration, templates for common tasks, etc. 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
Call the folder you unzip into installDir
Shortcut
M Many d l developers put Eclipse link on their desktop t E li li k th i 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) Click on Servers tab at bottom. R-click in window. New, Server, Apache, Tomcat v7.0, Next, navigate to folder, Finish. 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 Double-click faces-config.xml
Tomcat v7.0 is choice only in Eclipse 3.6 (Helios). If you prefer Tomcat 6, choose Tomcat v6.0 above instead. If you lose the Servers tab at the bottom of Eclipse, use Window, Show View, and hunt for Servers.
18
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
20
Start Server
R click Tomcat at bottom R-click Start (use Restart if Tomcat already running)
Test URL
21
Test servlets
http://localhost/test-app/hello http://localhost/test-app/test1 http://localhost/test-app/test2
22
Apache MyFaces
Main page: http://myfaces.apache.org/core20/ http://myfaces apache org/core20/ Runs in any server supporting servlets 2.5 or later Also integrated into Apache Geronimo 3
web.xml entries
Two required and one recommended settings
WEB-INF/faces-config.xml
Body can be empty, but legal start/end tags required
If you download the jsfblank Eclipse project, you can skip this entire slide, since the jsf-blank project already includes all of the required pieces.
Installing Glassfish 3
Download
Start at https://glassfish.dev.java.net/, follow link to Downloads. Choose the latest released 3.x version 3x
There are both completely open source and commerciallysupported versions. The completely open source version is sufficient for everything in JSF 2 0 2.0
Install
Run installer
I installed in C:\glassfishv3 I chose anonymous admin login and changed HTTP port from 8080 to 80
28
Details
See slides in previous section
29
Specify folder
Choose glassfish subfolder in glassfish location where you installed Glassfish. For example, I installed in C:\glassfishv3, so I C:\glassfishv3 navigate to C:\glassfishv3\glassfish
31
Main difference
Remove JAR files from WEB-INF/lib before deploying
All of the sample projects at coreservlets.com have the JSF 2.0 and JSTL 1 2 JAR files in WebContent/WEB 20 1.2 WebContent/WEBINF/lib. Delete them before deploying to Java EE 6 server.
Java EE 6 already supports JSF 2.0 and JSTL 1.2, so it is illegal t ill l to supply those JAR files. l th fil
32
Glassfish 3 ignores them, but even with Glassfish 3, it is better to delete them.
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
Or, make your own Dynamic Web project and copy top three pieces above to the new project.
Glassfish
Click on Servers tab at bottom R-click on Glassfish v3 bottom. Java EE 6, choose Add and Remove . Choose jsfblank. R-click Glassfish again and choose Start.
Test
Start browser and enter URL
http://localhost/jsf-blank/ p j
It should redirect to http://localhost/jsf-blank/page-a.jsf
36
Solution
Go to file system, edit eclipse workspace/projName/ 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 g p j R-click on project and choose Refresh Close the Navigator when done
37
Open in normal text editor or Eclipse Navigator (not Eclipse Project Explorer)
39
Specify Apache Tomcat v6.0 or v7.0 as Target Runtime Copy files from jsf-blank to same location in new project
Four JAR files in WebContent/ WEB-INF/lib WebContent/WEB INF/web xml WebContent/WEB-INF/web.xml WebContent/WEB-INF/ faces-config.xml
41
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 css styles.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
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
These two can be downloaded from https://javaserverfaces.dev.java.net/download.html These two can be downloaded from https://jstl.dev.java.net/download.html Or, O more simply, grab jjsf-blank from coreservlets.com and copy from there. i l b f bl k f l t d f 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, you can delete all four of these JARs. 6 JARs
46
web.xml
<?xml version="1.0" encoding="UTF-8"?> Must be version 2.5 or later. Glassfish supports servlets pp pp <web-app version="2.5"> 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> Use U JSF for URLs that end in blah.jsf. Other f URL th t d i bl h j f Oth popular options are .faces and /faces/*. </servlet-mapping> 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> Give more and more detailed error <welcome-file-list> messages. For example, unknown <welcome file>index.jsp</welcome file> <welcome-file>index.jsp</welcome-file> outcomes are flagged this way (vs (vs. silently redisplaying input form when <welcome-file>index.html</welcome-file> in deployment mode). Optional. </welcome-file-list> This is new in JSF 2.0. </web-app>
47
faces-config.xml
<?xml version="1.0"?> <faces config xmlns http://java.sun.com/xml/ns/javaee <faces-config 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"> </faces-config>
File is mostly empty in this example. It uses default bean names (derived from the class name), default bean scopes (request), and default results pages (derived from the action controllers return values). But you are still required to have a faces-config.xml file with legal start and end tags tags.
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 making JSF pages. Note that file is It is not necessary to use h:body and h:head in this example y y p <h:body> :body blah.xhtml, but URL is blah.jsf (regular <body> and <head> are fine). However, when you 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 web.xml). <h:form> routinely. <fieldset> <legend>Health Insurance Plan Signup</legend> The input elements are ignored in this simplistic First name: <h:inputText/><br/> 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. </h:body></html>
49
HealthPlanBean.java
package coreservlets; import javax.faces.bean.*; @ManagedBean public class HealthPlanBean { public String signup() { if (Math.random() < 0.2) { return("accepted"); } else { return("rejected"); } } }
Declares this as managed bean, without requiring entry in faces-config.xml. Since no name given, name is the class name with the first letter changed to lower case (i.e., healthPlanBean). Since no scope given, it is request scope.
Since there are no explicit navigation rules in faces-config faces-config, these return values correspond to accepted.xhtml and rejected.xhtml (in same folder as page that has the form).
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 dont actually have any dynamic code in this simplistic example, but <h:head> it is a good idea to plan ahead and always include th l i l d these. </h:head> <h:body> <table border="5"> <tr><th class="title">Accepted (Version 1)</th></tr> </table> <p/> <h2>You are accepted into our health plan.</h2> <p>Congratulations.</p> </h:body></html>
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 this simple example, but it is <h:head> recommended practice to include them routinely. </h:head> <h:body> <table border="5"> <tr><th class="title">Rejected (Version 1)</th></tr> </table> <p/> <h2>You are rejected from our health plan.</h2> <p>Get lost.</p> </h:body></html>
52
index.jsp
<% response sendRedirect("health-plan-signup-1 jsf"); %> response.sendRedirect( health plan signup 1.jsf );
53
Results
54
Wrap-Up
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Summary
Setup
Install Java 6 and Eclipse 3.6 Install recent Tomcat version (6 or 7) or Java EE 6 server Test by downloading and deploying jsf-blank.zip jsf blank zip
Due to Eclipse bug, you must then edit workspace/projectName/.settings/component and change all occurrences of jsf-blank to new name
Questions?
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, JSF 2.0, Java 6, Ajax, jQuery, GWT, Spring, Hibernate, RESTful Web Services, Android. Developed and taught by well-known author and developer. At public venues or onsite at your location.