How to rearrange the Action implementation class in struts.xml?

How do I add an action implementation class to struts.xml from struts2.xml?

Suppose struts.xml looks like this:

<struts>
    <action name="EnginesList" class="example.EnginesListAction">
        <result name="*">viewengines.jsp</result>
    </action>
</struts>

      

Then I would like to achieve: when struts2.xml exists (or is not empty), then the class mapping in struts2.xml overrides them in struts.xml. So if I have the following definition, at runtime the EnginesList maps to example.EnginesListAction2 instead of example.EnginesListAction.

// struts.xml:
<struts>    
    <action name="EnginesList" class="example.EnginesListAction">
        <result name="*">viewengines.jsp</result>
    </action>
    <include file="struts2.xml" />
</struts>

// struts2.xml
<struts>    
    <action name="EnginesList" class="example.EnginesListAction2">
        <result name="*">viewengines.jsp</result>
    </action>
</struts>

      

It doesn't seem to work due to a name conflict. So what's the best way to achieve my ultimate goal?


Added background information :

  • Team-A develops and maintains struts.xml. My team (team-B) uses the team-A codebase and deploys it to a separate datacenter.
  • Command-B should display some actions for different classes. However, we cannot directly change it in struts.xml as it will change the behavior of the Team-A service.
  • So I would like to have a separate file (struts2.xml) whose class mappings can override those specified in struts.xml. There are no changes to the name of the action package. There are no changes in the JSP code. This struts2.xml will ONLY deploy to team-B datacenter.
+3


source to share


1 answer


An easy way to do this is to use multiple (separately) struts config files and import them into the main file struts.xml

.

In your case, rename struts.xml

to struts1.xml

. Add these two files to your mainstruts.xml

<include file="struts1.xml"></include>
<include file="struts2.xml"></include>

      



I'm not sure about the import order of this file, so I cannot comment if the mappings in the second file will be shadow mappings in the 1st or vice versa. Try to decide for yourself which file should be included first.

For more information about configuration is turned on, see. The this .

Refer to this for a complete example.

0


source







All Articles