我有一个在一个字段上带有验证器的表单.我有两个 h:commandButton:Ok 和 Cancel.当我输入错误的数据并单击取消时,我会收到一条验证消息.当我单击 cancel 时,验证器不运行,我必须做什么?
I have a form with a validator on one field. I have two h:commandButtons: Ok and Cancel. When I input wrong data and click Cancel, I get a validation message. What must I do that validator don't run when I click cancel?
推荐答案如果您没有使用 ajax,或者仍在使用 JSF 1.x,并且您确实需要在 cancel() 方法(即仅重新加载页面是不够的),那么最好的办法是将 immediate="true" 添加到按钮.这样,所有没有 immediate="true" 的输入字段将在处理中被跳过.
In case you aren't using ajax, or are still on JSF 1.x, and you really need to invoke a business action in cancel() method (i.e. just reloading the page is insufficient), then your best bet is to add immediate="true" to the button. This way all input fields which don't have immediate="true" will be skipped in processing.
<h:commandButton value="Cancel" action="#{bean.cancel}" immediate="true" />在 JSF 2.x 上,通过 提交表单更好,默认情况下它只处理 @this 而不是 @form.
On JSF 2.x, much better is to submit the form by <f:ajax>, which by default only processes @this instead of @form.
<h:commandButton value="Cancel" action="#{bean.cancel}"> <f:ajax /> </h:commandButton>如果您想在此处导航到另一个页面,请将 ?faces-redirect=true 添加到 cancel() 方法的结果中.
If you want to navigate to another page here, add ?faces-redirect=true to the outcome in the cancel() method.
或者,如果您实际上根本不需要调用任何业务操作,那么只需使用 <h:button>,您可以在其中直接指定(隐式)导航案例结果.>
Or, if you actually don't need to invoke any business action at all, then just use <h:button> wherein you directly specify the (implicit) navigation case outcome.
<h:button value="Cancel" outcome="previouspage" />这基本上会通过 GET 请求重新加载页面.<h:button> 在 JSF 1.x 中不存在,但您也可以只使用普通的 HTML+JS.
This will basically reload the page by a GET request. The <h:button> doesn't exist in JSF 1.x, but you can also just use plain HTML+JS for that.
- 为什么是立即"?添加到 EditableValueHolders 的属性?
- 如何在 JSF 中导航?如何使 URL 反映当前页面(而不是前一个页面)
- 如何让验证依赖于按下的按钮?
更多推荐
单击特定按钮时如何跳过验证?
发布评论