SpringMVC—Web.xml中DispatcherServlet配置文件以及Maven搭建


執行流程:

  url地址發送請求 –> 通過web.xml判斷是否為 .action結尾的路徑->將請求提交給DispatcherServlet,它會委托應用系統的其他模塊負責對請求進行真正的處理工作 -> 進入spring配置文件 ->查詢一個或多個HandlerMapping這個類的配置 -> 通過配置找到請求的key -> 再找到這個key 的值的對應id名的bean配置 -> 通過這個bean,進入controller類進行處理 -> 返回一個視圖ModelAndView(比如“hello”) -> Dispathcher查詢一個或多個ViewResolver視圖解析器 –> 並按ModelAndView的要求找到對應的視圖對象文件 -> 響應(視圖對象負責渲染返回給客戶端)。

 

 

SpringMVC框架,最關鍵的其中一個任務就是配置DispatcherServlet

DispatcherServlet是前置控制器,配置在web.xml文件中的。攔截匹配的請求,Servlet攔截匹配規則要自己定義,把攔截下來的請求,依據相應的規則分發到目標Controller來處理,是配置spring MVC的第一步。

但是,我們在搭建SpringMVC框架時,往往總是把DispatcherServlet的配置文件放錯位置。網上有的說:springMVC.xml(暫且命名這么一個springDispatcherServletMVC的dispatchservlet配置文件)應該放在WEB-INF下面(與web.xml放一起);有的說:應該直接把springMVC.xml放在src文件夾下面;有的說:在java resources下面新建一個config的resource folder,把配置文件都放在這個資源文件夾下面。那么到底哪種是正確的呢?答案是:這個要根據web.xml中DispatcherServlet的配置聲明有關系。
DispatcherServlet前端控制器配置文件contextConfigLocation存放位置:

  1.當web.xml中DispatcherServlet,沒有明確DispatcherServlet前端控制器配置文件的位置時,則系統默認DispatcherServlet前端控制器配置文件放在WEB-INF文件夾下。

<!-- org.springframework.web.servlet.DispatcherServlet 攔截所有請求 -->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
     <servlet-name>springmvc</servlet-name>
     <url-pattern>*.action</url-pattern>
   </servlet-mapping>

  2.當web.xml中DispatcherServlet,已經明確DispatcherServlet前端控制器配置文件的位置時,則必須將前端控制器的配置文件放在src下面,或者在java resources下面新建一個config的resource folder,把配置文件都放在這個資源文件夾下面。

<!-- org.springframework.web.servlet.DispatcherServlet 攔截所有請求 -->
    <servlet>
        <servlet-name>springDispatcherServlet</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <init-param>
            <!--contextConfigLocation: DispatcherServlet的屬性,默認contextConfigLocation -->
            <param-name>contextConfigLocation</param-name>
            <!-- 項目啟動,加載springmvc.xml(命名要一致) -->
            <param-value>classpath:springmvc.xml</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <!-- Map all requests to the DispatcherServlet for handling -->
    <!-- 將所有請求映射到 DispatcherServlet 進行處理 -->

    <servlet-mapping>
      <servlet-name>springmvc</servlet-name>
      <url-pattern>*.action</url-pattern>
    </servlet-mapping>

  3.如果有多個配置文件,可以在 <param-value>標記中用逗號作分隔符。在web.xml里配置Listener。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
                        http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
  version="3.1">
    <!-- spring上下文裝載器(監聽器),自動加載配置文件 -->
    <listener>
         <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>   
    </listener>
    <!-- Spring 加載配置文件位置,使用*匹配 -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:resource/spring/spring*.xml</param-value>
    </context-param>
    
    <!-- <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:spring-base.xml,classpath:spring-another.xml</param-value>
    </context-param> -->
    
   <!--  <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath*:**/spring*.xml</param-value>
    </context-param> -->
    
    <!--   多個配置文件可以在web.xml里用逗號分隔寫入-->
    <!-- <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:resource/spring/springmvc.xml,resource/spring/springmvc.xml</param-value>
    </context-param> -->
</web-app>    

  多個配置文件里的交叉引用可以用ref的external或bean解決

<bean id="userService" class="domain.user.service.impl.UserServiceImpl"> 
        <property name="entity">
             <ref bean="dbBean"/>
         </property> 
    </bean>

如果指定了要加載的文件,則會去加載相應的xml,而不會去加載/WEB-INF/下的配置文件。 如果沒有指定的話,默認會去/WEB-INF/下加載配置文件。


 spring文件配置,這里放在resource目錄下

<?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:aop="http://www.springframework.org/schema/aop"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:task="http://www.springframework.org/schema/task"
    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/aop
  http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
  http://www.springframework.org/schema/mvc      
  http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
  http://www.springframework.org/schema/task
  http://www.springframework.org/schema/task/spring-task-3.0.xsd" >
  
    <!-- 映射處理器,simpleUrlHandlerMapping(實現org.springframework.web.servlet.mvc.Controller)
        和BeanNameUrlHandlerMapping(繼承AbstractController)使用較多;
      ControllerClassNameHandlerMapping:
        支持慣例優先原則 在它的應用上下文中找出所有不同的處理器(handler)(或Controller)bean, 並去掉名稱中的Controller,來定義它的處理器映射。
      Removed "Controller" from class name 全部轉為小寫 前面加 “/” 后面附 "*“ ,結合multiActionController使用
-->
   <bean id="simpleUrlHandlerMapping" class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
        <property name="mappings">
            <props>
                <prop key="/index.action">startControl</prop>
            </props>
        </property>    
    </bean>
    <!-- 定義處理類 -->
    <bean id="startControl" class="com.zzk.Controller"></bean>
    <!-- 用於邏輯視圖到真正視圖的隱射 -->
    <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass" value="org.springframework.web.servlet.view.JstlView"></property>
        <property name="prefix" value="/WEB-INF/jsp/"/>
        <property name="suffix" value=".jsp"/>
    </bean>
</beans>

pom.xml文件

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">
  <modelVersion>4.0.0</modelVersion>
  <groupId>com.shop</groupId>
  <artifactId>mavenTest</artifactId>
  <packaging>war</packaging>
  <version>0.0.1-SNAPSHOT</version>
  <name>mavenTest Maven Webapp</name>
  <url>http://maven.apache.org</url>
  <dependencies>
    <dependency>
      <groupId>junit</groupId>
      <artifactId>junit</artifactId>
      <version>3.8.1</version>
      <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>org.springframework.web.servlet</artifactId>
        <version>3.2.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>org.springframework.expression</artifactId>
        <version>3.2.1.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>javax.servlet-api</artifactId>
        <version>4.0.1</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
            <artifactId>org.springframework.web</artifactId>
            <version>3.2.2.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
        <version>1.2</version>
    </dependency>
  </dependencies>

</project>

然后就是寫controller和jsp,只要目錄正確,搭建任務就成功了。

 如果要在一個controller中處理多個請求,則必須繼承MultiActionController。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM