我正在尝试使用命令按钮转发托管 bean 中的页面:
<h:commandButton action="#{bean.action}" value="Go to another page" />
以下行:
public void action() throws IOException {
FacesContext.getCurrentInstance().getExternalContext().redirect("another.xhtml");
}
重定向页面,而不是转发。我见过类似的问题并尝试了给定的解决方案:
public void action() throws IOException {
FacesContext.getCurrentInstance().getExternalContext().dispatch("another.xhtml");
}
但我收到以下错误:
Index: 0, Size: 0
那么如何从托管 bean 转发到页面呢?
只需将其作为操作方法返回值返回即可。
public String action() {
return "another.xhtml";
}
如果您除了导航之外不做任何其他事情,那么您也可以直接将字符串结果放入action
属性。
<h:commandButton action="another.xhtml" value="Go to another page" />
然而,这又是一种相当糟糕的做法。您不应该为简单的页面到页面导航执行 POST 请求。只需使用一个简单的按钮或链接:
<h:button outcome="another.xhtml" value="Go to another page" />
也可以看看:
- h:button 和 h:commandButton 之间的区别 https://stackoverflow.com/questions/13070537/difference-between-hbutton-and-hcommandbutton/
- 重定向和导航/转发之间有什么区别以及何时使用什么? https://stackoverflow.com/questions/11277366/what-is-the-difference-between-redirect-and-navigation-forward-and-when-to-use-w
- 如何在 JSF 中导航?如何使 URL 反映当前页面(而不是上一页) https://stackoverflow.com/questions/15521451/how-to-make-url-reflect-the-current-page-and-not-the-previous-one
- ExternalContext#dispatch() 不起作用 https://stackoverflow.com/questions/15108896/externalcontextdispatch-doesnt-work/
- JSF 隐式导航与显式导航 https://stackoverflow.com/questions/18037884/jsf-implicit-vs-explicit-navigation/
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)