POM
在 pom.xml
配置文件中增加 org.springframework:spring-webmvc
依賴
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.17.RELEASE</version>
</dependency>
配置 web.xml
CharacterEncodingFilter
配置字符集過濾器,用於解決中文編碼問題
<filter>
<filter-name>encodingFilter</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>
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
DispatcherServlet
配置 Spring 的 Servlet 分發器處理所有 HTTP 的請求和響應
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:/spring-mvc*.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
配置 Spring MVC
創建一個名爲 spring-mvc.xml
文件來配置 MVC
<?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: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.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<description>Spring MVC Configuration</description>
<!-- 加載配置屬性文件 -->
<context:property-placeholder ignore-unresolvable="true" location="classpath:myshop.properties"/>
<!-- 使用 Annotation 自動註冊 Bean,只掃描 @Controller -->
<context:component-scan base-package="com.lusifer.myshop" use-default-filters="false">
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 默認的註解映射的支持 -->
<mvc:annotation-driven />
<!-- 定義視圖文件解析 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="${web.view.prefix}"/>
<property name="suffix" value="${web.view.suffix}"/>
</bean>
<!-- 靜態資源映射 -->
<mvc:resources mapping="/static/**" location="/static/" cache-period="31536000"/>
</beans>
相關配置說明:
-
context:property-placeholder
:動態加載屬性配置文件以變量的方式引用需要的值 -
context:component-scan
:當前配置文件爲 MVC 相關,故只需要掃描包含@Controller
的註解即可,由於spring-context.xml
配置文件中也配置了包掃描,所以還需要排除@Controller
的註解掃描。 -
InternalResourceViewResolver
:視圖文件解析器的一種,用於配置視圖資源的路徑和需要解釋的視圖資源文件類型,這裏有兩個需要配置的屬性prefix
(前綴)以及suffix
(後綴)。prefix
:配置視圖資源路徑,如:/WEB-INF/views/
suffix
:配置視圖資源類型,如:.jsp
-
mvc:resources
:靜態資源映射,主要用於配置靜態資源文件存放路徑,如:JS、CSS、Image 等
系統相關配置
在 spring-mvc.xnl
中,我們配置了 <context:property-placeholder ignore-unresolvable="true" location="classpath:myshop.properties"/>
用於動態加載屬性配置文件,實際開發中我們會將系統所需的一些配置信息封裝到 .properties
配置文件中便於統一的管理。
創建一個名爲 myshop.properties
的配置文件,內容如下:
#============================#
#==== Framework settings ====#
#============================#
# \u89c6\u56fe\u6587\u4ef6\u5b58\u653e\u8def\u5f84
web.view.prefix=/WEB-INF/views/
web.view.suffix=.jsp
去掉 Spring 配置的重複掃描
由於 spring-mvc.xml
中已經配置了 @Controller
註解的掃描而 spring-context.xml
中配置的是掃描全部註解,故在這裏需要將 @Controller
註解的掃描配置排除。
修改 spring-context.xml
配置:
<!-- 使用 Annotation 自動註冊 Bean,在主容器中不掃描 @Controller 註解,在 SpringMVC 中只掃描 @Controller 註解。-->
<context:component-scan base-package="com.funtl.my.shop">
<context:exclude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>