HTTP Status 404 - There is no Action mapped for namespace [/] and action name [login!welcome] associated with context path [/Struts-0908].(动态调用方法DMI失败)
我使用的struts版本为struts-2.3.24
struts.xml:
<pre name="code" class="html"><span style="font-size:24px;"><strong><struts>
<package name="hello" extends="struts-default">
<action name="hello" class="">
<result>/index.jsp</result>
</action>
<action name="login" class="smu.edu.action.LoginAction">
<result name="welcome">/welcome.jsp</result>
</action>
<action name="login2" class="smu.edu.action.LoginAction" method="login">
<result name="login">/index.jsp</result>
</action>
</package>
</struts></strong></span>
action:
<span style="font-size:24px;"><strong>package smu.edu.action;
public class LoginAction {
public String execute(){
System.out.println("运行execute方法");
return "success";
}
public String login(){
System.out.println("运行login方法");
return "login";
}
public String welcome(){
System.out.println("运行welcome方法");
return "welcome";
}
}
</strong></span>
调用动态调用方法DMI,输入http://acer-pc:8080/Struts-0908/login!welcome,结果出现了
很多人都奇怪明明都没有敲错,都是按照步骤做的,怎么就不行了呢?因为struts2之前的版本动态方法DMI调用默认是打开的,一些struts版本可能动态调用方法DMI默认为关闭的。
解决方法:
<span style="font-size:24px;"><strong><struts>
<constant name="struts.enable.DynamicMethodInvocation" value="true"></constant>
<package name="hello" extends="struts-default">
<action name="hello" class="">
<result>/index.jsp</result>
</action>
<action name="login" class="smu.edu.action.LoginAction">
<result name="welcome">/welcome.jsp</result>
</action>
<action name="login2" class="smu.edu.action.LoginAction" method="login">
<result name="login">/index.jsp</result>
</action>
</package>
</struts></strong></span>
往struts.xml文件中添加如下:
<span style="font-size:24px;"><strong><constant name="struts.enable.DynamicMethodInvocation" value="true"></constant></strong></span>
结果成功: