jsf in action 笔记:在页面的海洋中导航(3.4 Navigating the sea of pages)--2008.04.16

本文详细介绍了JavaServer Faces (JSF) 中的导航系统工作原理,包括导航控制器如何基于组件产生的outcome值决定页面跳转,及如何通过XML配置文件定义导航规则。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

2008.04.16 摘自 JSF in action -- 3.4 Navigating the sea of pages   Page/163

1.  JSF上支持从一个页面到另一页面,与Struts 中的ActionForward有点相似,JSF称之为导航系统(JSF navigation system.) ,控制这种页面与页面之间联系的一个重要的部件是导航控制者(navigation handler),它的职责就是判断下一步应该加载那一个页面.这本书中主要描述的是默认的导航控制者(以下简称为handler)的处理特点.(当然这个handler是可以改变的).

2.  原文:(The navigation handler operates in response to action events,3 which are usually executed by
action sources (components that capture user gestures, like buttons or hyperlinks).用户点击组件(像按钮和可以触发事件的链接),这些组件产生事件,事件被相应的方法(这里指的是action method)处理后,action method后返回一个String 字符串(outcome),然后handler根据这个string返回值(logical outcome或者是hardcoded outcome--硬编码的值),去到配置文件找这个返回字符串对应的页面,然后把这个页面展现.这里只是简单的概述了一下,实际上的操作会有点复杂,下面会做具体的分析.

3.  原文:(The navigation handler operates on a set of navigation rules, which define all of the application’s possible navigation paths. A navigation rule specifies which pages can be selected from a specific page or set of pages. Each path to a page is represented by a single navigation case. The navigation case is selected based on logical outcome (retrieved from the component itself or its associated action method) and/or the expression for the executed action method itself.)

handler要处理很多的导航规则(navigation rule),这些规则就是指定从那一个页面或者是那一些页面(通过类似于与正则表达式的方式)可以去到某一个特定的页面.这种从一个页面到一个页面是通过配置一个导航示例(navigation case )来实现的,具体选择那一个导航示例,是由一个outcome值来决定的.

4.下面是一个导航的示例图:JSF navigation rule sample

这个配置文件中涉及到了三个页面:PlaceOrder.jsp, ConfirmOrder.jsp, and OrderEntry.jsp.

  • 从PlaceOrder.jsp去到的页面会有两种情况(case),当outcome为"success"时,去到的是ConfirmOrder.jsp这个页面,当outcome为"failure"时去到的页面是OrderEntry.jsp这个页面.
  • OrderError.jsp也有一个导航规则(navigation rule)当outcome为"retry"时去到页面是PlaceOrder.jsp
  • 这里对于ConfirmOrder.jsp这个页面没有导航规则,也就是说从ConfirmOrder.jsp不会去到任何页面.

5.  上面示例的.xml配置文件的代码为:

Listing 3.6 The XML source for the navigation rules shown in figure 3.13

<navigation-rule>
  
<from-view-id>/OrderError.jsp</from-view-id>
  
<navigation-case>
    
<from-outcome>retry</from-outcome>
    
<to-view-id>/PlaceOrder.jsp</to-view-id>
  
</navigation-case>
</navigation-rule>

<navigation-rule>
  
<from-view-id>/PlaceOrder.jsp</from-view-id>
  
<navigation-case>
    
<from-outcome>success</from-outcome>
    
<to-view-id>/ConfirmOrder.jsp</to-view-id>
  
</navigation-case>
  
<navigation-case>
    
<from-outcome>failure</from-outcome>
    
<to-view-id>/OrderError.jsp</to-view-id>
    
<redirect/>
  
</navigation-case>
</navigation-rule>

6.  导航规则的.xml文件中元素项的示意图:

JSF navigation rule configure

Figure 3.14
Navigation consists of navigation rules, which are defined with the <navigation-rule> element in a JSF configuration file. Optionally, a <navigation-rule> can specify the page to which it applies with the <from-view-id> element. Each <navigation-rule> has one or more <navigation-case> elements, which must have a <to-view-id> element that specifies the next page to load. A <navigation-case> can optionally specify an outcome with a <from-outcome> element and an action with the <from-action> element. If the <redirect> element is
specified, JSF will send an HTTP redirect instead of forwarding to the view internally. Both the <navigation-rule> and <navigation-case> elements can optionally have <description>, <display-name>, and <icon> elements which are generally used for tools.

7.  具体的规则:

  1. 一个navigation rule下可以包含多个navigation case,表示从一个页面可以跳转到的页面有多少种可能.
  2. <from-view-id> 指的是从那一个页面接受的用户请求,内容为页面的地址或者为页面的名称(包含一个相对路径的值)
  3. <to-view-id> 表明的去向的页面的地址或名称.
  4. <from-view-id>和<to-view-id>可以指向的是任何的页面或资源,必须以"/"开头,而且包含相对于应用的根目录的地址(it must be relative to the web application’s root directory and prefixed with a slash (/).)
  5. 当<navigation-case>中指定了<redirect>值时,是告诉JSF redirect这个请求到相应的地址去.这时用户看到它的页面展示地址变了,不再是请求的那个地址.
  6. 对于一些通用的outcome值没有必要再指定其它的一些页面,比如当outcome这个返回值为"login","logout"等,

8.  outcome值是从何处来?这里重点说一下这个判断执行那个navigation case的根据,因为刚开始学习JSF的navigation 的时候,对于这个outcome总会让人分不清.

  1. outcome 值可以由组件中的 action属性来决定.
  2. 组件中的action属性可以有两种写法,一种是直接写一个字符串,比如上面例子中的由/OrderError.jsp页面到/PlaceOrder.jsp的组件代码就为:
    <h:commandButton value="Retry order submission" action="retry"/>
    这种写法也是static outcome或硬编码(hardcode outcome)
  3. 另一种方法是配置组件的action属性到一个方法,具体的outcome值由应用执行完这个action method后来决定返回的值是什么,就像PlaceOrder.jsp页面上的组件的代码这样:
    <h:commandButton value="Place Order" action="#{orderManager.placeOrder}"/>
    用户发送请求后,由应用来调用orderManager 的placeOrder 方法,这个方法返回一个字符串(outcome)"success" or "failure",当然这个action method 也可以返回null,当返回null的时候不会执行这种导航或跳转.
  4. 当组件中不设置action 属性的时候也是可以在.xml配置文件中指定产生outcome值的方法的,这就是<from-action>要做的.就像这样: <navigation-case>
    <from-action>#{orderManager.placeOrder}</from-action>
    <from-outcome>success</from-outcome>
    <to-view-id>/ConfirmOrder.jsp</to-view-id>
    </navigation-case>
    ,只有从orderManager中placeOrder方法过来,而且outcome值为"success"才执行这个case.如果把<from-outcome>success</from-outcome>去掉,则说明只要是从orderManager中placeOrder方法过来的就到>/ConfirmOrder.jsp去,不管outcome的值是多少,是null也罢.
  5. <from-view-id>中的值可以用匹配的方式来指定:
    <navigation-rule>
    <from-view-id>/corporate/*</from-view-id>或者<from-view-id>*</from-view-id>
    <navigation-case>
    <from-outcome>about</from-outcome>
    <to-view-id>/corporate/about.html</to-view-id>
    </navigation-case>
    <navigation-case>
    <from-outcome>contact info</from-outcome>
    <to-view-id>/corporate/contact.html</to-view-id>
    </navigation-case>
    </navigation-rule>
    表明只要用户的请求的url地址符合匹配方式就可以
  6. 最后要说的是组件中的action 属性和 actionListener属性,区别主要在于action 是用来做导航(navigation)的,而actionlistener是指用户产生一个行为的时候(点击按钮等)这个按钮要触发的方法.
写在最后:写导航这一篇是我写的最长的一篇,因为这里面涉及的东西太多,而且如果是第一篇看JSF会注意不到action与actionListener的区别而产生混淆,我的理解或者说是翻译肯定会有很多地方不正确,希望能给我能表达清楚了我的理解,这篇文章随意看JSF的深入还会回来纠正一些东西,希望看到的读者可以多提意见!


 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值