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
?
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.