02 Page Navigation PDF
02 Page Navigation PDF
Static Navigation
Blah.jsp faces-config.xml
- beans declared in managed-bean section
- mapping of return conditions
declared in navigation-rule section
Business
Logic
results
Example: Registration
• Started by copying jsf-blank-myfaces
– Renamed it to jsf-navigation
– Edited .settings/…component file as in previous lecture
• Original URL
– http://hostname/jsf-navigation/register.faces
• When form submitted
– A static page (WEB-INF/results/result.jsp) is displayed
• Static result
– No business logic, beans, or Java code of any sort
• Main points
– Format of original form
– Use of navigation-rule in faces-config.xml
10
Main Points of This Example
• Input form has following format:
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<f:view>
HTML markup
<h:form>
HTML markup and h:blah tags
</h:form>
HTML markup
</f:view>
• faces-config.xml specifies navigation rules:
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE faces-config PUBLIC …>
<faces-config>
<navigation-rule>
<from-view-id>/blah.jsp</from-view-id>
<navigation-case>
<from-outcome>some string</from-outcome>
<to-view-id>/WEB-INF/results/something.jsp</to-view-id>
</navigation-case>
</navigation-rule>
11 </faces-config>
12
Step 2: Create Input Form
• Basic format
<%@ taglib uri="http://java.sun.com/jsf/core" prefix="f" %>
<%@ taglib uri="http://java.sun.com/jsf/html" prefix="h" %>
<f:view>
…
<BODY>
…
<h:form>
…
</h:form>
…
</BODY>
</f:view>
• Invoking page
– Actual file is blah.jsp
– URL is blah.faces
13
Step 2: Result
• File is …/WebContent/register.jsp
• URL is http://localhost/jsf-navigation/register.faces
16
Step 3: Edit faces-config.xml
• General format
<?xml version='1.0' encoding='UTF-8'?>
<!DOCTYPE faces-config PUBLIC …>
<faces-config>
…
</faces-config>
18
Step 4: Create Results Pages
• RequestDispatcher.forward used
– So page can/should be in WEB-INF
• Example code:
– …/WEB-INF/results/result.jsp
<!DOCTYPE …>
<HTML>
<HEAD>…</HEAD>
<BODY>
<CENTER>
<TABLE BORDER=5>
<TR><TH CLASS="TITLE">Success</TH></TR>
</TABLE>
<H2>You have registered successfully.</H2>
</CENTER>
</BODY></HTML>
19
20
Step 5: Prevent Direct Access
to JSP Pages
• Filename/URL correspondence
– Actual files are of the form blah.jsp
– URLs used are of the form blah.faces
– You must prevent clients from directly accessing JSP
pages
• Since they would give erroneous results
• Strategies
– You cannot put input-form JSP pages in WEB-INF
• Because URL must correspond directly to file location
– So, use filter in web.xml. But:
• You have to know the extension (.faces)
• Assumes no non-JSF .jsp pages
• This is a major drawback to JSF design
21
22
Preventing Direct Access:
FacesRedirectFilter
public class FacesRedirectFilter implements Filter {
private final static String EXTENSION = "faces";
24
Preventing Direct Access:
Result
• Either URL
– http://localhost/jsf-navigation/register.faces
– http://localhost/jsf-navigation/register.jsp
25
Dynamic Navigation
Blah.jsp faces-config.xml
- beans declared in managed-bean section
- mapping of return conditions
declared in navigation-rule section
Business
Logic
results
27
32
Step 2: Create Input Form
• Same general syntax as in previous example
– Except for action of commandButton
Step 2: Result
• File is …/WebContent/signup.jsp
• URL is http://localhost/jsf-navigation/signup.faces
34
Step 3: Edit faces-config.xml
(A) Declaring the bean
…
<faces-config>
<managed-bean>
<managed-bean-name>
healthPlanController
</managed-bean-name>
<managed-bean-class>
coreservlets.HealthPlanController
</managed-bean-class>
<managed-bean-scope>request</managed-bean-scope>
</managed-bean>
…
</faces-config> Use request scope unless you have
a specific reason to use session scope
or (rarely) application scope
35
<!DOCTYPE …>
<HTML>
<HEAD>…</HEAD>
<BODY>
<CENTER>
<TABLE BORDER=5>
<TR><TH CLASS="TITLE">Accepted!</TH></TR>
</TABLE>
<H2>You are accepted into our health plan.</H2>
Congratulations.
</CENTER>
</BODY></HTML>
37
<!DOCTYPE …>
<HTML>
<HEAD>…</HEAD>
<BODY>
<CENTER>
<TABLE BORDER=5>
<TR><TH CLASS="TITLE">Rejected!</TH></TR>
</TABLE>
<H2>You are rejected from our health plan.</H2>
Get lost.
</CENTER>
</BODY></HTML>
38
Step 4: Results
39
40
© 2012 Marty Hall
Notes and
Additional Capabilities
Summary
• Wildcards in navigation rule
– * for from-view-id matches any starting page
– Omitting from-outcome results in all values matching
• from-action in addition to from-outcome
– For when different buttons invoke different methods and methods
have same values mapped differently. Overused.
• Getting the request and response objects
ExternalContext context =
FacesContext.getCurrentInstance().getExternalContext();
HttpServletRequest request =
(HttpServletRequest)context.getRequest();
HttpServletResponse response =
(HttpServletResponse)context.getResponse();
• Interleaving managed-bean & navigation-rule
– It is legal to alternate back and forth
42
Wildcards in Navigation Rules
• * for from-view-id matches any starting page
– Used when multiple different pages map same return
value to same result page
• Example
<navigation-rule>
<from-view-id>*</from-view-id>
<navigation-case>
<from-outcome>success</from-outcome>
<to-view-id>/WEB-INF/results/success.jsp</to-view-id>
</navigation-case>
</navigation-rule>
43
Without Wildcards
<navigation-rule>
<from-view-id>/page1.jsp</from-view-id>
<navigation-case>
<from-outcome>condition1</from-outcome>
<to-view-id>/WEB-INF/results/result1.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>unknown-user</from-outcome>
<to-view-id>/WEB-INF/results/unknown.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/page2.jsp</from-view-id>
<navigation-case>
<from-outcome>condition2</from-outcome>
<to-view-id>/WEB-INF/results/result2.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>unknown-user</from-outcome>
<to-view-id>/WEB-INF/results/unknown.jsp</to-view-id>
</navigation-case>
</navigation-rule>
44
With Wildcards
<navigation-rule>
<from-view-id>*</from-view-id>
<navigation-case>
<from-outcome>unknown-user</from-outcome>
<to-view-id>/WEB-INF/results/unknown.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/page1.jsp</from-view-id>
<navigation-case>
<from-outcome>condition1</from-outcome>
<to-view-id>/WEB-INF/results/result1.jsp</to-view-id>
</navigation-case>
</navigation-rule>
<navigation-rule>
<from-view-id>/page2.jsp</from-view-id>
<navigation-case>
<from-outcome>condition2</from-outcome>
<to-view-id>/WEB-INF/results/result2.jsp</to-view-id>
</navigation-case>
</navigation-rule>
45
46
Explicit from-outcome
<navigation-rule>
<from-view-id>/page1.jsp</from-view-id>
<navigation-case>
<from-outcome>condition1</from-outcome>
<to-view-id>/WEB-INF/results/result1.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>condition2</from-outcome>
<to-view-id>/WEB-INF/results/result2.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>condition3</from-outcome>
<to-view-id>/WEB-INF/results/result2.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-outcome>condition4</from-outcome>
<to-view-id>/WEB-INF/results/result2.jsp</to-view-id>
</navigation-case>
</navigation-rule>
47
48
from-action
• Designates the method that you came from
– Suppose you had two buttons that invoked two different methods,
and both returned "error". But you want the two "error" values to
have different results pages.
<navigation-rule>
<from-view-id>/somepage.jsp</from-view-id>
<navigation-case>
<from-action>#{beanName.method1}</from-action>
<from-outcome>error</from-outcome>
<to-view-id>/WEB-INF/results/err1.jsp</to-view-id>
</navigation-case>
<navigation-case>
<from-action>#{beanName.method2}</from-action>
<from-outcome>error</from-outcome>
<to-view-id>/WEB-INF/results/err2.jsp</to-view-id>
</navigation-case>
</navigation-rule>
– Easier to avoid using the same names for different things
– Rarely needed in real life, but some books (JSF The Complete
Reference) use it needlessly.
49
51
Common Problems
Summary
• Basic steps to using JSF
– Create a bean
• For now, only contains controller method
– Create an input form
• The action of h:commandButton refers to controller method
– Edit faces-config.xml
• Declare bean
• Define navigation rules
– Create results pages
– Prevent direct access to JSP pages
• Static navigation
– Specify literal outcome as action of button
• Outcome mapped by faces-config.xml to output page
• Dynamic navigation
– Specify method as action of button
– Method returns outcomes
58 • Outcomes mapped by faces-config.xml to output pages
© 2012 Marty Hall
Questions?