如果发生验证错误,“取消"按钮将不起作用 [英] Cancel button doesn't work in case of validation error

查看:131
本文介绍了如果发生验证错误,“取消"按钮将不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个表单,其中的某些输入已经过验证(required = true) 并且当我在发生验证错误时单击取消"按钮时,取消"按钮不会导航到上一页,而是删除了验证错误(我认为它可以返回到发生验证错误之前的一个步骤?)

i have a form with some inputs that have validation (required=true) and when i click on the cancel button in case of validation error, the cancel button doesn't navigate to previous page, instead it removes the validation error (i think it goes back one step that was before the validation error ?)

这是我的代码:

<h:form>

<h:inputText value="#{myBean.nickName}" id="nickname" required="true"
requiredMessage="nickname should be specified" />

<h:commandLink immediate="true" id="cancel_link" onclick="history.back(); return false" style="float: left;margin: 118px 189px 0 0;">
 <h:graphicImage width="90" height="28" value="#{resource['images:blu_btnCancel.png']}" />
</h:commandLink>

</h:form>

请提出解决方法.

推荐答案

JavaScript history.back()函数将您带到上一个同步请求,而不是您所期望的上一个视图.

The JavaScript history.back() function takes you to the previous synchronous request, not to the previous view as you seemed to expect.

即使history.back()如此糟糕(不可靠,容易受到攻击等),一种快速的解决方法是在表单提交上发送ajax请求,而不是同步请求.

Even though the history.back() is terrible this way (unreliable, hackable, etc), a quick fix would be to send an ajax request on form submit instead of a synchronous request.

<h:form>
    ...
    <h:commandButton value="submit">
        <f:ajax execute="@form" render="@form" />
    </h:commandButton>
</h:form>

Ajax请求不会被视为浏览器历史记录.

Ajax requests doesn't account as browser history.

一种更可靠的方法是在导航期间仅传递前一个视图的标识符,然后使用<h:link>链接回它.例如

A more robust way is to just pass the identifier of the previous view along during navigation and then use <h:link> to link back to it. E.g.

<h:link value="Go to next view" outcome="nextview">
    <f:param name="from" value="#{view.viewId}" />
<h:link>

然后在nextview.xhtml

<f:metadata>
    <f:viewParam name="from" />
</f:metadata>

...

<h:link ... outcome="#{from}" rendered="#{not empty from}">
    <h:graphicImage ... />
</h:link>

如果要通过POST导航,则可以考虑使用Flash作用域记住初始视图.

If you're navigating by POST, you might consider using the flash scope to remember the initial view.

无关与具体问题无关,将<h:graphicImage>与JSF2资源一起使用的正确方法是只使用其name属性,而不是将其value属性与#{resource}一起使用.简直笨拙.

Unrelated to the concrete problem, the proper way to use <h:graphicImage> with JSF2 resources is to just use its name attribute instead of its value attribute with a #{resource} which is plain clumsy.

替换

<h:graphicImage width="90" height="28" value="#{resource['images:blu_btnCancel.png']}" />

作者

<h:graphicImage name="images/blu_btnCancel.png" width="90" height="28" />

请注意,库名images在这里只是由路径名代替.名称图像"作为库名的使用非常令人怀疑.另请参见>什么是JSF资源库以及如何使用它?

Note that the library name images is here just replaced by the path name. The usage of the name "images" as library name is highly questionable. See also What is the JSF resource library for and how should it be used?

这篇关于如果发生验证错误,“取消"按钮将不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆