关于struts.xml中的namespace的一些疑问
2010-12-25 13:15
323 查看
根据struts reference中所说的
Namespaces are not a path!
Namespace are not hierarchical like a file system path. There is one namespace level. For example if the URL /barspace/myspace/bar.action is requested, the framework will first look for namespace /barspace/myspace. If the action does not exist at /barspace/myspace, the search will immediately fall back to the default namespace "". The framework will not parse the namespace into a series of "folders". In the Namespace Example, the bar action in the default namespace would be selected.
但是我在实践中发现如果请求http://localhost:8080/StrutsTest/RefLink/deap/login.action,这样的url,我的struts.xml中:
<package name="TestNamespace2" extends="struts-default" namespace="">
<action name="login" class="com.struts2.test.LoginDefault">
<result name="got">/login.jsp</result>
</action>
</package>
<package name="TestNamespace4" extends="struts-default" namespace="/RefLink">
<action name="login" class="com.struts2.test.LoginTest">
<result name="got">/login.jsp</result>
</action>
</package>
<package name="TestNamespace3" extends="struts-default" namespace="/RefLink/deap/leaf">
<action name="login" class="com.struts2.test.LoginLeaf">
<result name="test">/login.jsp</result>
</action>
</package>
如果按照reference所说的应该是找不到对应的namespace后,直接查找namespace为“”的,但是我实际试验中发现它调用LoginTest,这个情况和wdl13的http://wdl123.javaeye.com/blog/340709所说的一样,难道reference错了,还是我的英语有问题
明白了,struts的寻找方法是这样的:引自wdl13的http://wdl123.javaeye.com/blog/340709
1.获得请求路径的URI,例如url是:http://server/myapp/path1/path2/path3/test.action
2.首先寻找namespace为/path1/path2/path3的package,如果存在这个package,则在这个package中寻找名字为test的action,若找到则执行,否则报错;如果不存在这个package则转步骤3;
//问题的关键在这里:如果在寻找namespace的时候,没能找到符合的namespace,例如/path1/path2/path3,则一切如wdl123所写的,逐次往上目录当做namespace找,但是一旦存在/path1/path2/path3这样的namespace,那么如果在这个namespace下没能找到test.action,则他不会再逐次往上级目录匹配namespace,而是直接转向了namespace为""的去寻找action。
3.寻找namespace为/path1/path2的package,如果存在这个package,则在这个package中寻找名字为test的action,若找到则执行,否则报错;如果不存在这个package则转步骤4;
4.寻找namespace为/path1的package,如果存在这个package,则在这个package中寻找名字为test的action,若找到则执行,否则报错;
如果仍然不存在这个package,就去namaspace为空字符串的package下面去找名字为test的action,如果还是找不到,页面提示找不到action。
更新后的<package name="TestNamespace2" extends="struts-default" namespace="">
<action name="login" class="com.struts2.test.LoginDefault">
<result name="got">/login.jsp</result>
</action>
</package>
<package name="TestNamespace4" extends="struts-default" namespace="/RefLink">
<action name="login" class="com.struts2.test.LoginTest">
<result name="got">/login.jsp</result>
</action>
</package>
<package name="TestNamespace5" extends="struts-default" namespace="/RefLink/deap">
<action name="loginNO" class="com.struts2.test.LoginTestDeap">
<result name="got">/login.jsp</result>
</action>
</package>
这次 http://localhost:8080/StrutsTest/RefLink/deap/login.action,直接在namespace="/RefLink/deap"下没有找到login.action,就不再是往/RefLink下找action了,而是直接去了namespace="", 调用了com.struts2.test.LoginDefault。
Namespaces are not a path!
Namespace are not hierarchical like a file system path. There is one namespace level. For example if the URL /barspace/myspace/bar.action is requested, the framework will first look for namespace /barspace/myspace. If the action does not exist at /barspace/myspace, the search will immediately fall back to the default namespace "". The framework will not parse the namespace into a series of "folders". In the Namespace Example, the bar action in the default namespace would be selected.
但是我在实践中发现如果请求http://localhost:8080/StrutsTest/RefLink/deap/login.action,这样的url,我的struts.xml中:
<package name="TestNamespace2" extends="struts-default" namespace="">
<action name="login" class="com.struts2.test.LoginDefault">
<result name="got">/login.jsp</result>
</action>
</package>
<package name="TestNamespace4" extends="struts-default" namespace="/RefLink">
<action name="login" class="com.struts2.test.LoginTest">
<result name="got">/login.jsp</result>
</action>
</package>
<package name="TestNamespace3" extends="struts-default" namespace="/RefLink/deap/leaf">
<action name="login" class="com.struts2.test.LoginLeaf">
<result name="test">/login.jsp</result>
</action>
</package>
如果按照reference所说的应该是找不到对应的namespace后,直接查找namespace为“”的,但是我实际试验中发现它调用LoginTest,这个情况和wdl13的http://wdl123.javaeye.com/blog/340709所说的一样,难道reference错了,还是我的英语有问题
明白了,struts的寻找方法是这样的:引自wdl13的http://wdl123.javaeye.com/blog/340709
1.获得请求路径的URI,例如url是:http://server/myapp/path1/path2/path3/test.action
2.首先寻找namespace为/path1/path2/path3的package,如果存在这个package,则在这个package中寻找名字为test的action,若找到则执行,否则报错;如果不存在这个package则转步骤3;
//问题的关键在这里:如果在寻找namespace的时候,没能找到符合的namespace,例如/path1/path2/path3,则一切如wdl123所写的,逐次往上目录当做namespace找,但是一旦存在/path1/path2/path3这样的namespace,那么如果在这个namespace下没能找到test.action,则他不会再逐次往上级目录匹配namespace,而是直接转向了namespace为""的去寻找action。
3.寻找namespace为/path1/path2的package,如果存在这个package,则在这个package中寻找名字为test的action,若找到则执行,否则报错;如果不存在这个package则转步骤4;
4.寻找namespace为/path1的package,如果存在这个package,则在这个package中寻找名字为test的action,若找到则执行,否则报错;
如果仍然不存在这个package,就去namaspace为空字符串的package下面去找名字为test的action,如果还是找不到,页面提示找不到action。
更新后的<package name="TestNamespace2" extends="struts-default" namespace="">
<action name="login" class="com.struts2.test.LoginDefault">
<result name="got">/login.jsp</result>
</action>
</package>
<package name="TestNamespace4" extends="struts-default" namespace="/RefLink">
<action name="login" class="com.struts2.test.LoginTest">
<result name="got">/login.jsp</result>
</action>
</package>
<package name="TestNamespace5" extends="struts-default" namespace="/RefLink/deap">
<action name="loginNO" class="com.struts2.test.LoginTestDeap">
<result name="got">/login.jsp</result>
</action>
</package>
这次 http://localhost:8080/StrutsTest/RefLink/deap/login.action,直接在namespace="/RefLink/deap"下没有找到login.action,就不再是往/RefLink下找action了,而是直接去了namespace="", 调用了com.struts2.test.LoginDefault。
相关文章推荐
- 关于xml命名空间的——xmlns:f="http://www.name.com.cn/sub",一些疑问
- struts 中关于xml配置文件中namespace的理解
- struts.xml属性重名处理:关于<package><action><result>的name和namespace属性同名时struts2的处理
- 关于getche的一些疑问
- 关于struts fileupload拦截器的流程整理和一些要点
- 关于C#使用XML序列化的一些注意事项
- 关于jdbc链接SQLServer数据库的一些疑问
- 关于xml与dom的一些概念
- 关于图片的居中的一些疑问
- 关于Android中http请求Gosn解析的一些个人见解: 首先是XML中构建布局: 在布局里面建一个listview用来展示Gson解析的字符
- 关于eclipse中编辑struts.xml文件没有提示
- 关于SQL Server 2000对XML支持的一些见解
- 关于xml的一些知识,DTD,XSD
- struts.xml配置文件(package,namespace,action)
- 关于WiMax认证的一些疑问
- 关于using namespace 的一些体会
- 关于XML的一些基础知识
- 谁给我一些关于XML基本操作方面的材料?
- 关于socket的一些疑问
- 关于Struts2的namespace的一些总结