Springmvc-multiactioncontroller

提供:Dev Guides
移動先:案内検索

Spring MVC-マルチアクションコントローラーの例

次の例は、Spring Web MVCフレームワークを使用してマルチアクションコントローラーを使用する方法を示しています。 MultiActionController クラスは、単一のコントローラー内のメソッドを使用して複数のURLをそれぞれマップするのに役立ちます。

package com.finddevguides;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class UserController extends MultiActionController{

   public ModelAndView home(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("home");
      model.addObject("message", "Home");
      return model;
   }

   public ModelAndView add(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("user");
      model.addObject("message", "Add");
      return model;
   }

   public ModelAndView remove(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("user");
      model.addObject("message", "Remove");
      return model;
   }
}
<bean class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
<bean name = "/home" class = "com.finddevguides.UserController"/>
<bean name = "/user/*" class = "com.finddevguides.UserController"/>

たとえば、URIの場合、上記の構成を使用して-

*/homeが要求されると、DispatcherServletは要求をUserController* home()*メソッドに転送します。
*user/addが要求されると、DispatcherServletはその要求をUserController* add()*メソッドに転送します。
*user/removeがリクエストされると、DispatcherServletはリクエストをUserController* remove()*メソッドに転送します。

はじめに、作業中のEclipse IDEを用意し、次の手順に従って、Spring Web Frameworkを使用して動的フォームベースのWebアプリケーションを開発します。

Step Description
1 Create a project with a name TestWeb under a package com.finddevguides as explained in the Spring MVC - Hello World chapter.
2 Create a Java class UserController under the com.finddevguides package.
3 Create view files home.jsp and user.jsp under the jsp sub-folder.
4 The final step is to create the content of the source and configuration files and export the application as explained below.

UserController.java

package com.finddevguides;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.mvc.multiaction.MultiActionController;

public class UserController extends MultiActionController{

   public ModelAndView home(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("home");
      model.addObject("message", "Home");
      return model;
   }

   public ModelAndView add(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("user");
      model.addObject("message", "Add");
      return model;
   }

   public ModelAndView remove(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("user");
      model.addObject("message", "Remove");
      return model;
   }
}

TestWeb-servlet.xml

<beans xmlns = "http://www.springframework.org/schema/beans"
   xmlns:context = "http://www.springframework.org/schema/context"
   xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
   xsi:schemaLocation = "
   http://www.springframework.org/schema/beans
   http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
   http://www.springframework.org/schema/context
   http://www.springframework.org/schema/context/spring-context-3.0.xsd">

   <bean class = "org.springframework.web.servlet.view.InternalResourceViewResolver">
      <property name = "prefix" value = "/WEB-INF/jsp/"/>
      <property name = "suffix" value = ".jsp"/>
   </bean>

   <bean class = "org.springframework.web.servlet.handler.BeanNameUrlHandlerMapping"/>
   <bean name = "/home"
      class = "com.finddevguides.UserController"/>
   <bean name = "/user/*"
      class = "com.finddevguides.UserController"/>
</beans>

home.jsp

<%@ page contentType = "text/html; charset = UTF-8" %>
<html>
   <head>
      <meta http-equiv = "Content-Type" content = "text/html; charset = ISO-8859-1">
      <title>Home</title>
   </head>

   <body>
      <a href = "user/add" >Add</a> <br>
      <a href = "user/remove" >Remove</a>
   </body>
</html>

user.jsp

<%@ page contentType = "text/html; charset = UTF-8" %>
<html>
   <head>
      <title>Hello World</title>
   </head>
   <body>
      <h2>${message}</h2>
   </body>
</html>

ソースファイルと構成ファイルの作成が完了したら、アプリケーションをエクスポートします。 アプリケーションを右クリックし、[エクスポート]→[WARファイル]オプションを使用して、 TestWeb.war ファイルをTomcatのwebappsフォルダーに保存します。

次に、Tomcatサーバーを起動し、標準ブラウザーを使用してwebappsフォルダーから他のWebページにアクセスできることを確認します。 ここで、URL- http://localhost:8080/TestWeb/home を試してください。SpringWebアプリケーションで問題がなければ、次の画面が表示されます。

Spring Multi Action Controller 1

*http://localhost:8080/TestWeb/user/add* というURLを試してください。SpringWebアプリケーションで問題がなければ、次の画面が表示されます。

Spring Multi Action Controller 2