Spring MVC多動作控制器

以下示例演示如何使用Spring Web MVC框架的多動作控制器。 MultiActionController類用於在單個控制器中分別映射多個URL到對應的方法。

所下所示配置 -

package com.yiibai.springmvc;

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;
   }
}

URL映射配置文件如下 -

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

例如,使用上面的配置,如果URI:

  • 對於/home.html請求,DispatcherServlet將請求轉發到UserController類的 home()方法。
  • 對於user/add.htmlDispatcherServlet將請求轉發到UserController類的 add()方法。
  • 對於user/remove.htmlDispatcherServlet將請求轉發到UserController類的 remove()方法。

首先,讓我們使用Eclipse IDE,並按照以下步驟使用Spring Web Framework開發基於動態表單的Web應用程序:

  1. 創建一個名稱爲 MultiActionController 的動態WEB項目。
  2. com.yiibai.springmvc 包下創建一個Java類UserController
  3. jsp子文件夾下創建兩個視圖文件:home.jspuser.jsp
  4. 最後一步是創建所有源和配置文件的內容並運行應用程序,詳細如下所述。

完整的項目文件目錄結構如下所示 -
Spring

UserController.java 的代碼如下所示 -

package com.yiibai.springmvc;
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;
   }
}

MultiActionController-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.html" 
      class="com.yiibai.springmvc.UserController" /> 
   <bean name="/user/*.html" 
      class="com.yiibai.springmvc.UserController" />    
</beans>

home.jsp 的代碼如下所示 -

<%@ page contentType="text/html; charset=UTF-8" %>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Home</title>
</head>
<body>
<body>
<a href="user/add.html" >add()方法</a> <br>
<a href="user/remove.html" >remove()方法</a>
</body>
</html>

user.jsp 的代碼如下所示 -

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

完成創建源和配置文件後,發佈應用程序到Tomcat服務器。

現在啓動Tomcat服務器,現在嘗試訪問URL => http://localhost:8080/MultiActionController/home.html ,如果Spring Web應用程序沒有問題,應該看到以下結果:

Spring

當訪問URL => http://localhost:8080/MultiActionController/add.html , 如果Spring Web應用程序沒有問題,應該看到以下結果:

Spring