Struts2.5 添加配置信息报错

December 14, 2016

问题:

Struts2.5 添加<global-allowed-methods>regex:.*</global-allowed-methods>配置信息报错,错误信息如下:

错误信息:
The content of element type "package" must match "(result-types?,interceptors?,default-interceptor-ref?,default-action-ref?,default-class-ref?,global-results?,global-allowed-methods?,global-exception-mappings?,action*)".
源码:

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
 <!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">
<struts>

    <constant name="struts.action.extension" value="do,action" />
    <!-- 设置文件上传大小限制为100m -->
    <constant name="struts.multipart.maxSize" value="104857600" />

    <package name="publiz" extends="json-default">
        <global-allowed-methods>regex:.*</global-allowed-methods>

        <interceptors>
            <!-- 系统的默认拦截器 -->
            <interceptor-stack name="default">
                <!-- json -->
                <interceptor-ref name="json" />
                <!-- struts2 default -->
                <interceptor-ref name="defaultStack" />
            </interceptor-stack>
        </interceptors>

        <!-- 定义全局默认拦截器 -->
        <default-interceptor-ref name="default" />
        <default-action-ref name="notFound" />

        <!-- ========= 4.action ======== -->

        <action name="json_*_*" class="{1}Action" method="{2}">
            <result name="success" type="json"></result>
            <result name="error" type="json"></result>
            <result name="input" type="json"></result>
        </action>

        <action name="notFound">
            <result>/404.html</result>
        </action>

    </package>

</struts>

分析:

错误提示信息意思是package元素下只允许如下的元素,但global-allowed-methods确实为允许的元素啊,检查拼写正确,不知原因。

解决办法:

错误信息还有另一个意思,即只允许如下元素且匹配如下顺序。
所以将
<global-allowed-methods>regex:.*</global-allowed-methods>
放到action元素前面即可。
代码如下:

    <?xml version="1.0" encoding="UTF-8"?>
     <!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">
    <struts>
        <constant name="struts.action.extension" value="do,action" />
        <!-- 设置文件上传大小限制为100m -->
        <constant name="struts.multipart.maxSize" value="104857600" />
        <package name="publiz" extends="json-default">
            <interceptors>
                <!-- 系统的默认拦截器 -->
                <interceptor-stack name="default">
                    <!-- json -->
                    <interceptor-ref name="json" />
                    <!-- struts2 default -->
                    <interceptor-ref name="defaultStack" />
                </interceptor-stack>
            </interceptors>
            <!-- 定义全局默认拦截器 -->
            <default-interceptor-ref name="default" />
            <default-action-ref name="notFound" />
            <global-allowed-methods>regex:.*</global-allowed-methods>
            <!-- ========= 4.action ======== -->
            <action name="json_*_*" class="{1}Action" method="{2}">
                <result name="success" type="json"></result>
                <result name="error" type="json"></result>
                <result name="input" type="json"></result>
            </action>
            <action name="notFound">
                <result>/404.html</result>
            </action>
        </package>
    </struts>