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

O.Kuz picture O.Kuz · Aug 30, 2015 · Viewed 33.4k times · Source

I have a problem when I build a Spring + Spring MVC + Hibernate + MySQL project.

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:

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

<%@ 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>

Answer

user3509208 picture user3509208 · Aug 30, 2015

This usually comes when the information that is required in your method

@RequestParam(value = "id", required = true)Integer 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