What would be the flow order if I include multiple struts config files in the project

I am using Struts2. Below is my action class ( TutorialAction

).

public class TutorialAction {
    public String execute() {
        System.out.println("Hello from Execute!");
        return "failure";

    }
}

      

I return "failure"

in the execution method of this Action class.

Below are my files with two lines:

======================== 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>    
        <package name="default" namespace="/tutorials" extends="struts-default">        
            <action name="getTutorial" class="com.tushar.action.TutorialAction">
                <result name="failure">/ErrorPage.jsp</result>        
            </action>       
        </package>    
        <include file="struts2.xml"></include> 
    </struts>

      

In the above config file, I include another struts ( struts2.xml

) config file for the same namespace:

======================= struts2.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" namespace="/tutorials" extends="struts-default">

        <action name="getTutorial" class="com.tushar.action.TutorialAction">
            <result name="failure">/SuccessPage.jsp</result>        
        </action>
    </package>
</struts>

      

My project is working fine. I'm just curious to know if the included file is running in struts.xml

(which is struts2.xml

) after the main one struts.xml

or before?

Or what will be output: /SuccessPage.jsp

or /ErrorPage.jsp

?

+3


source to share


2 answers


The Struts configuration is generated after the XML documents have been parsed when your application starts up. It then uses configuration properties to map actions under their namespaces. This mapping is created by iterating over all packages that are also a map. If you have the same namespace in other packages, then the latter will override the previous mapping. You should be aware that iterating over the map does not guarantee the order of the retrieved elements. See HashMap .

Thus, the order in which the namespace mapping is created is not guaranteed, and that the namespace will only contain the actions that were last entered by the iterator. The namespace to action mapping is used when Struts2 gets the action configuration from the action mapping (at the time the action proxy was created) generated after parsing the URL. It then continues if such a configuration file is found. The results are matched against the action and you have no results with the same name.



Hope this is easy to understand. If you have the same namespace and the same action name and the same package name, which I doubt is not possible, such a configuration cannot be used and may lead to unpredictable results. It doesn't matter in what order the packages are created. Note that order is important if you have dependencies between packages not present in your case.

+2


source


If you have struts2 config like this.

   <struts>    
        <package name="default" namespace="/tutorials" extends="struts-default">        
            <action name="getTutorial" class="com.tushar.action.TutorialAction">
                <result name="failure">/ErrorPage.jsp</result>        
            </action>       
        </package>    
        <include file="struts-module2.xml"></include> 
    </struts>

      

or

    <struts>    
      <include file="struts-module1.xml">   
        <include file="struts-module2.xml"></include> 
    </struts>

      



and according to the Apache Struts 2 Web 2.0 Practical Projects .

Order is very important when including files . Dependencies between included files are not automatically detected and resolved , so if struts-module1.xml depends on the configuration provided in struts-module2.xml (and struts-module2.xml is configured after struts-module1.xml), an exception is thrown. the solution is to change the file that depends on the dependent configuration contained within or to change the order of the included files.

But since you have the same url as /getTutorial

, the last one you configured always wins because you are rewriting your definitions. So the first one will be useless, you must provide a different name if you want to use both.

+1


source







All Articles