spring HTTP 状态 400 - 所需的整数参数“id”不存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/32299733/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
HTTP Status 400 - Required Integer parameter 'id' is not present
提问by O.Kuz
I have a problem when I build a Spring + Spring MVC + Hibernate + MySQL project.
我在构建 Spring + Spring MVC + Hibernate + MySQL 项目时遇到了问题。
Everything works fine but when I get "editpage" and then want to post editing information into MySQL, I get the following error from my Tomcat:
一切正常,但是当我收到“editpage”然后想将编辑信息发布到 MySQL 时,我的 Tomcat 出现以下错误:
HTTP Status 400 - Required Integer parameter 'id' is not present
Controller 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";
}
Service 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);
}
My JSP page
我的 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>
采纳答案by user3509208
This usually comes when the information that is required in your method
这通常发生在您的方法中所需的信息
@RequestParam(value = "id", required = true)Integer id
@RequestParam(value = "id", required = true)整数id
in this case id is not present as a request parameter whenever u are posting the saved data. Trying hard coding any number instead of @RequestParam(value = "id", required =
true)Integer id, you should be able to save the data. Another way is to changed from required = true to false just for testing purposes and see if it makes any difference
在这种情况下,每当您发布保存的数据时,id 不会作为请求参数出现。尝试硬编码任何数字而不是 @RequestParam(value = "id", required =
true)Integer id,您应该能够保存数据。另一种方法是将 required = true 更改为 false 仅用于测试目的,看看它是否有任何区别
回答by zehao chen
If you request access Get method, the url path must contain param id
.If POST,you can hard coding @RequestParam(value = "id", required =false)
如果你请求访问Get方法,url路径必须包含param id
。如果是POST,你可以硬编码@RequestParam(value = "id", required =false)