2

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バインドされたコマンドオブジェクトを永続化されたオブジェクトに設定できますか?

4

2 に答える 2

3

実際には、注釈を使用してこれを行うためのより良い方法があります。

リポジトリから必要なコマンドオブジェクトを返すModelAttributeメソッドを作成します。

@ModelAttribute("task")
public Task task(@RequestParam(value = "id", required = true) String id) {
    return taskRepo.get(taskId);
}

次に、ModelAttributeをフォーム送信メソッドに追加するだけです。

@RequestMapping(value="/project/task/update", method=RequestMethod.POST)
public String updateTask(@ModelAttribute(value="task") Task task,
                         HttpServletRequest request, ModelMap modelMap) {

    taskRepo.merge(task);
    ...
}
于 2009-05-19T13:37:01.580 に答える
1

を使用@ModelAttributeしてコマンドオブジェクトにアクセスすると、コマンドオブジェクトにアクセスする前にバインディングが発生するようです。フォームからリクエストパラメータをバインドする前に、そのコマンドオブジェクトを必要なものに設定するには、属性のIDを渡してデータベースから取得し、WebRequestパラメータをバインドするだけです。

POSTメソッドで

@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.
    WebRequestDataBinder binder = new WebRequestDataBinder(task);
    ServletWebRequest webRequest = new ServletWebRequest(request);
    binder.bind(webRequest);

    // Persist the changes.
    taskRepo.merge(task);

    // BACK to the setupForm method/form view
    return "/project?id=" + task.getProject().getId();
}

Spring2.5.xドキュメントにWebRequestDataBinderは、このタイプのアプリケーションの「手動データバインディング」のユルゲンヘラーの例があります。

MyBean myBean = new MyBean();
// apply binder to custom target object
WebRequestDataBinder binder = new WebRequestDataBinder(myBean);
// register custom editors, if desired
binder.registerCustomEditor(...);
// trigger actual binding of request parameters
binder.bind(request);
// optionally evaluate binding errors
Errors errors = binder.getErrors();
...
于 2009-05-15T14:49:32.793 に答える