如果我在项目中包含多个struts配置文件,流程顺序是什么? [英] What would be the flow order if I include multiple struts config file in the project

查看:65
本文介绍了如果我在项目中包含多个struts配置文件,流程顺序是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Struts2.以下是我的动作类(TutorialAction).

I am using Struts2. Below is my Action Class (TutorialAction).

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

    }
}

我正在此Action类的execute方法中返回"failure".

I am returning "failure" in execute method of this Action class.

下面是我的2个struts配置文件:

Below are my 2 struts config files :

======================== struts.xml ============= ==================

======================== 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>

在上面的配置文件中,我包括用于相同名称空间的另一个struts配置文件(struts2.xml):

In above config file I am including another struts config file(struts2.xml) for same namespace :

======================== struts2.xml ============= ==================

======================== 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>

我的项目运行正常.我只是想知道struts.xml(是struts2.xml)中包含的文件是否在主struts.xml之后或之前运行?

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 ?

或者输出是什么:/SuccessPage.jsp/ErrorPage.jsp?

推荐答案

在应用程序启动时已解析xml文档之后,将构建Struts配置.然后,它使用配置属性将动作映射到它们的名称空间下.通过迭代也是映射的所有包来创建此映射.如果在其他软件包中具有相同的名称空间,则最后一个软件包将覆盖先前的映射.您应该知道,迭代映射并不能保证所检索元素的顺序.请参见 HashMap .

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.

因此,不能保证创建名称空间映射的顺序,并且名称空间将仅包含迭代器最后一次执行的操作.当Struts2从解析URL之后创建的动作映射(在创建动作代理时)获取动作配置时,将使用名称空间到动作的映射.然后,如果找到了这样的操作配置,它将继续.结果将映射到操作,并且没有相同名称的结果.

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.

这篇关于如果我在项目中包含多个struts配置文件,流程顺序是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆