Professional Documents
Culture Documents
For live training on JSF 1.x or 2.0, 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.
Courses developed and taught by Marty Hall
Customized Java coreservlets.com experts (edited by Marty) Courses developed and taught by EE Training: http://courses.coreservlets.com/
Java 5, Java 6, intermediate/beginning servlets/JSP, advanced servlets/JSP, Struts, JSF, Ajax, GWT, custom courses.
Spring, Hibernate/JPA, EJB3, Ruby/Rails Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at your location. Contact hall@coreservlets.com for details
Background: Beans
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.
Persistent values should be accessed through methods called getXxx and setXxx
If class has method getTitle that returns a String, class is said to have a String property named title Boolean properties use isXxx instead of getXxx
with ith
private double speed; public double getSpeed() { return(speed); } public void setSpeed(double newSpeed) { speed = newSpeed; }
If users of your class accessed the fields directly, then they would each be responsible for checking constraints.
10
If users of your class accessed the fields directly, then they would each be responsible for executing side effects. Too much work and runs huge risk of having display be inconsistent from actual values.
11
To support both, beans that will be session-scoped should implement the java.io.Serializable interface java io Serializable
There are no methods in this interface; it is just a flag:
public class MyBean implements Serializable ... }
Updated Flow
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.
- beans declared in managed-bean section - mapping of return conditions declared in navigation-rule section
Business Logic
results
Instantiate Bean
Choose JSP
forward
14
3) Edit faces-config.xml
A) Declare the bean B) S if navigation rules ) Specify i i l
Example
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.
New features
Action controller obtains request data from within bean Output pages access bean properties
M i points Main i t
Defining a bean with properties for the form data Declaring beans in faces-config.xml g g Outputting bean properties
18
Strings converted to other types as with jsp:setProperty Form redisplayed if there are errors: see validation section
20
Method also invokes business logic, takes results, and g , , stores them in placeholders reserved for output values
21
24
}
26
Example code p
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %> <%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %> <f:view> <h:form> Email address: <h:inputText value="#{registrationBean.email}"/><BR> Password: <h:inputSecret value="#{registrationBean.password}"/><BR> <h:commandButton value="Sign Me Up!" action #{registrationBean.register} /> action="#{registrationBean.register}"/> </h:form> </f:view>
27
Step 2: Result
File is tomcat_dir/webapps/jsf-beans/register.jsp URL i h is http://localhost/jsf-beans/register.faces //l lh /j f b / i f The user@host value comes from the bean
28
30
31
32
33
34
35
36
37
39
40
41
Alternative Approaches
Preview: using the JSP 2.0 EL
If output pages only display bean properties (rather than t t l di l b ti ( th th manipulating a form or using form elements):
Why bother with f:view and associated taglib declaration? Why use h:outputText and associated taglib declaration when the JSP 2.0 EL is simpler?
P Pros of using EL f i
Shorter, simpler, more readable, already familiar
42
44
Looking Ahead
Algorithm for password length was clumsy
Ni to h Nice have b il i checking for textfield lengths builtin h ki f fi ld l h Already supported in JSF
See validation section
Algorithm for checking legal email addresses was primitive and easily fooled
Nice to have builtin checking of valid addresses Supported in MyFaces via Tomahawk extensions
See section on MyFaces extensions
If both password and email were wrong, b th d d il only one was reported
Error pages for bad input results in too many error pages Better to redisplay form and say what was wrong
See section on validation
45
Summary
Create a bean
P Properties for each request parameter ti f h t t Action controller method Placeholders to hold results objects
Questions?
Customized Java EE Training: http://courses.coreservlets.com/
Servlets, JSP, Struts, JSF/MyFaces/Facelets, Ajax, GWT, Spring, Hibernate/JPA, Java 5 & 6. Developed and taught by well-known author and developer. At public venues or onsite at your location.