JSF 重定向到其他页面 [英] JSF redirect to other page
问题描述
我有三个 XHTML 页面;
I have three XHTML pages;
- index.xhtml
- page_1.xhtml
- 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 类?
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
然而不会执行重定向,而是转发.最终用户不会在浏览器地址栏中看到更改的 URL.目标页面不可添加书签.如果可以,我建议您改用
.
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";
}
另见:
- 如何在 JSF 中导航?如何使 URL 反映当前页面(而不是前一个页面)
- 我什么时候应该使用 h:outputLink 而不是 h:commandLink?
这篇关于JSF 重定向到其他页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!