Struts 2 REST 和验证

发布于 2024-08-15 11:38:44 字数 252 浏览 2 评论 0原文

我正在使用 Struts 2 和 REST 插件。 Struts 2 中的验证是 ClassName-actionAlias-validation.xml。但是,使用 REST 插件,操作别名始终为 /。例如 OrdersController -> /订单-> OrdersController-orders-validation.xml。 如何根据 REST 方法获得不同的验证?主要是,我想要对 update() 方法进行一种验证,对 create() 方法进行另一种验证。

I'm using Struts 2 and the REST plugin. Validation in Struts 2 is ClassName-actionAlias-validation.xml. However, using the REST plugin the action alias is always /. For example OrdersController -> /orders -> OrdersController-orders-validation.xml.
How can I get different validations depending on the REST method? Mainly, I want one type of validation for the update() method and another for the create() method.

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

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

发布评论

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

评论(2

动听の歌 2024-08-22 11:38:44

您是否考虑过使用注释进行验证?这使得将它们与正确的方法联系起来变得更简单。

Have you considered using annotations for the validations? That makes it simpler to tie them to the correct method.

灵芸 2024-08-22 11:38:44

啊,我不知道您可以在特定方法上使用基于注释的验证。我没怎么用过Struts。谢谢!

但是,我确实必须修改 struts.xml 以允许对不同方法进行不同的验证(validateAnnotatedMethodOnly 必须为 true)。它看起来是这样的:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <constant name="struts.convention.action.suffix" value="Controller" />
    <constant name="struts.convention.action.mapAllMatches" value="true" />
    <!-- Set to "default" instead of "rest-default" -->
    <constant name="struts.convention.default.parent.package"
        value="default" />

    <constant name="struts.convention.package.locators" value="rest" />

    <package name="default" extends="rest-default">
        <interceptors>
            <interceptor-stack name="restDefaultStack">
                <interceptor-ref name="exception" />
                <interceptor-ref name="alias" />
                <interceptor-ref name="servletConfig" />
                <interceptor-ref name="messages">
                    <param name="operationMode">AUTOMATIC</param>
                </interceptor-ref>
                <interceptor-ref name="prepare" />
                <interceptor-ref name="i18n" />
                <interceptor-ref name="chain" />
                <interceptor-ref name="debugging" />
                <interceptor-ref name="profiling" />
                <interceptor-ref name="actionMappingParams" />
                <interceptor-ref name="scopedModelDriven" />
                <interceptor-ref name="modelDriven">
                    <param name="refreshModelBeforeResult">true</param>
                </interceptor-ref>
                <interceptor-ref name="fileUpload" />
                <interceptor-ref name="checkbox" />
                <interceptor-ref name="staticParams" />
                <interceptor-ref name="params">
                    <param name="excludeParams">dojo\..*</param>
                </interceptor-ref>
                <interceptor-ref name="rest" />
                <interceptor-ref name="conversionError" />
                <interceptor-ref name="validation">
                    <param name="excludeMethods">input,back,cancel,browse,index</param>
                    <!-- Modified! -->
                    <param name="validateAnnotatedMethodOnly">true</param>
                </interceptor-ref>
                <interceptor-ref name="restWorkflow">
                    <param name="excludeMethods">input,back,cancel,browse,index</param>
                </interceptor-ref>
            </interceptor-stack>

        </interceptors>
    </package>
</struts>

Ahh, I didn't know that you could use annotation based validation on a specific method. I haven't used Struts that much. Thanks!

However, I did have to modify my struts.xml to allow different validations on different methods (validateAnnotatedMethodOnly had to be true). Here is what it looks like:

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN"
    "http://struts.apache.org/dtds/struts-2.0.dtd">
<struts>
    <constant name="struts.convention.action.suffix" value="Controller" />
    <constant name="struts.convention.action.mapAllMatches" value="true" />
    <!-- Set to "default" instead of "rest-default" -->
    <constant name="struts.convention.default.parent.package"
        value="default" />

    <constant name="struts.convention.package.locators" value="rest" />

    <package name="default" extends="rest-default">
        <interceptors>
            <interceptor-stack name="restDefaultStack">
                <interceptor-ref name="exception" />
                <interceptor-ref name="alias" />
                <interceptor-ref name="servletConfig" />
                <interceptor-ref name="messages">
                    <param name="operationMode">AUTOMATIC</param>
                </interceptor-ref>
                <interceptor-ref name="prepare" />
                <interceptor-ref name="i18n" />
                <interceptor-ref name="chain" />
                <interceptor-ref name="debugging" />
                <interceptor-ref name="profiling" />
                <interceptor-ref name="actionMappingParams" />
                <interceptor-ref name="scopedModelDriven" />
                <interceptor-ref name="modelDriven">
                    <param name="refreshModelBeforeResult">true</param>
                </interceptor-ref>
                <interceptor-ref name="fileUpload" />
                <interceptor-ref name="checkbox" />
                <interceptor-ref name="staticParams" />
                <interceptor-ref name="params">
                    <param name="excludeParams">dojo\..*</param>
                </interceptor-ref>
                <interceptor-ref name="rest" />
                <interceptor-ref name="conversionError" />
                <interceptor-ref name="validation">
                    <param name="excludeMethods">input,back,cancel,browse,index</param>
                    <!-- Modified! -->
                    <param name="validateAnnotatedMethodOnly">true</param>
                </interceptor-ref>
                <interceptor-ref name="restWorkflow">
                    <param name="excludeMethods">input,back,cancel,browse,index</param>
                </interceptor-ref>
            </interceptor-stack>

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