2015-09-06 101 views
0

我有一个具有两个按钮,一个提交按钮和一个更新按钮的窗体。Spring MVC second Post方法@ModelAttribute为null

这里是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> 

这里是我的控制器:

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

的想法是,打提交按钮将一个新的项目,并更新按钮将编辑现有的一。这两个方法有不同的参数来区分它们。

提交按钮按预期工作。但是,当按下更新按钮时,将Task对象传送到updateForm控制器方法 - 但其所有字段均为空。

我不知道为什么形式正确地结合田间地头到Tasksubmit方法,但似乎创造了新Task而不是在update方法都结合了。

我倾向于只将这些方法合并到一个控制器方法中,并使用一些Java逻辑来确定是否根据参数提交/更新 - 但我好奇我缺少什么以及为什么没有工作。

+0

请在这里提供完整的HTML页面..? – SaviNuclear

+0

我添加了JSP文件 –

回答

0

好的,我觉得很愚蠢。这根本不是SpringMVC问题。这个问题是由于禁用了部分表单的Jquery方法造成的。这表明表单没有绑定,但实际上它只是绑定到空值。

相关问题