`

关于struts.xml中的namespace的一些疑问

阅读更多

     根据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.iteye.com/blog/340709所说的一样,难道reference错了,还是我的英语有问题

 

 

明白了,struts的寻找方法是这样的:引自wdl13的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。

 

 

 

更新后的<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。

分享到:
评论

相关推荐

    一个struts2的例子:彻底解决STRUTS2 错误There is no Action mapped for namespace / and action name login

    再声讨下struts2,为什么要在框架里面加载一个死的struts.xml,难得不能想struts1 一样的在web.xml里面配置吗? 看网上都催struts2比struts1 整的整的好,我看就这个问题,完全可以算struts2是个垃圾 我现在把完整...

    Struts2的struts.xml配置详细介绍

    配置常量,可以改变Struts 2框架的一些行为 name属性表示常量名称,value属性表示常量值 package元素: 包的作用:简化维护工作,提高重用性 包可以“继承”已定义的包,并可以添加自己包的配置 name属性为必须去且...

    struts2课件

    struts2课件 很好的struts2当输入login.jsp访问jsp页面填写完相关信息并提交给login.action时,它会首先被在web.xml中配置的过滤器监听到,过滤器会去查找strust.xml文件,并结合namespace查找名为login的action,...

    struts2.0扫盲文档

    struts2.0扫盲文档2加入struts.xml配置文件(自己建) &lt;?xml version="1.0" encoding="UTF-8" ?&gt; &lt;!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" ...

    struts-2.3.4.1所需的jar文件

    接触新版本出了问题后,解决了把jar文件上传上来供大家分享, 一共9个jar文件,我试过没问题 ...&lt;package name="default" namespace="/" extends="struts-default"&gt; /helloWorld.jsp &lt;/struts&gt;

    struts2示例程序

    struts.xml &lt;?xml version="1.0" encoding="UTF-8" ?&gt; &lt;!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.1//EN" "http://struts.apache.org/dtds/struts-2.1.dtd"&gt; ...

    struts2配置2.5版

    &lt;package name="default" namespace="/" extends="struts-default"&gt; &lt;action name="helloworld" class="com.mytest.HelloWorldAction"&gt; &lt;result&gt; /result.jsp &lt;package name="LoginForm" extends="...

    ssh整合配置文档

    ssh 整合所有配置文件的配置 各个标签如何填写 Struts2.xml配置文档 ... &lt;package name="mystruts" extends="struts-default" namespace="/"&gt; &lt;result&gt;&lt;/result&gt; &lt;/struts&gt; struts2在web.xml里的配置

    Struts2 2.3.16_doc

    This result uses the ActionMapper provided by the ActionMapperFactory to redirect the browser to a URL that invokes the specified action and (optional) namespace. This is better than the ...

    struts项目搭建

    &lt;package name="" namespace="/" extends="struts-default"&gt; &lt;action name="helloWorld" class="com.top.web.HelloWorldAction"&gt; &lt;result&gt;welcome.jsp &lt;/struts&gt; 在webroot目录下新建JSP页面,名字:welcome....

    struts2注解详细说明

    需要在你项目的struts.xml中添加如下配置    name="struts.convention.action.suffix" value="Controller"/&gt;   name="struts.convention.action.mapAllMatches" value="true"/&gt;   name="struts.convention....

    最新Struts2+jq+ajax+json 学会总要4步‵‵超级简单,里面包含实例

    2. 配置struts.xml &lt;?xml version="1.0" encoding="UTF-8" ?&gt; &lt;!DOCTYPE struts PUBLIC "-//Apache Software Foundation//DTD Struts Configuration 2.0//EN" ...

    Struts2+Spring3+MyBatis3完整实例

    - Parsing configuration file [struts.xml] - Initializing Struts-Spring integration... - Setting autowire strategy to name - ... initialized Struts-Spring integration successfully - Initializing Spring...

    web页面模块化异步渲染struts-gpipe.zip

    2,struts.xml配置 name="gweb.groovy.dir" value="biz" /&gt;  name="gweb" namespace="/index" extends="gpipe-default"&gt;   name="index" class="com.gweb.front.action.Index"&gt;   name="success" type...

    jfreechar 整合struts2.1.8版本生成线图,饼图,柱形图

    -- include节点是struts2中组件化的方式 可以将每个功能模块独立到一个xml配置文件中 然后用include节点引用 --&gt; &lt;include file="struts-default.xml"&gt; &lt;!-- package提供了将多个Action组织为一个模块的方式 ...

    解决struts2下载异常的jar包 struts2-sunspoter-stream-1.0.jar

    &lt;package name="default" namespace="/" extends="struts-default"&gt; &lt;!-- 添加如下内容 --&gt; &lt;result-types&gt; &lt;result-type name="streamx" class="com.sunspoter.lib.web.struts2.dispatcher.StreamResultX"/&gt; ...

    struts2实例 学生信息管理系统

    &lt;package name="Student_CRUD_DEMO" extends="struts-default" namespace="/jsp"&gt; &lt;action name="add" class="cn.fjnu.edu.action.AddAction" method="addStu"&gt; &lt;result name="success"&gt;list.jsp ...

    Struts升级到Struts2.3.35

    该漏洞可能在两种情况下被触发,第一,当没有为底层xml配置中定义的结果设置namespace 值,并且其上层动作集配置没有或只有通配符命名空间值,可能构成 RCE攻击。第二,当使用没有 value和动作集的url标签时,并且其...

Global site tag (gtag.js) - Google Analytics