Spring 配置DispatcherServlet

jopen 10年前發布 | 60K 次閱讀 Spring Spring MVC Web框架

本文描述了web.xml最基本配置方式。

Spring MVC的核心是DispatcherServlet,作為Spring MVC的前端控制器;

和任何Servlet一樣,我們需要在web.xml文件中配置DispatcherServlet;

下面的描述以這個web.xml為例:

<?xml version="1.0" encoding="UTF-8"?>  
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">  
  
    <!-- The definition of the Root Spring Container shared by all Servlets and Filters -->  
    <context-param>  
        <param-name>contextConfigLocation</param-name>  
        <param-value>  
            /WEB-INF/iSystem-beans.xml,  
        </param-value>  
    </context-param>  
      
    <!-- Creates the Spring Container shared by all Servlets and Filters -->  
    <listener>  
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
    </listener>  
  
    <!-- Processes application requests -->  
    <servlet>  
        <servlet-name>iSystem</servlet-name>  
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
        <init-param>  
            <param-name>contextConfigLocation</param-name>  
            <param-value>/WEB-INF/iSystem-servlet.xml</param-value>  
        </init-param>  
        <load-on-startup>1</load-on-startup>  
    </servlet>  
          
    <servlet-mapping>  
        <servlet-name>iSystem</servlet-name>  
        <url-pattern>/</url-pattern>  
        <url-pattern>*.htm</url-pattern>  
    </servlet-mapping>  
  
</web-app>

在其中的<servlet>標簽下,定義了DispatcherServlet。

注意:在DispatcherServlet載入后,它將從XML文件中載入Spring的應用上下文,這個XML文件的名字取決于Servlet的名字,或者由init-param來指定。

如果我們不設置init-param,形如:

<servlet>  
    <servlet-name>iSystem</servlet-name>  
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>  
    <load-on-startup>1</load-on-startup>  
</servlet>

這時,DispatcherServlet載入后將默認去加載  /WEB-INF/iSystem-servlet.xml文件,iSystem就是上面定義的servlet-name。

如果沒有這個文件,將會報錯:

java.io.FileNotFoundException: Could not open ServletContext resource [/WEB-INF/iSystem-servlet.xml]

因此我們如果省略init-param的話,需要按下圖放置該xml:

09163748_nsku.png

附上模板工程自帶的servlet.xml文件內容:

<?xml version="1.0" encoding="UTF-8"?>  
<beans  
    xmlns="http://www.springframework.org/schema/beans"  
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  
    xmlns:tx="http://www.springframework.org/schema/tx"  
    xmlns:context="http://www.springframework.org/schema/context"    
    xmlns:mvc="http://www.springframework.org/schema/mvc"    
    xsi:schemaLocation="http://www.springframework.org/schema/beans   
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd   
    http://www.springframework.org/schema/tx   
    http://www.springframework.org/schema/tx/spring-tx-3.0.xsd  
    http://www.springframework.org/schema/context  
    http://www.springframework.org/schema/context/spring-context-3.0.xsd  
    http://www.springframework.org/schema/mvc  
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd"> 
  
   <mvc:resources mapping="/resources/**" location="/resources/" /> 
      <!-- 此處的name的作用就是:http://localhost:8080/項目名/welcome  而MytestController是我的一個攔截器-->
    <bean name="/welcome" class="com.unis.ucsm.control.MytestController"></bean>
    <!-- Resolves views selected for rendering by @Controllers to .jsp resources in the /WEB-INF/views directory -->  
    <beans:bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">  
        <beans:property name="prefix" value="/WEB-INF/views/" />  
        <beans:property name="suffix" value=".jsp" />  
    </beans:bean>  
      
    <context:component-scan base-package="com.codeevoship.app" />  
</beans:beans>

接下來再為DispatcherServlet指定需要處理的URL:

<servlet-mapping>  
    <servlet-name>iSystem</servlet-name>  
    <url-pattern>/</url-pattern>  
    <url-pattern>*.htm</url-pattern>  
</servlet-mapping>

當然這里還可以加上*.png、*.gif等等等等……

攔截地址:

  <url-pattern>/</url-pattern>:這種方式攔截了類似/user/login的地址;會導致靜態資源無法訪問。

  <url-pattern>/*</url-pattern>:錯誤的攔截方式,可以走到Action中,但轉發到jsp時再次被攔截,不能訪問到jsp。

  <url-pattern>/path/*</url-pattern>:攔截包含/path/路徑的請求。

  <url-pattern>*.do</url-pattern>:簡單,實用的攔截方式,不存在靜態資源訪問的問題。

到這里DispatcherServlet的配置就完成了,這里再說說其他內容。

雖然我們已經加載了iSystem-servlet.xml,雖然可以把全項目所有的bean全都定義在這一個xml中……

推薦分層、分模塊分別定義,比如iSystem-security.xml/iSystem-data.xml/iSystem-service.xml……

這就用到了上下文載入器,最常用的ContextLoaderListener:

<!-- Creates the Spring Container shared by all Servlets and Filters -->  
<listener>  
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>  
</listener>

這個載入器可以載入除DispatcherServlet載入的配置文件之外的其他上下文配置文件,通過下述方式:

<context-param>  
    <param-name>contextConfigLocation</param-name>  
    <param-value>  
        /WEB-INF/iSystem-security.xml,  
        /WEB-INF/iSystem-data.xml,  
    </param-value>  
</context-param>

param-value中就是需要加載的文件,以逗號分隔。

以上就是web.xml的最基本配置。

創建攔截器類MytestController:

package com.unis.ucsm.control;

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 MytestController extends AbstractController{

@Override
protected ModelAndView handleRequestInternal(HttpServletRequest request,
        HttpServletResponse response) throws Exception {
    // TODO Auto-generated method stub
    System.out.println("攔截器被調用!");
    return new ModelAndView("test");
}

}</pre>

如果我們在瀏覽器里面訪問

http://localhost:8080/項目名/welcome;當然我這里服務的是http://localhost:8080/ucsm/welcome

這時候就會訪問/WEB-INF/views/文件夾下面的test.jsp文件(當然這個文件是你事先就創建好的)。

為什么會反問這個文件而不是別的文件呢,因為你在前面設置下面這個:

     <beans:property name="prefix" value="/WEB-INF/views/" />  
        <beans:property name="suffix" value=".jsp" />

這個的意思是一旦你的攔截器返回的ModelAndView(String str)中的類容(我的ModelAndView("test"))會告訴web容器要訪問的頁面是

<beans:property name="prefix" value="prefixvlaue" /> +str+<beans:property name="suffix" value="suffixvalue" />而我上面配置prefixvlaue=“/WEB-INF/views/”   str="test" suffixvalue=“.jsp”所以加在一起就是訪問/WEB-INF/views/test.jsp

來自:Spring MVC 教程,快速入門,深入分析

 本文由用戶 jopen 自行上傳分享,僅供網友學習交流。所有權歸原作者,若您的權利被侵害,請聯系管理員。
 轉載本站原創文章,請注明出處,并保留原始鏈接、圖片水印。
 本站是一個以用戶分享為主的開源技術平臺,歡迎各類分享!