找到你要的答案

Q:When i write default action in my struts.xml an error is occured in struts

Q:当我写默认的行为在我的错误发生在Struts Struts.xml

This is my code

struts.xml

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

         <!-- ********************************************************** -->   

        <struts>

         <!-- ********************************************************** -->

            <constant name="struts.enable.DynamicMethodInvocation" value="false" />
            <constant name="struts.devMode" value="true" />

         <!-- ********************************************************** -->

            <package name="default" extends="struts-default">

        <!-- *****************DEFAULT ACTION STARTS********************* -->        
                <default-action-ref name="somedefault" />

                <!-- ******************************* --> 

                <action name="somedefault">
                <result>/jsp/DefaultPage.jsp</result>
                </action>


        <!-- *****************DEFAULT ACTION ENDS********************* -->      

        <!-- *****************GLOBAL RESULTS STARTS********************* -->

                <global-results>

                      <result name="null">/jsp/Fail1.jsp</result>
                      <result name="excep">/jsp/Fail2.jsp</result>

                </global-results>

        <!-- ******************GLOBAL RESULTS ENDS********************** -->        

        <!-- *************GLOBAL EXCEPTION MAPPING STARTS*************** -->    

                <global-exception-mappings>

                      <exception-mapping result="null" exception="java.lang.NullPointerException" />              
                      <exception-mapping result="excep" exception="java.lang.Exception" />

                </global-exception-mappings>

        <!-- **************GLOBAL EXCEPTION MAPPING ENDS***************** -->   

        <!-- ******************OTHER ACTION STARTS*********************** -->

                <action name="Calculator" class="com.action.Calculator">

                    <exception-mapping result="error" exception="java.lang.NumberFormatException" />
                    <exception-mapping result="none" exception="java.lang.NullPointerException" />       

                <result name="success">/jsp/Success.jsp</result>
                <result name="error">/jsp/Error.jsp</result>
                <result name="none">/jsp/Error1.jsp</result>

            </action>

                <!-- ************************************ -->       

                <action name="Action1" class="com.action.Action1">

                <result name="success">/jsp/Success1.jsp</result>

            </action>

                <!-- ************************************ -->

            <action name="Action2" class="com.action.Action2">

                <result name="success">/jsp/Success1.jsp</result>

            </action>       

        <!-- *********************OTHER ACTION ENDS********************** -->   

            </package>

        <!-- ************************************************************ -->   

        </struts>

I dont know whts happening an error the error shows The content of element type "package" must match "(result-types?,interceptors?,default-interceptor-ref?,default-action-ref?,default-class-ref?,global-results?,global- exception-mappings?,action*)". Please correct me

这是我的密码

Struts.xml

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

         <!-- ********************************************************** -->   

        <struts>

         <!-- ********************************************************** -->

            <constant name="struts.enable.DynamicMethodInvocation" value="false" />
            <constant name="struts.devMode" value="true" />

         <!-- ********************************************************** -->

            <package name="default" extends="struts-default">

        <!-- *****************DEFAULT ACTION STARTS********************* -->        
                <default-action-ref name="somedefault" />

                <!-- ******************************* --> 

                <action name="somedefault">
                <result>/jsp/DefaultPage.jsp</result>
                </action>


        <!-- *****************DEFAULT ACTION ENDS********************* -->      

        <!-- *****************GLOBAL RESULTS STARTS********************* -->

                <global-results>

                      <result name="null">/jsp/Fail1.jsp</result>
                      <result name="excep">/jsp/Fail2.jsp</result>

                </global-results>

        <!-- ******************GLOBAL RESULTS ENDS********************** -->        

        <!-- *************GLOBAL EXCEPTION MAPPING STARTS*************** -->    

                <global-exception-mappings>

                      <exception-mapping result="null" exception="java.lang.NullPointerException" />              
                      <exception-mapping result="excep" exception="java.lang.Exception" />

                </global-exception-mappings>

        <!-- **************GLOBAL EXCEPTION MAPPING ENDS***************** -->   

        <!-- ******************OTHER ACTION STARTS*********************** -->

                <action name="Calculator" class="com.action.Calculator">

                    <exception-mapping result="error" exception="java.lang.NumberFormatException" />
                    <exception-mapping result="none" exception="java.lang.NullPointerException" />       

                <result name="success">/jsp/Success.jsp</result>
                <result name="error">/jsp/Error.jsp</result>
                <result name="none">/jsp/Error1.jsp</result>

            </action>

                <!-- ************************************ -->       

                <action name="Action1" class="com.action.Action1">

                <result name="success">/jsp/Success1.jsp</result>

            </action>

                <!-- ************************************ -->

            <action name="Action2" class="com.action.Action2">

                <result name="success">/jsp/Success1.jsp</result>

            </action>       

        <!-- *********************OTHER ACTION ENDS********************** -->   

            </package>

        <!-- ************************************************************ -->   

        </struts>

I dont know whts happening an error the error shows The content of element type "package" must match "(result-types?,interceptors?,default-interceptor-ref?,default-action-ref?,default-class-ref?,global-results?,global- exception-mappings?,action*)". Please correct me

answer1: 回答1:

Move the definition of your default action to the section of the spec where the other actions are defined. I've removed the comments and reindented so that you can see the XML elements more clearly.

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
    "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
    <package name="default" extends="struts-default">
        <default-action-ref name="somedefault" />
        <global-results>
            <result name="null">/jsp/Fail1.jsp</result>
            <result name="excep">/jsp/Fail2.jsp</result>
        </global-results>
        <global-exception-mappings>
            <exception-mapping result="null" exception="java.lang.NullPointerException" />          
            <exception-mapping result="excep" exception="java.lang.Exception" />
        </global-exception-mappings>
        <action name="somedefault">
            <result>/jsp/DefaultPage.jsp</result>
        </action>
        <action name="Calculator" class="com.action.Calculator">
            <exception-mapping result="error" exception="java.lang.NumberFormatException" />
            <exception-mapping result="none" exception="java.lang.NullPointerException" />       
            <result name="success">/jsp/Success.jsp</result>
            <result name="error">/jsp/Error.jsp</result>
            <result name="none">/jsp/Error1.jsp</result>
        </action>
        <action name="Action1" class="com.action.Action1">
            <result name="success">/jsp/Success1.jsp</result>
        </action>
        <action name="Action2" class="com.action.Action2">
            <result name="success">/jsp/Success1.jsp</result>
        </action>       
    </package>
</struts>

将默认动作的定义移动到其他动作定义的规范部分。我已经删除了评论和reindented这样你可以看到XML元素更清晰。

<?xml version="1.0" encoding="UTF-8" ?>
<!DOCTYPE struts PUBLIC
    "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN"
    "http://struts.apache.org/dtds/struts-2.1.dtd">
<struts>
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
    <package name="default" extends="struts-default">
        <default-action-ref name="somedefault" />
        <global-results>
            <result name="null">/jsp/Fail1.jsp</result>
            <result name="excep">/jsp/Fail2.jsp</result>
        </global-results>
        <global-exception-mappings>
            <exception-mapping result="null" exception="java.lang.NullPointerException" />          
            <exception-mapping result="excep" exception="java.lang.Exception" />
        </global-exception-mappings>
        <action name="somedefault">
            <result>/jsp/DefaultPage.jsp</result>
        </action>
        <action name="Calculator" class="com.action.Calculator">
            <exception-mapping result="error" exception="java.lang.NumberFormatException" />
            <exception-mapping result="none" exception="java.lang.NullPointerException" />       
            <result name="success">/jsp/Success.jsp</result>
            <result name="error">/jsp/Error.jsp</result>
            <result name="none">/jsp/Error1.jsp</result>
        </action>
        <action name="Action1" class="com.action.Action1">
            <result name="success">/jsp/Success1.jsp</result>
        </action>
        <action name="Action2" class="com.action.Action2">
            <result name="success">/jsp/Success1.jsp</result>
        </action>       
    </package>
</struts>
struts2  struts2-interceptors