Spring 配置DispatcherServlet

1、Spring MVC 教程,快速入門,深刻分析
前端

2、SpringMVC 基礎教程 簡單入門實例java

SpringMVC實例代碼(maven工程)
web

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

Spring MVC的核心是DispatcherServlet,做爲Spring MVC的前端控制器;瀏覽器

和任何Servlet同樣,咱們須要在web.xml文件中配置DispatcherServlet;spring-mvc

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

<?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。app

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

若是咱們不設置init-param,形如:maven

<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:


附上模板工程自帶的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");
	}
}

若是咱們在瀏覽器裏面訪問

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

相關文章
相關標籤/搜索