JSF 重定向到其他页面

标签 jsf redirect submit

我有三个 XHTML 页面;

  • index.xhtml
  • page_1.xhtml
  • page_2.xhtml

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

    我如何将用户重定向到 page_2.xhtml来自 index.xhtml使用另一个 commandButton假设 commandButtons ' 操作链接到支持 Java 类?

    最佳答案

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

    <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 支持隐式导航。导航结果可以只是所需目标 View 的路径/文件名。结果中的文件扩展名是可选的。
    如果您不一定需要对导航执行任何业务操作,或者您可以在目标页面的支持 bean 的 (post) 构造函数中执行此操作,那么您也可以将结果值放在 action 中。直接地。
    <h:commandButton value="Go to page 1" action="page_1" />
    <h:commandButton value="Go to page 2" action="page_2" />
    
    一个 <h:commandButton>但是不会执行重定向,而是转发。最终用户不会在浏览器地址栏中看到正在更改的 URL。目标页面不可收藏。如果可以,我建议使用 <h:button>反而。
    <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";
    }
    
    也可以看看:
  • How to navigate in JSF? How to make URL reflect current page (and not previous one)
  • When should I use h:outputLink instead of h:commandLink?
  • 关于JSF 重定向到其他页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9463295/

    相关文章:

    php - 使用 www 将完整的网站重定向到 https

    ajax - 我还可以将什么传递给 Ext.Ajax.request 的 `form` 属性?

    php - 使用AJAX .on(click)方法提交多个HTML表单+其他变量数据

    forms - Ruby on Rails 表单中的 select 和 onChange

    java - 弹出窗口 - 执行操作并关闭

    jsf - Primefaces 确认消息更新值

    java - Servlet未获取请求参数

    .htaccess - 多个参数的多个重写条件

    java - JSF 2 复合 :actionSource exposing commandButtons in ui:repeat

    java - JSF 转换器如何处理 :convertDateTime works?