Spring MVC簡單URL處理程式對映


以下範例顯示如何使用Spring Web MVC框架來實現一個簡單URL處理程式對映。 SimpleUrlHandlerMapping類分別顯式地將URL對映到相應的控制器上。

所下所示組態 -

<beans>

   <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.SimpleUrlHandlerMapping">
      <property name="mappings">
         <props>
            <prop key="/welcome.html">welcomeController</prop>           
            <prop key="/helloWorld.html">helloController</prop>
         </props>
      </property>
   </bean>

   <bean id="helloController" class="com.yiibai.springmvc.HelloController" />

   <bean id="welcomeController" class="com.yiibai.springmvc.WelcomeController"/>  
</beans>

例如,使用上面的組態,如果URI:

  • 對於/helloWorld.html請求,DispatcherServlet將請求轉發到HelloController類。
  • 對於/welcome.htmlDispatcherServlet會將請求轉發給WelcomeController類。

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

  1. 建立一個名稱為 SimpleUrlHandlerMapping 的動態WEB專案。
  2. com.yiibai.springmvc 包下建立兩個Java類HelloControllerWelcomeController
  3. jsp子檔案夾下建立兩個檢視檔案:hello.jspwelcome.jsp
  4. 最後一步是建立所有源和組態檔案的內容並執行應用程式,詳細如下所述。

完整的專案檔案目錄結構如下所示 -

HelloController.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.AbstractController;

public class HelloController extends AbstractController{

   @Override
   protected ModelAndView handleRequestInternal(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("hello");
      model.addObject("message", "Hello World!");
      return model;
   }
}

WelcomeController.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.AbstractController;

public class WelcomeController extends AbstractController{

   @Override
   protected ModelAndView handleRequestInternal(HttpServletRequest request,
      HttpServletResponse response) throws Exception {
      ModelAndView model = new ModelAndView("welcome");
      model.addObject("message", "Welcome!");
      return model;
   }
}

SimpleUrlHandlerMapping-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.SimpleUrlHandlerMapping">
      <property name="mappings">
         <props>
            <prop key="/welcome.html">welcomeController</prop>           
            <prop key="/helloWorld.html">helloController</prop>
         </props>
      </property>
   </bean>

   <bean id="helloController" class="com.yiibai.springmvc.HelloController" />

   <bean id="welcomeController" class="com.yiibai.springmvc.WelcomeController"/>   
</beans>

hello.jsp 的程式碼如下所示 -

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

welcome.jsp 的程式碼如下所示 -

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

完成建立源和組態檔案後,發布應用程式到Tomcat伺服器。

現在啟動Tomcat伺服器,現在嘗試存取URL => http://localhost:8080/SimpleUrlHandlerMapping/helloWorld.html ,如果Spring Web應用程式沒有問題,應該看到以下結果:

當存取URL => http://localhost:8080/SimpleUrlHandlerMapping/welcome.html , 如果Spring Web應用程式沒有問題,應該看到以下結果: