HTTP状态400 - 必需整数参数“id”不存在

时间:2015-08-30 18:22:10

标签: spring spring-mvc http-status-code-400

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

一切正常但当我得到“editpage”然后想要将编辑信息发布到MySQL时,我从Tomcat中收到以下错误:

@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";
}

控制器类

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);
}

服务类

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

我的JSP页面

select t.* from (
  select 
    t.bada,
    t.boom, 
    row_number() over (partition by bada, boom order by bada) as seqnum,
    t.*,
    from table t) t
where seqnum = 1;

2 个答案:

答案 0 :(得分:1)

这通常在您的方法所需的信息

时出现
  

@RequestParam(value =“id”,required = true)整数id

在这种情况下,只要你发布保存的数据,id就不会作为请求参数出现。尝试对任何数字进行硬编码而不是@RequestParam(value =“id”,required =
true)整数id,你应该能够保存数据。另一种方法是将required = true更改为false,仅用于测试目的,看看它是否有任何区别

答案 1 :(得分:0)

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