首页 > 解决方案 > 未找到映射 404 错误 n DispatcherServlet 名称为 'dispatcher'

问题描述

我正在创建小型 crud web 应用程序。我有这个并且它的工作正常,我可以从数据库中保存和读取表,所以这个代码工作:

EmployeeController:

    @Controller
@RequestMapping("/employee")
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    @GetMapping("/list")
    public String listEmployee(Model theModel) {

        // get customers from the service
        List<Employee> theEmployee = employeeService.listEmployee();

        // add the customers to the model
        theModel.addAttribute("employees", theEmployee);

        return "list-employee";
    }

    @GetMapping("/showFormForAdd")
    public String showFormForAdd(Model theModel) {

        // create model attribute to bind form data
        Employee theEmployee = new Employee();

        theModel.addAttribute("employee", theEmployee);

        return "addNewEmployeeForm";
    }

    @PostMapping("/addNewEmployee")
    public String addNewEmployee(@ModelAttribute("employee") Employee theEmployee) {

        // save the customer using our service
        employeeService.addNewEmployee(theEmployee);


        return "redirect:/employee/list";
    }
}

添加新员工部分代码的表格:

<form:form action="addNewEmployee" modelAttribute="employee" method="POST">

该代码工作正常。

这段代码没有,我不知道为什么?

EmployeeDetailController:

 @Controller
@RequestMapping("/employeeDetail")
public class EmployeeDetailController {

    @Autowired
    private EmployeeService employeeService;


    @GetMapping("/listEmployeeDetail")
    public String listEmployeeDetail(Model theModel) {

        // get customers from the service
        List<EmployeeDetail> theEmployeeDetail = employeeService.listEmployeeDetail();

        // add the customers to the model
        theModel.addAttribute("employeeDetails", theEmployeeDetail);

        return "list-employeeDetail";
    }

    @GetMapping("/showFormForAddEmployeeDetail")
    public String showFormForAddEmployeeDetail(Model theModel) {

        // create model attribute to bind form data
        EmployeeDetail theEmployeeDetail = new EmployeeDetail();

        theModel.addAttribute("employeeDetail", theEmployeeDetail);

        return "addNewEmployeeDetailForm";
    }

    @PostMapping("/addNewEmployeeDetail")
    public String addNewEmployeeDetail(@ModelAttribute("employeeDetail") EmployeeDetail theEmployeeDetail) {

        // save the customer using our service
        employeeService.addNewEmployeeDetail(theEmployeeDetail);


        return "redirect:/employeeDetail/listEmployeeDetail";
    }

}

addNewEmployeeDetailForm:

<form:form action="addNewEmployeeDetail" modelAttribute="employeeDetail" method="POST">

我得到这个错误:

No mapping found for HTTP request with URI [/Employee_Tracking_System-1.0-SNAPSHOT/employeeDetail/showFormForAddEmployeeDetail/addNewEmployeeDetail] in DispatcherServlet with name 'dispatcher']]

我没有使用任何 xml,纯干净的 java 配置。我试图把它放在同一个班级,但仍然找不到 404。

标签: javaspring

解决方案


推荐阅读