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类,我如何使用另一个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";
}
另请参见:
- JSF 2.0中的通信-隐式导航
- 何时应使用h:outputLink代替h:commandLink?
- Communication in JSF 2.0 - Implicit navigation
- When should I use h:outputLink instead of h:commandLink?
See also:
这篇关于JSF重定向到其他页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!