Jsf 页面导航Navigation总结

mac2022-06-30  27

导航(Navigation)现在对jsf中的导航进行些小结,分为三部分来说,参考与core jsf1,静态的导航2,动态的导航3,高级的导航主要讨论在你web程序中如何配置导航,即如何让你的程序从一个页面跳转到下一个页面,当然要根据你业务

逻辑的actions和outcomes。

1,静态导航(Static Navigation)在一个简单的web应用程序中,页面导航一般是静态指定的,在jsf中你只需要给每个按钮一个action属性,然

后再faces-config.xml中配置以下就可以轻松实现页面之间的跳转问题。例如:<h:commandButton label="Login" action="login"/>但是这样还不行,action必须匹配在faces-config.xml文件中的导航规则的outcome:<navigation-rule>

   <from-view-id>/index.jsp</from-view-id>

   <navigation-case>

     <from-outcome>login</from-outcome>

     <to-view-id>/welcome.jsp</to-view-id>

   </navigation-case>

</navigation-rule>这表明如果按钮在/index.jsp中被点击,页面将跳转到/welcome.jsp.注意:这里的view-id必须以"/"开始,扩展名为.jsp。例:如果你把from-view-id设置为/index.faces,将不

能工作。

我们看下面这个导航规则:<navigation-rule>

   <navigation-case>

     <from-outcome>logout</from-outcome>

     <to-view-id>/logout.jsp</to-view-id>

   </navigation-case>

</navigation-rule>

可以看到他没有from-view-id,这就意味着:无论你在整个web程序中的任何页面点击,只要outcome为logout

,她都会跳转到/logout.jsp页面。

我们再看下面这段导航规则:

<navigation-rule>

   <from-view-id>/index.jsp</from-view-id>

   <navigation-case>

     <from-outcome>login</from-outcome>

     <to-view-id>/welcome.jsp</to-view-id>

   </navigation-case>

   <navigation-case>

     <from-outcome>signup</from-outcome>

     <to-view-id>/newuser.jsp</to-view-id>

   </navigation-case>

</navigation-rule>

我们可以看到,她有一个相同的from-view-id,意思就是说在/index.jsp中如果你触发的outcome为login,页面

就会跳转到/welecom.jsp,要是为signup的话就跳转到/newuser.jsp页面。事实上我们会经常用到这种规则的

配置。

注意:如果没有导航规则来匹配一个给定的action,那么当前页面将简单的重新显示一下。

2,动态的导航(Dynamic Navigation)在大多数web程序中,导航都不是静态的。页面的流程不仅仅依靠你点击的那个按钮,而且还有根据你输入的

内容来判断。最常见的就是提交一个处理登陆的页面,可能有两种结果:success or failure!结果(outcome)

将依赖于一定的计算判断,譬如用户名和密码是否合法。

为了实现动态的导航,提交按钮必须有一个方法来参考,例如:

<h:commandButton label="Login" action="#{loginController.verifyUser}"/>loginController是某个类的受管Bean,这个类必须有一个方法名字是verifyUser,返回一个String,供

action属性使用,这个方法可能是这样:String verifyUser() {   if (...)      return "success";   else      return "failure";}注意:如果一个action method返回null,那么将重新显示页面本身。

 

3,高级的导航

As you can see from the syntax diagram, each navigation-rule and navigation-case element can

have arbitrary description, display-name, and icon elements. These elements are intended for use

in builder tools, and we do not discuss them further.

我们来看下面的导航规则:<navigation-case>

   <from-outcome>success</from-outcome>

   <to-view-id>/success.jsp</to-view-id>

   <redirect/>

</navigation-case>

你会发现在to-view-id后面多了一个redirect元素,Redirecting the page is slower than forwarding

because another round trip to the browser is involved. However, the redirection gives the

browser a chance to update its address field.注释:没有redirect,原来的url(localhost:8080/javaquiz/index.faces)在你从/index.jsp到/success.jsp

后在地址栏中没有改变,反之改变了,变成了localhost:8080/javaquiz/success.faces。

我们接着看下面的导航规则:<navigation-rule>

   <from-view-id>/secure/*</from-view-id>

   <navigation-case>

      . . .

   </navigation-case>

</navigation-rule>你会发现from-view-id中使用了通配符(wildcards),/secure目录下面的所有页面将应用导航规则。同理:<from-view-id>/*</from-view-id>or<from-view-id>*</from-view-id>将应用的所有的页面。

我们再看下面的导航规则:

<navigation-rule>     <from-view-id>/index/zhuce/denglu.jsp</from-view-id>     <navigation-case>       <from-action>#{LoginBean.doLogin}</from-action>       <from-outcome>success</from-outcome>       <to-view-id>/index/zhuce/welcome.jsp</to-view-id>     </navigation-case>     <navigation-case><from-action>#{LoginBean.doLogin}</from-action>         <from-outcome>failed</from-outcome>         <to-view-id>/index/zhuce/denglu.jsp</to-view-id>    </navigation-case></navigation-rule>你会发现多了一个<from-action>,上边这个好像不太恰当,因为from-action一般在下面这种情况下才能显示

出灵活性:

That flexibility can be useful if you have two separate actions with the same action string, or

two action method references that return the same action string.如果你有连个单独的actions却有相同的action string或两个相同的action method返回相同的action

string.举个例子:answerAction和startOverAction都返回again<navigation-case>

<from-action>#{quiz.answerAction}</from-action>

<from-outcome>again</from-outcome>

<to-view-id>/again.jsp</to-view-id>

</navigation-case>

<navigation-case>

<from-action>#{quiz.startOverAction}</from-action>

<from-outcome>again</from-outcome>

<to-view-id>/index.jsp</to-view-id>

</navigation-case>

 

 

例如,假设在我们的测验程序中,startOverAction返回字符串"again"而不是"Start Over"。answerAction也可能返回相同的字符串。为了区别两种导航情况,可以使用from-action元素。该元素的内容必须与action属性的方法表达式字符串相同。

<navigation-case> <from-action>#{quiz.answerAction}</from-action> <from-outcome>again</from-outcome> <to-view-id>/again.jsp</to-view-id> </navigation-case> <navigation-case> <from-action>#{quiz.startOverAction}</from-action> <from-outcome>again</from-outcome> <to-view-id>/index.jsp</to-view-id> </navigation-case>

说明

 

导航处理程序不会调用#{...}分隔符中的方法。在导航处理程序处理之前,该方法已经被调用。导航处理程序只是使用from-action字符串作为一个主键,用于查找匹配导航的情况

 

转载于:https://www.cnblogs.com/Catherinezhilin/p/9910194.html

相关资源:JAVA上百实例源码以及开源项目
最新回复(0)