Struts2使用通配符出现org.apache.struts2.dispatcher.Dispatcher - Could not find action or result的问题

这个问题困扰了我很久,一开始以为是路径的万体,找了一天半发现不是路径的问题。

下面是我的代码和解决办法:

Action类:

UsersAction.java

public class UsersAction extends ActionSupport  {
    private String email;
    private String pwd;
    private UsersService usersService;

    public void setPwd(String pwd) {
        this.pwd = pwd;
    }

    public void setUsersService(UsersService usersService) {
        this.usersService = usersService;
    }

    @Override
    public String execute() throws Exception {
        return super.execute();
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public String login(){
        if (usersService.selectByEmail(email,pwd))
            return "index";
        else
            return  ERROR;
    }

}

struts.xml:(Action类我用的是spring注入的所以没有写路径)

<?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.enable.DynamicMethodInvocation" value="false"/>
    <constant name="struts.devMode" value="true" />
    <constant name="struts.objectFactory" value="spring"/>
    <package name="default" extends="struts-default" namespace="/" >
            <action name="users_*" class="usersAction" method="{1}">
                <result name="login">public/head.jsp</result>
                <result name="error">public/foot.jsp</result>
            </action>
            <action name="register" >
                <result>/public/register.jsp</result>
            </action>
        <action name="index">
            <result>/common/index.jsp</result>
        </action>
    </package>
</struts>

jsp:

<s:form action="users_login.action" method="post">
    ……
</s:form>

解决办法:

最后是在配置文档struts.xml的Action中配置了

<allowed-methods>Action内的方法名</allowed-methods>才成功了

例如在我action配置中加入:

<action name="users_*" class="usersAction" method="{1}">
    <result name="login">public/head.jsp</result>
    <result name="error">public/foot.jsp</result>
    <allowed-methods>login</allowed-methods>
</action>

总结:

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

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


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值