Struts 表单验证不根据单击的按钮执行验证
我有一个在 struts 上运行的 j2ee Web 应用程序。我正在使用验证 XML 进行表单验证。我在一种表单中使用多个按钮时遇到问题。我有保存、删除和取消按钮。问题是我希望当单击删除按钮时不执行验证。我该怎么做呢。
I have a j2ee web application running on struts. I am using validation XMLs for my form validation. I have a problem with multiple buttons in One form. I have Save, Delete and Cancel buttons. The problem is I want that when the delete button is clicked the validation is not performed. How do I do this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
尚未在 Struts 中使用太多验证框架,但您可以通过覆盖操作表单中的 validate() 方法来实现此目的。
org.apache.struts.validator.ValidatorForm 是 Validator 插件的 Struts 入口点。 ValidatorForm 重写 ActionForm 的 validate() 方法并将验证委托给 Validator。
如果您覆盖表单中的 validate() 方法,并且仅在需要时调用 ValidatorForm 中的方法,则可能会跳过对所需按钮的验证,如下所示:
Haven't used the Validation framework that much with Struts, but you might do this with overwriting the validate() method in your action form.
The org.apache.struts.validator.ValidatorForm is the Struts entry point into the Validator plugin. The ValidatorForm overrides the validate() method of the ActionForm and delegates validation to the Validator.
You might skip validation for your desired button if you overwrite the validate() method in your form and only call the one in ValidatorForm if needed, something like:
我也对这个限制感到沮丧。我通过在表单内创建一个输入按钮,并使用 url 标签作为设置浏览器位置的操作,提出了“取消”类型的功能。这样,表单上的验证只会在提交时触发,但不会在我们特殊的取消按钮时触发。当两个按钮均为
submit
类型时,我无法使其跳过对一个按钮或另一个按钮的验证。使用此解决方案,客户端验证也将正常工作。I was also frustrated by this limitation. I came up with "cancel" type functionality by creating an input button inside the form, with a url tag as the action to set the browser location to. That way the validation on the form will only fire for submit, but not for our special cancel button. I was not able to make it skip validation for one button or the other when both were of type
submit
. With this solution the client side validation will work properly as well.