![](/img/trans.png)
[英]Spring MVC and thymeleaf ModelAttribute either null or not evaluated
[英]Spring MVC second Post method @ModelAttribute is 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:/";
}
想法是,單擊“提交”按鈕將創建一個新項目,而“更新”按鈕將編輯一個現有項目。 這兩個.POST
方法指定了不同的參數來區分它們。
提交按鈕按預期方式工作 。 但是,當按下更新按鈕時, updateForm
Task
對象傳遞給updateForm
控制器方法-但其所有字段均為空。
我不確定為什么表單會在submit
方法上將字段正確綁定到Task
,但是似乎創建了一個新Task
卻在update
方法中根本沒有綁定它。
我傾向於將這些方法組合為一個控制器方法,並使用一些Java邏輯來確定是否基於參數來提交/更新-但我對我所缺少的內容以及為什么這樣做不起作用感到好奇。
好吧,我感到非常愚蠢。 這根本不是SpringMVC的問題。 該問題是由禁用表單的一部分的Jquery方法引起的。 這樣看來,表單沒有綁定,但實際上它只是綁定到空值。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.