struts2.5框架使用通配符指定方法

struts2.5框架使用通配符指定方法(常见错误)

一、问题描述

昨天使用struts2.5做人员管理系统的练习时候,遇到一个错误,怎么都解决不好,一直报错:

Struts has detected an unhandled exception:
Message:There is no Action mapped for namespace [/] and action name [user_login] associated with context path [/Struts2_exec_PM].

二、尝试的无效方法

昨晚排查到1点过,都没有找到问题在哪儿。几个可能的原因都查过了:
struts.xml配置文件在src下,并且服务器也部署到了WEB-INF\classes文件夹下。

三、各配置的具体配置

<?xml version="1.0" encoding="UTF-8" ?>
<!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.devMode" value="true" />
    <constant name="struts.ui.theme" value="simple" />
    <package name="default" namespace="/" extends="struts-default">
        <action name="user_*" class="cn.lim.user.web.action.UserAction" method="{1}">
            <result name="login_success">/login/home.jsp</result>
            <result name="login_input">/login/login.jsp</result>
        </action>
    </package>
</struts>

web.xml的配置也检查过了没有问题,welcome下的index.jsp也存在。

<filter>
    <filter-name>struts2</filter-name>
    <filter-class>org.apache.struts2.dispatcher.filter.StrutsPrepareAndExecuteFilter</filter-class>
</filter>

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

<welcome-file-list>
    <welcome-file>index.jsp</welcome-file>
</welcome-file-list>

jsp的form标签action的路径并没有错误。

<s:form id="loginAction_home" name="form1" action="user_login" namespace="/" target="_parent" method="post">

action类中的方法也没有问题。

public class UserAction extends ActionSupport implements ModelDriven<User>{

    User user = new User();

    @Override
    public User getModel() {
        // TODO Auto-generated method stub
        return user;
    }

    //登录操作
    @InputConfig(resultName="login_input")
    public String login(){
        return null;
    }
}

解决方法

今天早上找到了解决方案:
在struts2.3之前的版本,正常的配置就可以了,但在struts2.3版本之后,使用通配符调用方法时,内部会验证是否允许访问该方法,所以要加上:

<allowed-methods>方法名1,方法名2…</allowed-methods>

更改struts.xml后的文件:


```xml
<?xml version="1.0" encoding="UTF-8" ?>
<!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.devMode" value="true" />
    <constant name="struts.ui.theme" value="simple" />
    <package name="default" namespace="/" extends="struts-default">
        <action name="user_*" class="cn.lim.user.web.action.UserAction" method="{1}">
            <result name="login_success">/login/home.jsp</result>
            <result name="login_input">/login/login.jsp</result>
            <!--添加上这个配置后问题解决-->
            <allowed-methods>login</allowed-methods>
        </action>
    </package>
</struts>
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值