首页 > 代码库 > Struts2 默认Action和模块包含

Struts2 默认Action和模块包含

在我们定义Action的时候会希望有一个默认的action在写错action时或者不写action时不至于报错

这里我只写一个简单的strut.xml例子

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"    "http://struts.apache.org/dtds/struts-2.3.dtd"><struts>    <!-- Add packages here -->     <constant name="struts.devMode" value="http://www.mamicode.com/true" />    <constant name="struts.enable.DynamicMethodInvocation" value="http://www.mamicode.com/true"/>    <package name="default" namespace="/abc" extends="struts-default">             <default-action-ref name="index"></default-action-ref>                <action name="index" class="com.pengli.struts.parampack.MyActionOne"  method="Def">               <result>                   /index.jsp               </result>       </action>    </package></struts>

除了这个也可以用全局的

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"    "http://struts.apache.org/dtds/struts-2.3.dtd"><struts>    <!-- Add packages here -->     <constant name="struts.devMode" value="http://www.mamicode.com/true" />    <constant name="struts.enable.DynamicMethodInvocation" value="http://www.mamicode.com/true"/>        <!-- Add packages here -->           <package name="defalutError" namespace="/" extends="struts-default">        <global-results>            <result name="error">                /Errors.jsp            </result>        </global-results>    </package>        <package name="testdefalult" namespace="/abc" extends="defalutError">                                      <action name="index" class="com.pengli.struts.parampack.MyActionOne"  method="Def">               <result >                   /index.jsp               </result>       </action>    </package>        </struts>

 

有时候我们也是需要模块化struts.xml在多人开发时更加灵活

我们再建一个xml文档

struts.xml代码

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"    "http://struts.apache.org/dtds/struts-2.3.dtd"><struts>    <!-- Add packages here -->     <constant name="struts.devMode" value="http://www.mamicode.com/true" />    <constant name="struts.enable.DynamicMethodInvocation" value="http://www.mamicode.com/true"/>        <include file="testModel.xml"></include></struts>

testModel.xml

<?xml version="1.0" encoding="UTF-8" ?><!DOCTYPE struts PUBLIC    "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"    "http://struts.apache.org/dtds/struts-2.3.dtd"><struts>    <!-- Add packages here -->    <package name="testdefalult" namespace="/abc" extends="struts-default">             <default-action-ref name="index"></default-action-ref>                <action name="index" class="com.pengli.struts.parampack.MyActionOne"  method="Def">               <result>                   /index.jsp               </result>       </action>    </package></struts>