问题描述
我正在尝试使用命令按钮在托管bean中转发页面:
I am trying to forward a page in my managed bean with the commandbutton:
<h:commandButton action="#{bean.action}" value="Go to another page" />
以下行:
public void action() throws IOException {
FacesContext.getCurrentInstance().getExternalContext().redirect("another.xhtml");
}
重定向页面,而不转发.我看到了与此类似的问题,并尝试了给定的解决方案:
redirects the page, not forwards. I have seen a similar question to this and tried the given solution:
public void action() throws IOException {
FacesContext.getCurrentInstance().getExternalContext().dispatch("another.xhtml");
}
但是出现以下错误:
Index: 0, Size: 0
那么如何从托管bean转发到页面?
So how can I forward to a page from a managed bean?
推荐答案
只需将其作为操作方法的返回值返回即可.
Just return it as action method return value.
public String action() {
return "another.xhtml";
}
如果您除了导航之外不执行其他任何操作,那么您也可以将字符串结果直接放在action
属性中.
If you're in turn not doing anything else than navigating, then you could also just put the string outcome directly in action
attribute.
<h:commandButton action="another.xhtml" value="Go to another page" />
但是,这反过来又是一个很差的做法.您不应该为纯页面到页面的导航执行POST请求.只需使用简单的按钮或链接:
However, this is in turn a rather poor practice. You should not be performing POST requests for plain page-to-page navigation. Just use a simple button or link:
<h:button outcome="another.xhtml" value="Go to another page" />
另请参见:
- h:button和h:commandButton之间的区别
- 什么重定向和导航/转发之间的区别以及何时使用什么?
- 如何导航在JSF中?如何使URL反映当前页面(而不是上一页)
- ExternalContext#dispatch()不起作用
- JSF隐式导航与显式导航
- Difference between h:button and h:commandButton
- What is the difference between redirect and navigation/forward and when to use what?
- How to navigate in JSF? How to make URL reflect current page (and not previous one)
- ExternalContext#dispatch() doesn't work
- JSF implicit vs. explicit navigation
See also:
这篇关于如何导航到托管bean中的另一个页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!