Struts 表单验证不根据单击的按钮执行验证

发布于 2024-08-30 00:17:21 字数 117 浏览 4 评论 0原文

我有一个在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

白云不回头 2024-09-06 00:17:21

尚未在 Struts 中使用太多验证框架,但您可以通过覆盖操作表单中的 validate() 方法来实现此目的。

org.apache.struts.validator.ValidatorForm 是 Validator 插件的 Struts 入口点。 ValidatorForm 重写 ActionForm 的 validate() 方法并将验证委托给 Validator。

如果您覆盖表单中的 validate() 方法,并且仅在需要时调用 ValidatorForm 中的方法,则可能会跳过对所需按钮的验证,如下所示:

    public ActionErrors validate(ActionMapping mapping, 
                                 HttpServletRequest request) { 
      if (/* check for clicked button*/) {
        //skip validation and just return empty (i.e. no error) 
        return new ActionErrors();
      }
      // else, delegate to the validator framework
      super.validate(mapping, request);
    }

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:

    public ActionErrors validate(ActionMapping mapping, 
                                 HttpServletRequest request) { 
      if (/* check for clicked button*/) {
        //skip validation and just return empty (i.e. no error) 
        return new ActionErrors();
      }
      // else, delegate to the validator framework
      super.validate(mapping, request);
    }
早茶月光 2024-09-06 00:17:21

我也对这个限制感到沮丧。我通过在表单内创建一个输入按钮,并使用 url 标签作为设置浏览器位置的操作,提出了“取消”类型的功能。这样,表单上的验证只会在提交时触发,但不会在我们特殊的取消按钮时触发。当两个按钮均为 submit 类型时,我无法使其跳过对一个按钮或另一个按钮的验证。使用此解决方案,客户端验证也将正常工作。

<sx:form validate="true" action="someAction">
     <sx:submit value="Save Changes" theme="simple"/> |
     <sx:url action="umhome"  id="cancel"></sx:url>
     <input type="button" value="Cancel" onclick="return window.location = '<sx:property value="%{#cancel}"/>'; "/>
</sx:form>

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.

<sx:form validate="true" action="someAction">
     <sx:submit value="Save Changes" theme="simple"/> |
     <sx:url action="umhome"  id="cancel"></sx:url>
     <input type="button" value="Cancel" onclick="return window.location = '<sx:property value="%{#cancel}"/>'; "/>
</sx:form>
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文