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

我们两清 提交于 2020-05-13 07:16:59

问题


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 am returning "failure" in execute method of this Action class.

Below are my 2 struts config files :

======================== 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 above config file I am including another struts config file(struts2.xml) for 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 running fine. I am just curious to know if included file in struts.xml (which is struts2.xml) is run after main struts.xml or before ?

Or what would be the output: /SuccessPage.jsp or /ErrorPage.jsp?


回答1:


Struts configuration is built after xml documents has been parsed on the start up of your application. Then it uses configuration properties to map actions under their namespaces. This mapping is created via iterating all packages which is also a map. If you have the same namespace in other packages then the last will override the previous mapping. You should know that iterating a map doesn't guarantee the order of the retrieved elements. See HashMap.

So, the order in which the namespace mapping is created is not guaranteed and that namespace will only contain those actions put by the iterator at last time. The namespace to actions mapping is used when Struts2 is getting action config from the action mapping (at the time it's creating an action proxy) created after parsing an URL. Then it continues if such action config is found. The results are mapped to an action, and you don't have results with the same name.

Hope it's easy to understand. If you have the same namespace and the same action name, and the same package name which I doubt impossible, such configuration can't be used and might lead to unpredictable results. And this is not important in which order the packages are created. Note the order is important if you have dependency between packages that are absent in your case.




回答2:


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 Practical Apache Struts 2 Web 2.0 Projects.

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

But since you have same url which is /getTutorial the last one you've configured always win because you're overwriting your definitions. So the the first one will be useless, you should give another name if you want to use both.



来源:https://stackoverflow.com/questions/25820395/what-would-be-the-flow-order-if-i-include-multiple-struts-config-file-in-the-pro

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!