## 在 struts2.3 的版本中,我们通常这么配置 struts.xml 文件:


<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
    <constant name="struts.objectFactory" value="spring"/>
    <package name="crm" namespace="/" extends="struts-default">
        <action name="UserAction_*" class="com.dw.web.action.UserAction" method="{1}">
            <result name="success" >/index.jsp</result>
        </action>
    </package>
</struts>

## UserAction类如下:


import com.dw.service.UserService;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @ClassName UserAction
 * @Author Dennis
 * @Description TODO
 * @Date 2019/7/8 13:50
 * @versin V1.0
 */
public class UserAction extends ActionSupport {
    private UserService UserService;

    public void setUserService(UserService userService) {
        UserService = userService;
    }

    public String login() throws Exception {
        System.out.println(UserService);
        return super.execute();
    }

}

这样在 struts2.3 上能够成功,但在 struts2.5 上却无论如何定位不到 http://localhost:8080/CRM_ssh/UserAction_login,会发生如下错误:

Type Status Report

消息 There is no Action mapped for namespace [/] and action name [userAction_login] associated with context path [].

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

搜了半天,有说是要添加 ,明确打开动态方法调用功能,因为 struts2 默认是关的,但在这里并不能解决问题,因为配置是正确的。

原来是 struts2.5 为了增加安全性,在 struts.xml 添加了这么个属性:regex:.*

同时要注意,struts.xml 的解析版本要为 2.5,即头部信息应为:

    "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
    "http://struts.apache.org/dtds/struts-2.5.dtd">

最后,struts.xml 应该为:


<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.5//EN"
        "http://struts.apache.org/dtds/struts-2.5.dtd">

<struts>
    <constant name="struts.objectFactory" value="spring"/>
    <constant name="struts.enable.DynamicMethodInvocation" value="true"/>
    <package name="crm" namespace="/" extends="struts-default">
        <global-allowed-methods>regex:.*</global-allowed-methods>
        <action name="UserAction_*" class="com.dw.web.action.UserAction" method="{1}">
            <result name="success" >/index.jsp</result>
        </action>
    </package>
</struts>