JSF重定向到其他页面 [英] JSF redirect to other page

查看:134
本文介绍了JSF重定向到其他页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个XHTML页面;

I have three XHTML pages;

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

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

In the index.xhtml page, I have a commandButton which sends the user to page_1.xhtml. All this is done in the navigation rule in faces-config.xml.

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

How would I redirect the user to page_2.xhtml from the index.xhtml using another commandButton assuming that both commandButtons' actions are linked to a backing Java class?

推荐答案

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

Just bind the buttons to different action methods which each return a different outcome.

<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支持隐式导航.导航结果可以只是所需目标视图的路径/文件名.结果中的文件扩展名是可选的.

Navigation cases are not necessary. JSF 2.0 supports implicit navigation. The navigation outcome can just be the path/filename of the desired target view. The file extension in the outcome is optional.

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

If you don't necessarily need to perform any business action on navigation, or you can do it in the (post)constructor of the backing bean of the target page instead, then you can also just put the outcome value in the action directly.

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

A <h:commandButton>不会执行重定向,而是执行转发.最终用户不会在浏览器地址栏中看到URL更改.目标页面不可添加书签.如果可以的话,我建议改用<h:button>.

A <h:commandButton> will however not perform a redirect, but a forward. The enduser won't see the URL being changed in the browser address bar. The target page isn't bookmarkable. If you can, I'd suggest to use <h:button> instead.

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

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

Or if you really need to invoke a business action, but would like to perform a real redirect, then append faces-redirect=true as query string to the outcome value.

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

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

另请参见:

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆