Struth2过滤报错“警告: Could not find action or result”解决方法
用Struth2框架时,如果浏览器中输入了struth.xml中没有定义的action时,会报以下错误:
警告: Could notfind action or result
There is no Action mapped for namespace /and action name dawdwadwaad. - [unknown location]
警告: Could notfind action or result
There is no Action mapped for action namehtml. - [unknown location]
网上找了一下有以下几种解决办法,供参考
第一种方法:
<!-- 这句话意思就是你说的了,只要找不到action,都会找这个默认的action-->
<default-action-ref name="defaultAction" />
<!-- 配置全局result,因为你的action直接返回error,所以我们添加一个全局的result,用来指示错误以后,返回哪个页面。 -->
<global-results>
<result name="error" type="redirect">page/error.jsp</result> //里面的路径和JSP文件你自己写。
</global-results>
<!-- 默认action -->这一步是把你写的那个action配置到struts中。
<action name="defaultAction" class="前面加你的包名DefaultAction" />
第二种方法:
![](/qrcode.jpg)
类似第一种方法,只是没用全局跳转,而是在DefaultAction中加个execute()方法,直接return "xxxx";
public String execute(){
return "xxxx";//xxxx自己随便定义,只要跟struth.xml定义的一样就行
}
struth.xml中配置:
<default-action-ref name="defaultAction" />
<!-- 默认action -->这一步是把你写的那个action配置到struts中。
<action name="defaultAction" class="前面加你的包名 DefaultAction" >
<result name="XXXX">要跳转的jsp,如login.jsp</result>
</action>
第三种方法:
<struts>
<package name="default" namespace="/" extends="struts-default">
<default-action-ref name="index"></default-action-ref>
<action name="index">
<result>/default.jsp</result>
</action>
</package>
</struts>
第四种方法:
不管action还是页面,如果找不到,都会报404错误,那么在你的项目的web.xml中配置一下,让所有404错误都跳转到你指定的页面(比如你定义一个error.jsp页面),就OK了,配置如下:
<!-- 配置404错误时转向到的页面 -->
<error-page>
<error-code>404</error-code>
<location>/error.jsp</location>
</error-page>