struts2.5匹配action出错

错误描述

今日写struts的时候,突然出现一个Action中其他方法能访问,但唯独一个方法总是不能匹配的问题。

报错信息如下:

HTTP Status 404 - There is no Action mapped for namespace [/] and action name [emp_viewAdd] associated with context path [/UserSystem].

代码如下:

struts.xml

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <package name="employee" extends="struts-default" namespace="/">
        <!-- employee -->
        <action name="emp_*" class="employeeAction" method="{1}">
            <result name="list">/list.jsp</result>
            <result name="add">/add.jsp</result>
        </action>
    </package>
</struts>

employeeAction.java

package com.action;

import com.entity.Dept;
import com.entity.Employee;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import java.util.List;


@Controller
@Scope("prototype")
public class EmployeeAction extends BaseAction{
    
    private Employee employee;

    /**
     * 员工列表展示
     * @return
     */
    public String list(){
        //查询所有员工
        List<Employee> employeeList = employeeService.getAll();
        request.put("listEmp",employeeList);
        System.out.println("employee -- action");
        return LIST;
    }

    /**
     * 添加员工-进入添加页面
     * @return
     */
    public String viewAdd(){
        System.out.println("viewadd");
        //查询所有部门信息,保存到request
        List<Dept> listDept = deptService.getAll();
        request.put("listDept",listDept);
        return "add";
    }

    public Employee getEmployee() {
        return employee;
    }

    public void setEmployee(Employee employee) {
        this.employee = employee;
    }
}

解决方案

经过一番查找,发现这是struts2.5版本的问题。在2.5版本,struts在取得方法名称后,会验证是否允许访问,所以只需要在struts.xml中加入一行

<allowed-methods>list,viewAdd,save</allowed-methods> 

修改后的struts.xml配置如下

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE struts PUBLIC
        "-//Apache Software Foundation//DTD Struts Configuration 2.3//EN"
        "http://struts.apache.org/dtds/struts-2.3.dtd">

<struts>
    <package name="employee" extends="struts-default" namespace="/">
        <!-- employee -->
        <action name="emp_*" class="employeeAction" method="{1}">
            <result name="list">/list.jsp</result>
            <result name="add">/add.jsp</result>
            <allowed-methods>list,viewAdd,save</allowed-methods> 
        </action>
    </package>
</struts>

相关资料

转载于:https://my.oschina.net/u/3296367/blog/1491746

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值