Linux教程網
Linux教程網
Linux教程網
您现在的位置: Linux教程網 >> UnixLinux >  >> Linux 程式設計 >> Linux 程式設計

Spring MVC 注解入門:讓第一個 DispatcherServlet 跑起來

本文是系列文章的起點,目標很單純:用純注解的方式把 Spring MVC 跑起來,後續的攔截器、DWR、MyBatis 整合都以這個專案為基礎。

(1)導入 jar 包

先準備 Spring 核心、Spring Web、Spring MVC 與 JSTL 等依賴,包結構如下圖所示:


(2)配置 web.xml

web.xml 要完成四件事:指定 Spring 根容器的設定檔位置、掛上字元編碼過濾器、註冊 ContextLoaderListener、以及宣告 DispatcherServlet 並把 *.html 交給它處理。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
    xmlns="https://java.sun.com/xml/ns/javaee" xmlns:web="https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    xsi:schemaLocation="https://java.sun.com/xml/ns/javaee https://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
    id="WebApp_ID" version="2.5">
    <display-name>spring_test</display-name>

    <!-- 配置文件位置,默認為 /WEB-INF/applicationContext.xml -->
    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:applicationContext.xml</param-value>
    </context-param>

    <!-- 字符集過濾器 -->
    <filter>
        <filter-name>characterEncodingFilter</filter-name>
        <filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
        <init-param>
            <param-name>encoding</param-name>
            <param-value>UTF-8</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>characterEncodingFilter</filter-name>
        <url-pattern>/*</url-pattern>
    </filter-mapping>

    <!-- 上下文 Spring 監聽器 -->
    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <!-- servlet 控制轉向 -->
    <servlet>
        <servlet-name>spring</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <!-- 配置文件 -->
        <init-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>classpath:context-dispatcher.xml</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>spring</servlet-name>
        <url-pattern>*.html</url-pattern>
    </servlet-mapping>

    <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
    </welcome-file-list>
</web-app>

(3)配置 src/context-dispatcher.xml

MVC 子容器這裡只做三件事:掃描注解、打開注解驅動、設定視圖解析器的前後綴。

<?xml version="1.0" encoding="UTF-8"?>
<beans default-lazy-init="true"
    xmlns="https://www.springframework.org/schema/beans" xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"
    xmlns:context="https://www.springframework.org/schema/context"
    xmlns:mvc="https://www.springframework.org/schema/mvc"
    xsi:schemaLocation="
        https://www.springframework.org/schema/beans
        https://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        https://www.springframework.org/schema/mvc
        https://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
        https://www.springframework.org/schema/context
        https://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <!-- 使用注解的包,包括子集 -->
    <context:component-scan base-package="com.geloin.spring" />
    <!-- 通過注解,把 URL 映射到 Controller 上,該標簽默認注冊
         DefaultAnnotationHandlerMapping 和 AnnotationMethodHandlerAdapter -->
    <mvc:annotation-driven />
    <!-- 視圖解析器 -->
    <bean id="viewResolver"
        class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="viewClass"
            value="org.springframework.web.servlet.view.JstlView" />
        <property name="prefix" value="/WEB-INF/pages/" />
        <property name="suffix" value=".jsp"></property>
    </bean>
</beans>

兩個容器,各司其職

這裡刻意保留了 Spring 慣例的雙容器結構:ContextLoaderListener 載入 applicationContext.xml,負責 Service、DAO 與交易等後端組件;DispatcherServlet 載入 context-dispatcher.xml,負責 Controller、視圖解析與請求映射。視圖解析器設定的前綴 /WEB-INF/pages/ 與後綴 .jsp 意味著 Controller 只要回傳字串 menu,就會導向 /WEB-INF/pages/menu.jsp;而 *.html 的映射則讓所有請求都有一致的對外路徑樣貌。

Copyright © Linux教程網 All Rights Reserved