通过commandButton进行JSF重定向 [英] JSF redirect via commandButton
问题描述
如果代码如下,则无法重定向到另一个页面:
I cannot redirect to another page if the code is like this:
<h:commandButton type="button" value="Enter" action="index?faces-redirect=true" >
但是,如果代码为:
<h:commandButton type="button" value="Enter" action="index?faces-redirect=true" >
<f:ajax />
</h:commandButton>
有人可以解释吗?谢谢!
Could anyone explain this? Thanks!
----------------------- EDIT ----------------------- -
-----------------------EDIT------------------------
完整的xhtml代码供您参考:
The entire xhtml code for your reference:
<html xmlns="http://www.w3.org/1999/xhtml"
xmlns:h="http://java.sun.com/jsf/html"
xmlns:f="http://java.sun.com/jsf/core"
xmlns:ui="http://java.sun.com/jsf/facelets"
xmlns:p="http://primefaces.org/ui">
<h:head>
</h:head>
<h:body>
<h:form id="form">
<h:commandButton id="enterModelButton" type="button" value="Enter" action="index?faces-redirect=true" >
<f:ajax />
</h:commandButton>
</h:form>
</h:body>
</html>
推荐答案
<h:commandButton type="button">
不会生成提交按钮.它只是生成一个没有任何效果的死"按钮,纯粹是用于自定义onclick="..."
脚本等.这在某种程度上是黑暗的JSF 1.0/1.1时代的遗留物,当时不可能仅在JSF中使用纯原始的HTML来处理这种事情.
The <h:commandButton type="button">
doesn't generate a submit button. It just generates a "dead" button without any effects, purely intented to be used for custom onclick="..."
scripts and like. This is somewhat a leftover of the dark JSF 1.0/1.1 era, when it wasn't nicely possible to just use plain vanilla HTML in JSF for this kind of things.
<f:ajax>
通过JSF生成的onclick
以ajax的力量执行提交.它不在乎按钮的type
.
The <f:ajax>
performs the submit by ajax powers through JSF-generated onclick
. It doesn't care about the button's type
.
本质上,删除type="button"
并依靠其默认的type="submit"
应该可以解决您的问题.
Essentially, removing type="button"
and relying on its default type="submit"
should fix your problem.
<h:commandButton value="Enter" action="index?faces-redirect=true" />
总而言之,如果这是真实的代码,而您实际上并不打算调用bean动作,那么对于实现导航到另一个页面的功能要求,您的方向就完全错误了.通过一个按钮.您应该改用<h:button>
.
However, all with all, if this is the real code and you don't actually intend to invoke a bean action, then you're going in completely the wrong direction as to implementing the functional requirement of navigating to a different page by a button. You should be using <h:button>
instead.
<h:button value="Enter" outcome="index" />