Springフォームを含むJSPがあります。フォームのコマンドオブジェクトは、JSPがレンダリングされる前にコントローラに追加されます。Springは、JSPのフォームをこのコマンドオブジェクトにバインドし、NEWインスタンスを送信するときに正しく処理します。
ただし、DWR(これも正しく機能します)を介してコマンドオブジェクトを永続化し、フォームをコントローラーに送信します。フォームがコントローラーに送信された時点で、コマンドオブジェクトは新しいオブジェクトではなく、更新する必要のある永続オブジェクトです。ここで、フォーム要素をコマンドオブジェクトに自動的にバインドし、バインドを介して更新しますが、バインドされていません。
簡単な例:Springフォームがそのコマンドオブジェクトにバインドされるように、にnewTask
を追加します。ModelMap
ただし、新しいを送信する代わりに、 DWRを介してTask
その新しい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バインドされたコマンドオブジェクトを永続化されたオブジェクトに設定できますか?