今天遇到一个有关struts的挺郁闷的问题。
因为想让struts工作自己独立的一个路径contextPath/s/下面,所以我的web.xml配置文件如下: <filter>
<filter-name>struts2</filter-name>
<filter-class>
org.apache.struts2.dispatcher.ng.filter.StrutsPrepareAndExecuteFilter
</filter-class>
</filter>
<filter-mapping>
<filter-name>struts2</filter-name>
<url-pattern>/s/*</url-pattern>
</filter-mapping>
struts.xml配置文件如下: <package name="example" namespace="/example" extends="struts-default">
<action name="index" class="com.ExampleAction"> </action>
</package>
运行起来访问路径:contextPath/s/admin/index
竟然报出404错误,让我很费解。我的理解是:
这样配置后,struts2工作在contextPath/s/*的路径下面,只有关于这个路径下的请求,才会被struts2处理。
如果我把package的namespace="/example" 去掉,
访问contextPath/s/index,是正确,可是为什么不能加namespace呢?奇怪!

解决方案 »

  1.   

    表单提交时要指明提交的命名空间 namespace="/example". 默认为 namespace = "/" .
      

  2.   

    如果你加上namespace="/example"时,访问路径应该是:contextPath/s/example/index
      

  3.   

    contextPath/s/admin/index
    当struts2接收到这个请求后,会先去找namesapce为admin的package,
    1,如果admin存在,则打它下面名为index的action
    2,如果admin不存在,则去找默认的package下面为index的action当你把namespace="/example"去掉后,struts2会分配一个默认的namesapce,
    这个时候访问能匹配第二条但是你加了namespace="/example"后,两条都匹配不到,所有找不到了试试这样:
    contextPath/s/example/index
      

  4.   

    这样才是正确的访问路径,路径前面要加上namespace
      

  5.   

    如果你加上namespace="/example"时,访问路径应该是:contextPath/s/example/index
      

  6.   

    我的问题里描述有问题:我访问的路径就是:contextPath/s/example/index就是报404错误!!
      

  7.   


    我的问题描述错了我访问的路径就是:contextPath/s/example/index这样,就是访问时报404错误!,真奇怪!
      

  8.   

    以此回复作为结贴。很抱歉我的问题里描述错误了,
    将   contextPath/s/example/index   写成了    contextPath/s/admin/index
    我本来是想说访问  contextPath/s/example/index   报404错误的。大家都在纠正我,说访问   contextPath/s/example/index   就没有问题。但我这边要告诉大家,这样访问还是404!!---------------------------------------------
    修改package的namespace属性为 /s/example 后访问 contextPath/s/example/index 就没问题。
    个人感觉这样不合理。有时间看看源码再找出原因,写个博客!