如何在struts2中从验证中排除操作方法

发布于 2024-09-10 19:15:34 字数 834 浏览 5 评论 0 原文

我的 Action 类具有以下方法,

1.add
2.edit
3.loadEdit
4.remove
5.list
6.execute

在此我需要对添加和编辑应用验证。如何需要在 struts.xml 中配置。我遵循,

<action name="editComment" method="edit"
        class="com.mmm.ehspreg2.web.action.product.CommentAction">
    <result name="success">/jsp/propertyManager/loadList.jsp</result>
</action>
<action name="removeComment" method="remove"
        class="com.mmm.ehspreg2.web.action.product.CommentAction">
    <interceptor-ref name="validation">
        <param name="excludeMethods">remove</param>
    </interceptor-ref>
    <result type="tiles">listComment</result>
    <result type="tiles" name="input">listComment</result>
</action>

当我这样配置它时,删除操作方法不会被调用。我不明白这个问题。请帮忙。

My Action class have the following methods,

1.add
2.edit
3.loadEdit
4.remove
5.list
6.execute

in this i need to apply validation for add and edit..how do need to config in struts.xml.I followed,

<action name="editComment" method="edit"
        class="com.mmm.ehspreg2.web.action.product.CommentAction">
    <result name="success">/jsp/propertyManager/loadList.jsp</result>
</action>
<action name="removeComment" method="remove"
        class="com.mmm.ehspreg2.web.action.product.CommentAction">
    <interceptor-ref name="validation">
        <param name="excludeMethods">remove</param>
    </interceptor-ref>
    <result type="tiles">listComment</result>
    <result type="tiles" name="input">listComment</result>
</action>

When I configure it like this, remove action method is not getting called. I don't understand the problem. Please assist.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

用心笑 2024-09-17 19:15:34

您还可以在操作类中的方法初始化之前使用@SkipValidation

,例如

@SkipValidation
public String save() {
    String result = super.save();
    if (result.equals(SAVE)) {
        setMessage1(getText("save.successful"));
    } else {
        setMessage1(getText("save.unsuccessful"));
    }
    jsonResponse = new Hashtable<String, Object>();
    jsonResponse.put(FIELD_JSONRESPONSE_STATUS,
            KEY_JSONRESPONSE_MESSAGE_SUCCESS);
    jsonResponse.put(FIELD_JSONRESPONSE_MESSAGE,
            KEY_JSONRESPONSE_EMPTY_STRING);
    jsonResponse.put(FIELD_JSONRESPONSE_VALUE, domainModel.getId());
    // System.out.println("domainModel>>>>>>" + domainModel.getId());
    return result;
}

you can also use @SkipValidation before method initialization in action class

e.g.

@SkipValidation
public String save() {
    String result = super.save();
    if (result.equals(SAVE)) {
        setMessage1(getText("save.successful"));
    } else {
        setMessage1(getText("save.unsuccessful"));
    }
    jsonResponse = new Hashtable<String, Object>();
    jsonResponse.put(FIELD_JSONRESPONSE_STATUS,
            KEY_JSONRESPONSE_MESSAGE_SUCCESS);
    jsonResponse.put(FIELD_JSONRESPONSE_MESSAGE,
            KEY_JSONRESPONSE_EMPTY_STRING);
    jsonResponse.put(FIELD_JSONRESPONSE_VALUE, domainModel.getId());
    // System.out.println("domainModel>>>>>>" + domainModel.getId());
    return result;
}
寂寞花火° 2024-09-17 19:15:34

只需在 exceptMethods 参数中列出您希望通过验证框架运行的所有方法即可。由于您只想验证 addedit,因此列出其他 4 个如下:

<interceptor-ref name="validation">
    <param name="excludeMethods">loadEdit,remove,list,execute</param>
</interceptor-ref>

您可以在 验证拦截器文档

Simply list all the methods you don't want to be run through the validation framework in the excludeMethods parameter. Since you only want add and edit validated, list the other 4 as follows:

<interceptor-ref name="validation">
    <param name="excludeMethods">loadEdit,remove,list,execute</param>
</interceptor-ref>

You can read more about it in the Validation Interceptor docs.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文