【问题标题】:JSF redirect to other pageJSF 重定向到其他页面
【发布时间】:2012-03-16 19:46:52
【问题描述】:

我有三个 XHTML 页面;

  1. index.xhtml
  2. page_1.xhtml
  3. page_2.xhtml

index.xhtml 页面中,我有一个commandButton,它将用户发送到page_1.xhtml。这一切都在faces-config.xml 的导航规则中完成。

假设 commandButtons 的两个操作都链接到支持 Java 类,我将如何使用另一个 commandButton 将用户从 index.xhtml 重定向到 page_2.xhtml

【问题讨论】:

  • 为什么不复制和更改导航规则和命令按钮?
  • @Daniel 它也不是这样工作的
  • 您使用的是 JSF 1.x 还是 2.x?您真的需要在页面到页面导航中调用托管 bean 操作方法吗?
  • @BalusC 它的 JSF 2.0.. 是的,我想是的,因为我必须在每个页面上显示不同的值
  • 为什么要使用导航规则?您确定您没有过多关注 JSF 1.x 教程吗?另外,这些请求真的必须是 POST 请求而不是 GET 请求吗?

标签: jsf redirect submit


【解决方案1】:

如下图添加两个导航案例。在操作方法中,返回与按钮对应的结果。

            <navigation-rule>
                <from-view-id>index.html</from-view-id>
                <navigation-case>
                    <from-outcome>page1</from-outcome>
                    <to-view-id>page_1.xhtml</to-view-id>
                </navigation-case>
                <navigation-case>
                    <from-outcome>page2</from-outcome>
                    <to-view-id>page_2.xhtml</to-view-id>
                </navigation-case>
            </navigation-rule>

【讨论】:

  • 在这种情况下如何添加重定向
  • 我有同样的问题,?faces-redirect=true 被添加到支持 bean 的 return "some_view?faces-redirect=true"; 它给了我缺少的导航规则消息。
  • @AjaySharma 在 &lt;navigation-case&gt; 块内,您可以添加自结束标签 &lt;redirect /&gt;
【解决方案2】:

只需将按钮绑定到不同的操作方法,每个方法返回不同的结果。

<h:commandButton value="Go to page 1" action="#{bean.goToPage1}" />
<h:commandButton value="Go to page 2" action="#{bean.goToPage2}" />

public String goToPage1() {
    // ...
    return "page_1";
}

public String goToPage2() {
    // ...
    return "page_2";
}

不需要导航案例。 JSF 2.0 支持隐式导航。导航结果可以只是所需目标视图的路径/文件名。结果中的文件扩展名是可选的。

如果您不一定需要对导航执行任何业务操作,或者您可以在目标页面的支持 bean 的 (post) 构造函数中执行此操作,那么您也可以将结果值放在直接action

<h:commandButton value="Go to page 1" action="page_1" />
<h:commandButton value="Go to page 2" action="page_2" />

然而,&lt;h:commandButton&gt; 不会执行重定向,而是执行转发。最终用户不会在浏览器地址栏中看到正在更改的 URL。目标页面不可收藏。如果可以的话,我建议改用&lt;h:button&gt;

<h:button value="Go to page 1" outcome="page_1" />
<h:button value="Go to page 2" outcome="page_2" />

或者如果您真的需要调用业务操作,但想要执行真正的重定向,则将faces-redirect=true 作为查询字符串附加到结果值。

public String goToPage1() {
    // ...
    return "page_1?faces-redirect=true";
}

public String goToPage2() {
    // ...
    return "page_2?faces-redirect=true";
}

另见:

【讨论】:

    【解决方案3】:

    您也可以这样做,将代码的任何部分重定向到“example.xhtml”

    ExternalContext ec = FacesContext.getCurrentInstance()
            .getExternalContext();
    try {
        ec.redirect(ec.getRequestContextPath()
                + "/faces/jsf/example.xhtml");
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-01
      • 2018-07-10
      • 2023-04-09
      • 2021-11-06
      • 1970-01-01
      相关资源
      最近更新 更多