Springmvc-propertiesmethodnameresolver

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

Spring MVC-プロパティメソッド名リゾルバーの例

次の例は、Spring Web MVCフレームワークを使用して、Multi Action ControllerのProperties Method Name Resolverメソッドを使用する方法を示しています。 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("user");
      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 = "com.finddevguides.UserController">
   <property name = "methodNameResolver">
      <bean class = "org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
         <property name = "mappings">
            <props>
               <prop key = "/user/home">home</prop>
               <prop key = "/user/add">add</prop>
               <prop key = "/user/remove">update</prop>
            </props>
         </property>
      </bean>
   </property>
</bean>

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

*/user/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 Java class UserController under the com.finddevguides package.
3 Create a view file 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("user");
      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.mvc.support.ControllerClassNameHandlerMapping">
      <property name = "caseSensitive" value = "true"/>
   </bean>
   <bean class = "com.finddevguides.UserController">
      <property name = "methodNameResolver">
         <bean class = "org.springframework.web.servlet.mvc.multiaction.PropertiesMethodNameResolver">
            <property name = "mappings">
               <props>
                  <prop key = "/user/home">home</prop>
                  <prop key = "/user/add">add</prop>
                  <prop key = "/user/remove">update</prop>
               </props>
            </property>
         </bean>
      </property>
   </bean>
</beans>

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/user/add を試してください。SpringWebアプリケーションで問題がなければ、次の画面が表示されます。

Spring Multi Action Controller