開発者ドキュメント

Spring MVC MultiActionControllerアノテーションの例

このチュートリアルでは、「@ RequestMapping」を使用してSpring MVCアノテーションベース

MultiActionController

を開発する方法を説明します。

XMLベースのMultiActionControllerでは、メソッド名リゾルバ(

InternalPathMethodNameResolver



PropertiesMethodNameResolver

またはリンク://spring-mvc/spring-mvc-parametermethodnameresolver-example/[ParameterMethodNameResolver])を使用してURLを特定のメソッド名にマップします。しかし、アノテーションをサポートすることで人生がより簡単になりました。メソッド名リゾルバとして

@ RequestMapping

アノテーションを使用できるようになりました。これはURLを特定のメソッドにマップするために使用されました。

これを設定するには、メソッド名の上にURLをマッピングして

@ RequestMapping

を定義します。

package com.mkyong.common.controller;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class CustomerController{

    @RequestMapping("/customer/add.htm")
    public ModelAndView add(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

        return new ModelAndView("CustomerAddView");

    }

    @RequestMapping("/customer/delete.htm")
    public ModelAndView delete(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

        return new ModelAndView("CustomerDeleteView");

    }

    @RequestMapping("/customer/update.htm")
    public ModelAndView update(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

        return new ModelAndView("CustomerUpdateView");

    }

    @RequestMapping("/customer/list.htm")
    public ModelAndView list(HttpServletRequest request,
        HttpServletResponse response) throws Exception {

        return new ModelAndView("CustomerListView");

    }
}

URLは次のパターンのメソッド名にマップされます。

  1. /customer/add.htm – > add()メソッド

  2. /customer/delete.htm – > delete()メソッド

  3. /customer/update.htm – > update()メソッド

  4. /customer/list.htm – > list()メソッド

    • 注意** Spring MVCでは、この `@ RequestMapping`は常に最も柔軟で使いやすいマッピング機構です。

ソースコードをダウンロードする

ダウンロードする –

SpringMVC-MultiActionController-Annotation-Example.zip

(7KB)

モバイルバージョンを終了