HTTP 状态 400 - 必需的整数参数 'id' 不存在

HTTP Status 400 - Required Integer parameter 'id' is not present

我在构建 Spring + Spring MVC + Hibernate + MySQL 项目时遇到问题。

一切正常,但是当我得到 "editpage" 然后想 post 将信息编辑到 MySQL 时,我的 Tomcat 出现以下错误:

HTTP Status 400 - Required Integer parameter 'id' is not present

控制器class

@RequestMapping(value = "/users/edit", method = RequestMethod.POST)
public String saveEdit(@ModelAttribute("userAttribute") User user,
                       @RequestParam(value = "id", required =   
true)Integer id, Model model){

    logger.debug("Received request to update person");

    user.setId(id);

    personService.edit(user);

    model.addAttribute("id", id);

    return "editedpage";
}

服务class

public void edit(User user){

    logger.debug("Editing existing user");

    Session session = sessionFactory.getCurrentSession();

    User existingUser = (User) session.get(User.class, user.getId());

    existingUser.setLogin(user.getLogin());
    existingUser.setPassword(user.getPassword());
    existingUser.setReal_name(user.getReal_name());

    session.save(existingUser);
}

我的 JSP 页面

<%@ taglib uri="http://java.sun.com/jsp/jstl/core" prefix="c" %>
<%@ taglib uri="http://www.springframework.org/tags/form" prefix="form" %>
<%@ page language="java" contentType="text/html; charset=UTF-8"
     pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html lang="en">

<head>
<!-- Bootstrap core CSS -->
<link href="<c:url value="/resources/styles/bootstrap.min.css" />"        
rel="stylesheet">
<script src="<c:url value="../../resources/javascripts  
/non-empty_validation_editpage.js"/>"></script>

<title>Editing page</title>

</head>

<body>

<h1 style="text-align: center">Edit page</h1>

<c:url var="saveUrl" value="/main/users/edit?id = ${userAttribute.id}"/>
<form:form modelAttribute = "userAttribute" method = "POST" action =  
"${saveUrl}">

<div style="width: 300px; height: 500px; margin: auto;">

<div class="form-group" >
<form:label path = "id">id:</form:label>
<form:input type = "id" name = "id" path = "id" disabled="true"  
class="form-control" />

</div>

<div class="form-group">
  <form:label path = "login">Login</form:label>
  <form:input type = "login" name = "login" path = "login" class="form- 
control"  placeholder="new login"/>

</div>

<div class="form-group">
<form:label path = "password">Password</form:label>
<form:input type = "password" name = "password" path = "password" 
class="form-control"  placeholder="new password"/>

</div>

<div class="form-group">
<form:label path = "real_name">Real name</form:label>
<form:input type = "real_name" name = "real_name" path = "real_name" 
class = "form-control"  placeholder = "new Real name"/>

</div>

<input type="submit" class="btn btn-success" value="Save"  style="width:  
300px"/>

</div>

</form:form>

</body>
</html>

这通常发生在您的方法中需要的信息

@RequestParam(value = "id", required = true)Integer id

在这种情况下,每当您发布保存的数据时,id 都不会作为请求参数出现。尝试硬编码任何数字而不是 @RequestParam(value = "id", required =
true)Integer id,你应该可以保存数据。另一种方法是将 required = true 更改为 false 只是为了测试目的,看看它是否有任何区别

如果请求访问Get方法,url路径必须包含参数id。如果POST,可以硬编码@RequestParam(value = "id", required =false)