8/19/2019 JSF2 C06 Page Navigation
1/26
© 2011 Marty Hall
.and faces-config.xml
Originals of Slides and Source Code for Examples:
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.
© 2011 Marty Hall
For live training on JSF 2. x , please seecourses a p: courses.coreserv e s.com .Taught by the author of Core Servlets and JSP , More
, .venues, or customized versions can be held on-site at
your organization.
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.
• ourses eve ope an aug y ar y a – JSF, servlets/JSP, Ajax, jQuery, Android development, Java 6 programming, custom mix of topics
– Ajax courses can concentrate on 1 library (jQuery, Prototype/Scriptaculous, Ext-JS, Dojo, etc.) or survey several
• Courses developed and taught by coreservlets.com experts (edited by Marty) – Spring, Hibernate/JPA, EJB3, GWT, SOAP-based and RESTful Web Services
Contact [email protected] for details
8/19/2019 JSF2 C06 Page Navigation
2/26
Topics in This Section
• Explicit navigation rules
• Explicit bean declarations
• Advanced navigation options – Wildcards in navigation rules
– Conditional navigation rules
– -
• Static navigation
•
5
© 2011 Marty Hall
Explicit Navigation Rules
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.
8/19/2019 JSF2 C06 Page Navigation
3/26
JSF Flow of Control
balance.xhtmlIn the previous section, we used
the default ma in s of return.Uses
and
values, where the string returned
by the action controller method
corresponded to the name of the
results page. Now, we will putexplicit navigation rules in faces-
config.xml that say which page
corresponds to each return value.
submit form
Find return ChooseRun Action forward result1.xhtml
Run Setter
Methods
Business
Logic
results
POST request balance.jsf Bean value PageController Method result2.xhtml
...
resultN .xhtml
Uses
# bankForm.somePro ert
to display bean properties
7
faces-config.xml: Overview
• Location – WEB-INF/faces-config.xml
• Purposes – ve nav gat on ru es
• Map return conditions to results pages
– Declare beans• Map bean names to bean classes
– Inject bean properties
– e ne oca es
– Register validators
–
– Register custom components
– …8
8/19/2019 JSF2 C06 Page Navigation
4/26
faces-config.xml:
• General format
…
• Navigation rules
/some-start-page.xhtml
return-condition-1
/result-page-1.xhtml-
More navigation-case entries for other conditions
9
faces-config.xml: Syntax Details
• General format
…
– JSF 1. x faces-config.xml files are still supported, so youcan run JSF 1. x applications in JSF 2.0 implementationswithout changes.
• However, you must use 2.0 version of faces-config.xml ifou use the new 2.0 features
– Copy a starting-point faces-config.xml file from the jsf- blank application on the Web site
10
8/19/2019 JSF2 C06 Page Navigation
5/26
Navigation Rules:
- … .
/starting-page.xhtml
return-value-1
/result-page-1.xhtml
from-outcome>return-value-2
/result-page-2.xhtml
…Interpretation: “if you start on such-and-such a page, press a button, and
the action controller method returns such-and-such a value, go to page 1.
11
If the action controller returns some other value, go to page 2. And so on.”
Each form has one navigation-rule with a single from-view-id, but each
navigation-rule can have many navigation-cases.
Pros and Cons of Explicit
• Default mappings (return values=pages) – Simpler to start with. Faster to make quick test cases.
– Redundant if return values are 1-to-1 to results pages
• - . – Better understanding of project.
• Lets ou look in one lace for navi ation rules instead of searching many Java files
– There are even tools that automaticallymake charts of page flow based onfaces-config.xml
– More flexible• Can remap conditions to
other pages later
• Can use wildcards for starting page
• Can use wildcards for outcomes12
I find the arguments in favor of explicit
mappings to be strong, and recommend using
them on most real projects.
8/19/2019 JSF2 C06 Page Navigation
6/26
Example
• Overview – Collect a message, then navigate to one of three possible
results pages that display the message. – ,
show an error page
• Implementation – Action controller returns four possible strings: page1,
page2, page3, and too-short
– av ga on ru es n aces-con g.xm map eac o osereturn conditions to results page
13
Example: Starting Page.
=" ". .
xmlns:h="http://java.sun.com/jsf/html">
JSF 2.0: Basic Navigation Rules
…
…
Your message:
h:in utText value="# sim leController.messa e " >
…
/h:body> 14
8/19/2019 JSF2 C06 Page Navigation
7/26
Example: Managed Bean
@ManagedBeanThis example uses explicit navigation rules,
but does not use ex licit bean declarations.
private String message="";
.
So, the @ManagedBean entry is needed so
that the references in the facelets pages to
#{simpleController.blah} point at this class.
public String doNavigation() {
return("too-short");
} else {
Strin results =
{ "page1", "page2", "page3" };
return(RandomUtils.randomElement(results));
}
}
}
15
Example: faces-config.xml
/start-page-1.xhtml
too-short
/error-message.xhtml
page1 /result-page-1.xhtml
page2
/result-page-2.xhtml
page3
/result-page-3.xhtml
16
8/19/2019 JSF2 C06 Page Navigation
8/26
Example: First Results Page.
=" ". .
xmlns:h="http://java.sun.com/jsf/html">
Result Page 1 =" ". .
rel="stylesheet" type="text/css"/>
tr>Result Pa e 1< th>< tr>
er resu s pages are sm ar.
Your message: "#{simpleController.message}"
/div> 17
Example: Results
18
8/19/2019 JSF2 C06 Page Navigation
9/26
© 2011 Marty Hall
Explicit Bean Declarations
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.
JSF Flow of Control
balance.xhtml.Uses
and
submit form
Find return ChooseRun Action forward result1.xhtml
Run Setter Methods
Business
Logic
results
POST request balance.jsf Bean value PageController Method result2.xhtml
...
resultN .xhtml
Uses
# bankForm.somePro ert
to display bean properties
In the previous section, we used
@ManagedBean to give a name to the bean
based on the class name (or, we used
=" "
20
.
Now, we declare the bean name explicitly in
faces-config.xml.
8/19/2019 JSF2 C06 Page Navigation
10/26
Bean Declarations: Syntax
- …
someName - -
somePackage.SomeClass
- - - -
…
faces-confi >
Scopes are request, session, application, view,
none, custom. They are discussed elsewhere.Unlike with @ManagedBean, there is no default,
- -
21
- -
altogether.
You can also define (“inject”) bean properties in
faces-config. This is discussed in separate tutorial
section.
Pros and Cons of Explicit Bean
• @ManagedBean – Simpler/faster to start with
– Java developer knows the bean name
• xp c ec ara ons n aces-con g.xm – Easier for facelets developer to find bean
• ,knowing the bean class name (as with @ManagedBean)requires you to search many packages to find the class
– scopes (Paul Lee rule)
– Better understanding of beans used in project• One central file lists all managed beans
22
I find the arguments in favor of explicit declarations to be weak (as compared to explicit navigations rules, for which I find the arguments to be strong). So, on most real projects I
would mildly recommend the use of @ManagedBean. The exceptions are if I want to inject properties (in which case I would definitely use explicit declarations), or if I have a large
project with many packages (in which case I would at least consider explicit declarations to make it easier to find the beans).
8/19/2019 JSF2 C06 Page Navigation
11/26
Example
• Overview – Start with form that collects a message
– Go to either an error page (message too short) or a results
• Implementation – Reuse the same bean as in last exam le
– Use managed-bean• Declare bean explicitly in faces-config.xml, so that the
– Use navigation-rule
• Remap the return conditions so that page1, page2, andpage a po n o e same resu s page
23
Example: Starting Page.
=" ". .
xmlns:h="http://java.sun.com/jsf/html">
…
…
Your message:
h:in utText value="# messa eHandler.messa e " >
…
/h:body> 24
8/19/2019 JSF2 C06 Page Navigation
12/26
Example: Managed Bean
package coreservlets;
public class SimpleController2
extends SimpleController {
// Inherits getMessage, setMessage,
// and doNavigation. doNavigation returns
// "too-short", "page1", "page2", or "page3".
25
Example: faces-config.xml
…
-
messageHandler
.
request
-
…
26
8/19/2019 JSF2 C06 Page Navigation
13/26
Example: faces-config.xml
/start- a e-2.xhtml
too-short /error-message.xhtml
-
page1
/ message-page.xhtml
page2
- - - - -.
page3
/ message-page.xhtml
27
Example: Main Results Page.
=" ". .
xmlns:h="http://java.sun.com/jsf/html">
Message Page
=" ". .
rel="stylesheet" type="text/css"/>
tr>Messa e Pa e< th>< tr>
Your message: "#{ messageHandler.message}"
/div> 28
8/19/2019 JSF2 C06 Page Navigation
14/26
Example: Results
29
© 2011 Marty Hall
Rules
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.
8/19/2019 JSF2 C06 Page Navigation
15/26
Summary
• Overview – * for from-view-id matches any starting page
– Omitting from-outcome: all other return conditions match• ,
• Application – * for from-view-id
• Multiple forms can share some of the results pageswithout repeating entries in navigation-rule
– - – • Can have multiple return values point at the same results
page. Useful when you want to change results pages
– Temporary results pages with details coming later
– Reusing existing Java code but different rules on results pages31
Example: * for from-view-id
• Problem – Both of the previous examples used the same error page
– The navigation-case was repeated
• – Make a shared entry that maps “too-short” to the error
a e
32
8/19/2019 JSF2 C06 Page Navigation
16/26
faces-config.xml: Before
start- a e-1.xhtml< from-view-id>
too-short /error-message.xhtml
-
/start-page-2.xhtml
too-short
/error-message.xhtml
< nav ga on-ru e>
33
faces-config.xml: After
*< from-view-id>
too-short
/error-message.xhtml
- - - - - -.
/start-page-2.xhtml
34
8/19/2019 JSF2 C06 Page Navigation
17/26
Example:
• Problem – In the second example, we reused Java code from the first
example (good). But, we repeated virtually the samenavi ation-case three times bad .
• Solution – Omit the from-outcome. This means that any return
values not explicitly mentioned are mapped to the sameresults page.
• ,means to redisplay input form. This will be discussed in
the section on validation.
35
faces-config.xml: Before
/start-page-2.xhtml
page1
/message-page.xhtml
-
page2
/message-page.xhtml
page3
< o-v ew- > message-page.x m < o-v ew- >
36
8/19/2019 JSF2 C06 Page Navigation
18/26
faces-config.xml: After
/start-page-2.xhtml
/message-page.xhtml
-
37
Conditional Navigation Rules
• Idea – You can put tags that designate when rules apply
• Examplenav ga on-case
success
# user.returnVisitor /welcome-back.xhtml
success
.
/welcome-aboard.xhtml
38
8/19/2019 JSF2 C06 Page Navigation
19/26
Dynamic To-Ids
• Idea – You can compute the destination page directly in faces-
config, rather than indirectly via return value of the actioncontroller method
• Example
/exam-question.xhtml
exam.next uest on age < to-v ew- >
-
39
© 2011 Marty Hall
Static Navigation
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.
8/19/2019 JSF2 C06 Page Navigation
20/26
Idea
• Situation – Sometimes you don’t want/need Java code
• No input elements•
• Sometimes used for simple testing. But also used in realprojects when you want a button for navigation.
– Instead of this•
• Have the doNav method always return “fixed-page”
– Use this" "… -
– You can use either default mapping or explicit navigation rules todetermine the meaning of “fixed-page”
41
Example
• Overview – page-a.xhtml has a button that causes navigation to
page-b.xhtml
– - . page-a.xhtml
• Implementation – Page A
•
– •
42
8/19/2019 JSF2 C06 Page Navigation
21/26
Example: page-a.xhtml
=" ". .
xmlns:h="http://java.sun.com/jsf/html">
JSF 2.0: Static Navigation …
=" "
Page A
table>
h:commandButton value="Go to Pa e B"
action="page-b"/>
/div> 43
Example: page-b.xhtml
=" ". .
xmlns:h="http://java.sun.com/jsf/html">
JSF 2.0: Static Navigation
…
=" "
Page B
table>
h:commandButton value="Go to Pa e A"
action="page-a"/>
/div> 44
8/19/2019 JSF2 C06 Page Navigation
22/26
Example: Results
45
© 2011 Marty Hall
Problems
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.
8/19/2019 JSF2 C06 Page Navigation
23/26
Pressing Button and Nothing
• Issue – Many error conditions simply result in the system
redisplaying the form with no warning or error messages
– Set PROJECT_STAGE to Development in web.xml• This is already set in jsf-blank
– Many of the errors cause the process to abort at certain points. Knowing how far things got is very helpful.
• Put a print statement in the controller method
• Put a print statement in the empty constructor – public MyBean() { System.out.println("MyBean built"); }
– Bean should be instantiated twice for request scope
• Put print statements in the bean setter methods47
Pressing Button and Nothing
1. Return value of controller method does notmatch from-outcome of navigation-case
• Remember values are case sensitive
. - -
accepted
Should be from-outcome, not from-action
/accept-registration.jsp
• ,from-outcome
• This situation occurs frequently with Eclipse users thaton’t oo care u y at t e c o ces Ec pse o ers n
popup menu for the navigation-case entries.48
8/19/2019 JSF2 C06 Page Navigation
24/26
Pressing Button and Nothing
3. Forgetting # in action of h:commandButton
Should have # here
• This is really a special case of (1), sinceaction="{beanName.methodName}" means the literal
" ". -• In this situation and several others, it is very helpful to
put a print statement in controller method to see if/when
4. Typo in from-view-id• This is a s ecial case of 1 since the from-outcome
applies to nonexistent page
49
Pressing Button and Nothing
5. Controller method returns null ,
done accidentally as well.
6. Type conversion error ,
when you submit.• Behavior of redisplaying form is useful here.
See validation section.7. Missing setter method
• You associate textfield with bean property foo, but there is nosetFoo method in your bean.• Debugging hint: You will see printout for bean being
instantiated, but not for controller method
8. Missing h:form• you use : npu ex w no surroun ng : orm, ex e s w
still appear but nothing will happen when you press submit button
50
8/19/2019 JSF2 C06 Page Navigation
25/26
© 2011 Marty Hall
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
• Explicit navigation rules – ,
understand system page flow more easily
• Explicit bean declarations – It is less clear whether this is better than Mana edBean but in lar e
projects with many packages, this might let you find beans more easily
• Wildcards in navigation rules
– * in from-view-id lets you share results pages across forms – Omitting from-outcome lets you map different return values to the same
results page
• Static navigation – For simple testing, you can use a static string (instead of an EL expression)
for the action of h:commandButton
• Navigation problems – any s ua ons resu n re sp ay ng npu orm
• Most due to return value of Java code not matching from-outcome
– Debugging: set PROJECT_STAGE to Development, trace Java code52
8/19/2019 JSF2 C06 Page Navigation
26/26
© 2011 Marty Hall
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.