简体   繁体   中英

Spring MVC second Post method @ModelAttribute is null

I have a form that has two buttons, a submit button and an update button.

Here is the JSP:

<%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>
<%@ 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>
<!-- Bootstrap core CSS !-->
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap-theme.min.css">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/js/bootstrap.min.js"></script>

<!--  Custom scripts/styles for this page  !-->
<script type="text/javascript" src="${pageContext.request.contextPath}/static/script/jquery.js"></script>
<script type="text/javascript" src="${pageContext.request.contextPath}/static/script/script.js"></script>
<link rel="stylesheet" type="text/css" href="${pageContext.request.contextPath}/static/css/style.css" /> 

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Test</title>
</head>
<body>
<!-- General Information Form -->
    <form:form id="form" method="POST" action="${pageContext.request.contextPath}/create" modelAttribute="task">
    <div id="general">
            <table style="width: 224px;">
                <tr>
                    <td colspan="2"><form:select id="systemType" path="systemType" items="${systemTypes}" /></td>
                </tr>
                <tr>
                    <td id="buffer"></td>
                </tr>
                <tr>
                    <td colspan="2"><form:select  id="userName" path="user.fullName" items="${userFullNames}" /></td>
                </tr>
                <tr>
                    <td id="buffer"></td>
                </tr>
                <tr>
                    <td style="width: 50%;"><label for=line><b>Line: </b></label></td>
                    <td><form:select  path="location.line" items="${lines}" id="line"/></td>
                </tr>
                <tr>
                    <td id="buffer"></td>
                </tr>
                <tr>
                    <td style="width: 50%;"><label for=position><b>Position: </b></label></td>
                    <td><form:select path="location.position" items="${positions}" id="position" /></td>
                </tr>
                <tr>
                    <td id="buffer"></td>
                </tr>
                <tr>
                    <td colspan="2">
                    <input id="submitButton" type="submit" name="submit" value=Submit />
                    <input style="display:none;" id="updateButton" type="submit" name="update" value="Update" />
                    <input id="cancel" style="float: right;" type="Reset" value="Cancel"  />
                    </td>
                </tr>
            </table>
        </div>
</form:form>
</body>
</html>

Here is my controller:

@Controller
@RequestMapping("/")
public class HomeController {

private TaskService taskService;

@Autowired
public void setTaskService(TaskService taskService) {
    this.taskService = taskService;
    }

@RequestMapping(value = "/", method = RequestMethod.GET)
public String initForm(Model model, HttpServletRequest request) {
    Task task = new Task();

    model.addAttribute("task", task);
    return "home";
}

@RequestMapping(value="/create", params="submit", method = RequestMethod.POST)
public String submitForm(@ModelAttribute("task")Task task,BindingResult result, Model model) { 

    taskService.create(task);
    return "redirect:/";
}

@RequestMapping(value="/create", params="update", method = RequestMethod.POST)
public String updateForm(@ModelAttribute("task")Task task, BindingResult result, Model model) {

   System.out.println("Updating: " + task.toString());          
   taskService.update(task);

   return "redirect:/";
}

The idea being that hitting the submit button will make a new item, and the update button will edit an existing one. The two .POST methods have different parameters specified to distinguish them.

The submit button is working as expected . However when the update button is pressed a Task object is delivered to the updateForm controller method - but all of its fields are null.

I am not sure why the form is binding the fields to the Task correctly on the submit method, but seemingly creating a new Task and not binding it at all in the update method.

I am inclined to just combine these methods into a single controller method and use some Java logic to determine whether to submit/update based on the parameter - but am curious as to what I'm missing and why this doesn't work.

Ok, I feel very stupid. This was not a SpringMVC issue at all. The problem was caused by a Jquery method that was disabling a part of the form. This gave the appearance that the form was not binding, but actually it was just binding to null values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM