您的位置:首页 > 编程语言 > Java开发

关于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。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: