---
title: "关于struts.xml中的namespace的一些疑问"
author: "wcf1987"
date: "2009-08-19T22:47:47Z"
tags: ["Blog", "JSP", "MySpace", "Struts", "XML"]
slug: "struts-xml-namespace"
draft: false
lastmod: "2009-08-19T23:00:49Z"
id: 453192
url: "https://wcf1987.iteye.com/blog/453192"
---

根据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](http://localhost:8080/StrutsTest/RefLink/deap/login.action)，这样的url，我的struts.xml中：

 

&lt;package name="TestNamespace2" extends="struts-default" namespace=""&gt;  
      &lt;action name="login" class="com.struts2.test.LoginDefault"&gt;  
           &lt;result name="got"&gt;/login.jsp&lt;/result&gt;  
      &lt;/action&gt;  
    &lt;/package&gt;  
     
    
     &lt;package name="TestNamespace4" extends="struts-default" namespace="/RefLink"&gt;  
      &lt;action name="login" class="com.struts2.test.LoginTest"&gt;  
           &lt;result name="got"&gt;/login.jsp&lt;/result&gt;  
      &lt;/action&gt;  
    &lt;/package&gt;  
     
     &lt;package name="TestNamespace3" extends="struts-default" namespace="/RefLink/deap/leaf"&gt;  
      &lt;action name="login" class="com.struts2.test.LoginLeaf"&gt;  
           &lt;result name="test"&gt;/login.jsp&lt;/result&gt;  
      &lt;/action&gt;  
    &lt;/package&gt;

 

如果按照reference所说的应该是找不到对应的namespace后，直接查找namespace为“”的，但是我实际试验中发现它调用LoginTest，这个情况和wdl13的[http://wdl123.iteye.com/blog/340709](http://wdl123.iteye.com/blog/340709)所说的一样，难道reference错了，还是我的英语有问题

 

 

明白了，struts的寻找方法是这样的：引自wdl13的[http://wdl123.iteye.com/blog/340709](http://wdl123.iteye.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。

 

 

 

更新后的&lt;package name="TestNamespace2" extends="struts-default" namespace=""&gt;  
      &lt;action name="login" class="com.struts2.test.LoginDefault"&gt;  
           &lt;result name="got"&gt;/login.jsp&lt;/result&gt;  
      &lt;/action&gt;  
    &lt;/package&gt;  
     
    
     &lt;package name="TestNamespace4" extends="struts-default" namespace="/RefLink"&gt;  
      &lt;action name="login" class="com.struts2.test.LoginTest"&gt;  
           &lt;result name="got"&gt;/login.jsp&lt;/result&gt;  
      &lt;/action&gt;  
    &lt;/package&gt;  
    &lt;package name="TestNamespace5" extends="struts-default" namespace="/RefLink/deap"&gt;  
      &lt;action name="loginNO" class="com.struts2.test.LoginTestDeap"&gt;  
           &lt;result name="got"&gt;/login.jsp&lt;/result&gt;  
      &lt;/action&gt;  
    &lt;/package&gt;

 

这次 [http://localhost:8080/StrutsTest/RefLink/deap/login.action](http://localhost:8080/StrutsTest/RefLink/deap/login.action)，直接在namespace="/RefLink/deap"下没有找到login.action，就不再是往/RefLink下找action了，而是直接去了namespace=""， 调用了com.struts2.test.LoginDefault。