java - 使用 Spring MVC 提交表单时出错 - HTTP 状态 404

标签 java html spring spring-mvc

我正在尝试制作一个简单的表单并使用 Spring MVC 提交它。我尝试了多种方法来实现它,所有页面都映射到 web.xml 上,并且我检查它是否使用简单页面进行导航。但是当单击按钮时,我收到以下错误:

HTTP Status 404 – Not found
Type Status Report

Message /addEmployee

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

Apache Tomcat/9.0.20

employeeHome.jsp

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
    <title>Cadastro</title>
</head>
<body>
    <h1>Cadastro</h1>
    <form:form method="POST" action="/addEmployee" modelAttribute="employee">
        <table>
            <tr>
                <td><form:label path="name">Name</form:label></td>
                <td><form:input path="name"/></td>              
            </tr>
            <tr>
                <td><form:label path="id">ID</form:label></td>
                <td><form:input path="id"/></td>
            </tr>
            <tr>
                <td><form:label path="contactNumber">Contact Number</form:label></td>
                <td><form:input path="contactNumber"/></td>
            </tr>
            <tr>
                <td><input type="submit" value="Submit"/></td>
            </tr>
        </table>
    </form:form>
</body>
</html>

Controller :SpringMVCHelloWorld

@Controller
public class SpringMVCHelloWorld {

    // Employee
    @GetMapping("/employeeHome")
    public ModelAndView showForm() {
        return new ModelAndView("employeeHome", "employee", new Employee());
    }

    @RequestMapping(value = "/addEmployee", method = RequestMethod.POST)
    public String addEmployee(@ModelAttribute("employee") Employee employee, BindingResult result, ModelMap model) {
        if(result.hasErrors()) {
            return "error";
        }   
        model.addAttribute("name",employee.getName());
        model.addAttribute("contactNumber",employee.getContactNumber());
        model.addAttribute("id", employee.getId());     
        return "employeeView";
    }

} 

我认为问题是在 .jsp 页面和 @RequestMapping(value = "/addEmployee", method = RequestMethod.POST) 方法上设置 action ,但我不知道出了什么问题。

编辑:问题已解决

改变

<form:form method="post" action="/addEmployee" modelAttribute="employee">

<form:form method="post" action="addEmployee" modelAttribute="employee">

最佳答案

我认为问题可能出在操作 URL

你可以这样尝试

<form:form method="POST" action="${pageContext.request.contextPath}/addEmployee" modelAttribute="employee">

或者硬编码baseUrl(如果有的话)

<form:form method="POST" action="baseContextPath/addEmployee" modelAttribute="employee">

您可以refer this了解更多详情和 working code here

关于java - 使用 Spring MVC 提交表单时出错 - HTTP 状态 404,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56468046/

相关文章:

java - Spring Data JPA,如何通过Pageable获取最后一页

java - API 服务每隔一个请求工作一次

javascript - 主脚本中的 Vuejs 模板限制

html - 将形状从调色板拖到 Konvajs 舞台上

java - SAXNotRecognizedException : Feature 'http://javax.xml.XMLConstants/feature/secure-processing' not recognized

java - Spring Boot war 在 Elastic Beanstalk 中启动时失败,但在本地运行时正确启动

java - 在使用 AbstractRoutingDataSource 切换数据源时共享事务

java - 使用吗啡按字符串列表上的元素查询 MongoDB

java - 我的 Java 代码中的关键 SonarLint 问题 S1166 是否为误报?

javascript - 我想在同一页面显示来自不同链接的内容