我有一个 JSP,其中有一个 Spring 表单。在呈现 JSP 之前,将表单的命令对象添加到控制器中。Spring 将 JSP 中的表单绑定到这个命令对象,并在提交 NEW 实例时正确处理它。
但是,我想通过 DWR 保留命令对象(也可以正常工作),然后将表单提交给控制器。在表单提交给控制器的那一刻,命令对象不再是一个新对象,而是一个需要更新的持久化对象。这是我希望表单元素自动绑定到命令对象并通过绑定进行更新的地方,但它们没有被绑定。
简单示例:我将向 中添加一个新Task
的ModelMap
,以便 Spring 表单将绑定到该命令对象。但是,我不会提交新的,而是通过 DWRTask
持久化新Task
的,这将返回 ID,然后在将表单提交到控制器之前继续编辑任务。
控制器类
@Controller
public class ProjectController {
/**
* This adds the "task" command object to the session attributes and loads
* the initial form.
*/
@RequestMapping(value="/project", method=RequestMethod.GET)
public String setupForm(@RequestParam(value="id", required=true) String id,
HttpServletRequest request, ModelMap modelMap) {
modelMap.addAttribute("project", projectRepo.get(id));
modelMap.addAttribute("task", new Task());
return "/project/task";
}
/**
* This processes the form submit, and should update the Task.
*/
@RequestMapping(value="/project/task/update", method=RequestMethod.POST)
public String updateTask(@ModelAttribute(value="task") Task task,
@RequestParam(value="taskId") String taskId,
HttpServletRequest request, ModelMap modelMap) {
// BEFORE binding the parameters to the command object (task),
// I want to assign the command object as the one already persisted.
task = taskRepo.get(taskId);
// NOW, I want the request parameters to be bound to the task command object.
// HOW ?????????
// Persist the changes.
taskRepo.merge(task);
// BACK to the setupForm method/form view
return "/project?id=" + task.getProject().getId();
}
}
弹簧形式
<form:form commandName="task" method="post" action="/project/task/update" id="taskForm">
<form:hidden path="id" id="task.id"/>
<form:input path="name" id="task.name"/>
<!-- DWR will save the task (save and continue), then will return the id. -->
<!-- After saved, the user can still change the name,
then submit the form for processing by the controller -->
</form:form>
可以在任何提交后绑定发生之前将 Spring 绑定命令对象设置为持久对象吗?