JSF Navigation by Examples

xiaoxiao2021-03-06  40

The JavaServer Faces (JSF) Navigation Framework provides navigation rules that allow you to define navigation from view to view (mostly JSP pages) in a Web application. These navigation rules are defined in JSF configuration files along with other definitions for a JSF application. Usually , this file is named

Faces-config.xml. However, you can assign, Other Name and Even Use More Than One File to Store JSF Configuration Data. To Specify Configuration Files, Use Lines Like The Following In Your

Web.xml file:

Code: javax.faces.config_files /web-inf/faces-config.xml ,/web-inf/faces-beans.xml < / param-value>

A Simple Example The Actual Construction of a Navigation Rule Is Quite Simple. Let's Look AT Our First Example:

Code: /pages/inputname.jsp SayHello /PAGES/greeting.jsp Saygoodbye / pages / goodbye .jsp

This code specifies that view /pages/inputname.jsp has two outputs, sayHello and sayGoodbye, associated with particular pages. Making a Default Outcome Case The basic construction is simple, but there are many variations you can do on the basic construction. Look at The next snippet:

Code: /pages/inputname.jsp SayHello /PAGES/greeting.jsp /pages/goDBye.jsp This code is very similar to the previous example, except that the from-outcome element for the second navigation-case is missing. This means that all outcomes except sayHello, will be forwarded to / pages /Goodbye.jsp Using Patterns The JSF Navigation Model Allows Us Use Patterns. THESE PATTERSK "*". This is shown in the next example:

Code: / pages / * menu /MENU /MAIN_MAIN.JSP info /menu/info.html

THIS Navigation rule will be coplied for any page limited HAS / PAGES / AS The Asterisk Must Be Located At the Asterisk Must Be Located At the End. For Example, a Pattern Such AS / Pages / * .jsp Would Not Work. Resolving More Than Matching Rule Now Let's Pay More Attention To How Rules Can Be Specified in The JSF Navigation Model. Look At this next example:

Code: / pages / * info /Menu/generalhelp.html /pages/login.jsp info /menu/loginhelp.html in this example, the second navigation rule, not the first rule, will work for outcomes from /pages/login.jsp even though it is also matched by the pattern / pages / * in the first rule. The most specific match of from-view-id always takes precedence for a particular from-outcome. "Global" Outcomes Let's say we want the globalHelp outcome from any page to always cause a transition to the page /help/index.html. for this, we can use Either of these Two declarations:

Code: * globalhelp / menu / GeneralHelp.html globalhelp /MENU /GENERALHELP.HTML

THE FIRST SNIPPES An Asterisk for the from-view-id Element, While The Second Snippet Doesn't Use a from-view-id element at all. Both Approaches Work The Same. Note Though, That An Empty from-View-ID Element, AS Displayed In The Snippet Below, Will Not Work At AllCode: globalhelp /menu/generalhelp.html

Collision of Rules Here is an inTeresting Question. What happens when a couple of navigation rules That Have The Same from-view-id and the same from-outcome point to diffreent Pages. Look at the next esample:

Code: * globalhelp / menu / GeneralHelp.html * < From-outcome> globalhelp /pages/goaway.html

The last rule is always used in such a situation. Also, remember, we spoke at the very beginning of this article about the possibility of splitting JSF configuration data into several files. In the case where the competing rules are in different configuration files, the rule in the last loading configuration file as listed in the web.xml file prevails Spreading Out Parts of a Navigation rule This is one more variation on the same theme Compare the following snippets:.. Code: /pages/inputname.jsp Sayhello /pages/greeting.jsp Saygoodbye /pages/goDBye.jsp < / Navigation-Case>

Code: /pages/inputname.jsp SayHello /PAGES/greeting.jsp ... / pages / inputname. JSP Saygoodbye /pages/goDBye.jsp

Both snippets produce the same result at run-time. However, the second snippet shows that the declaration can be arbitrarily split up and spread out into different parts of a configuration file or even different configuration files. You can use either approach based on your own . preferences Navigation Rules in Action Now, it is time to lift the veil a little to see how what we've learned can be put to use in an application A JSP page might contain the following line:. Code:

The Action Attribute Will Be Used as an outcome. Or, here is another variation:

Code:

This means that the helloAction method of GetNameBean will be invoked and the result will be used as an outcome. HelloAction should be a public method that returns String. This distinction between two ways of specifying the action attribute is significant in considering an element in the configuration File We Haven't Looked At Yet. This is the from-action element. Look at the Following Code:

Code: /pages/inputname.jsp SayHello /PAGES/anotherhello.jsp # {getNameBean.HelloAction} Sayello /pages/hello.jsp

In this code, both navigation cases have the same from-view-id and from-outcome, however the second navigation case includes a from-action element. If the sayHello outcome is determined by GetNameBean.helloAction, the second navigation case will take effect , but only because otherwise both cases had equal precedence Review Let's check how well you understand the material The page /pages/inputname.jsp has the following declaration for commandButton:.. Code:

The JSF Configuration File Contains The Following Code:

Code: /pages/inputname.jsp SayHello /A.JSP / pages / * # {getNameBean.HelloAction} SayHello /b.jsp

If The Submit Button Mentioned Above Is Pressed, What Will The next page /a.jsp or /b.jsp if getNameBean.HelloAction Returns Sayello?

转载请注明原文地址:https://www.9cbs.com/read-51247.html

New Post(0)