Struts 2 Include Multiple configuration file

Struts 2 Include Multiple configuration file

Its is very difficult to maintain large configuration file. This happens if you are working on large project on struts application where multiple module is using same configuration file. To make it simpler and easy to maintain you could use “include” functionality to separate out file multipel files based on the module and include its path to the main file. Below is demo project structure with multiple configuration file:

Struts 2 Include Multiple configuration file

Single configuration file:

  • struts.xml:
<?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>
    <include file="struts-default.xml"/>
    
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
    <constant name="struts.custom.i18n.resources" value="ApplicationResources" />
    
    <package name="default" extends="struts-default">
        <action name="">
            <result>index.jsp</result>
        </action>
       <action name="strutsAction" class="com.javahonk.action.WelcomeFileAction">
          <result name="error">index.jsp</result>
          <result name="input">index.jsp</result>
          <result name="success">index.jsp</result>          
       </action>
    </package>
    
</struts>

 

Refactor into Multiple configuration file:

  • struts.xml:
<?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>
    <include file="struts-default.xml"/>
    
    <constant name="struts.enable.DynamicMethodInvocation" value="false" />
    <constant name="struts.devMode" value="true" />
    <constant name="struts.custom.i18n.resources" value="ApplicationResources" />
    
    <include file="welcome/struts-welcome.xml"/> 
</struts>

 

  • struts-welcome.xml:
<?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>    
    <package name="default" extends="struts-default">
        <action name="">
            <result>index.jsp</result>
        </action>
       <action name="strutsAction" class="com.javahonk.action.WelcomeFileAction">
          <result name="error">index.jsp</result>
          <result name="input">index.jsp</result>
          <result name="success">index.jsp</result>          
       </action>
    </package>
</struts>

 

For more information please read this tutorial

download Download Project: StrutsMultipleConfiFile

Leave a Reply

Your email address will not be published. Required fields are marked *