SSH框架整合之namespace下无action

      在整合ssh框架的时候,对于新手而言,总是会出现某些问题,此时我们首先想到的便是度娘,本以为度娘会给我们这些菜鸟一个满意的答案,但往往发现,找遍度娘,却没有一篇文章或一篇博文能够解决自己所遇到的问题。本人就是在整合的时候,因此头痛了两天,最终寻求大佬才得以解决。

注:Struts2  版本为2.5

        spring  版本为5.1

      问题始于Struts与spring整合时或者说是有Struts时,问题重现只需各位读者将struts.xml配置文件放到src/main/resource下面,源码放到src/main/com下面(包名称可自取),目录如下(如果下面无目录截图,读者只能看前面文字描述了,因为原来写过的一些博文图片发表后就没有了)

在web.xml中配置好Struts和spring,在Struts.xml中配置好action之后(假如action中配置了loginAction类的login方法,因为配置了spring,所以class="loginAction"),如下示

<package name="actionOne" namespace="/"  extends="struts-default">
            <!-- <global-allowed-methods>login</global-allowed-methods> -->
            <action name="loginAction" class="loginAction" method="login" >
                <result name="success">test.html</result>
            </action>
        </package>

此时将项目加到Tomcat中并启动,访问loginAction,会发现居然出现404,即下面所示错误,

HTTP Status 404 – Not Found


Type Status Report

Message There is no Action mapped for namespace [/] and action name [loginAction] associated with context path [/struts2.5].

Description The origin server did not find a current representation for the target resource or is not willing to disclose that one exists.

看上面错误提示,我们明明在Struts.xml里面配置了action,居然会出现no action ,然后各种百度之后发现,我们写的并没有错,对,代码本身没有错,但为何没错还报no action,其实问题出在Struts.xml文件所在位置上,Struts默认读取src根目录下面的Struts.xml配置文件,但是上面我们把Struts.xml文件放在了src/main/resource下,这就导致Struts找不到xml配置文件,从而出现no action的情况;

要解决这个问题,方法有两种:

方法一:

   在web.xml用filter配置Struts时,增加Struts配置文件路径参数,如下红色文字部分

 <filter>
      <filter-name>action2</filter-name>
      <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
      <init-param>  
            <param-name>config</param-name>  
            <param-value>  
                    struts-default.xml,struts-plugin.xml,main/resource/struts.xml  
                </param-value>  
    </init-param>

  </filter>
  <filter-mapping>
      <filter-name>action2</filter-name>
      <url-pattern>/*</url-pattern>
  </filter-mapping>

方法二:

  将Struts配置文件所在的resource文件夹提升到与src根目录下与main同级,操作如下

将resource文件夹提升到与src根目录下与main同级

选中项目右键properties->java build path->source,将source中的src移除,增加main与resource,apply后即可

 

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值