學(xué)習(xí)完整課程請(qǐng)移步 互聯(lián)網(wǎng) Java 全棧工程師
本節(jié)視頻
POM
在 pom.xml
配置文件中增加 org.springframework:spring-webmvc
依賴(lài)
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.3.17.RELEASE</version>
</dependency>
配置 web.xml
CharacterEncodingFilter
配置字符集過(guò)濾器巧颈,用于解決中文編碼問(wèn)題
<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 分發(fā)器處理所有 HTTP 的請(qǐng)求和響應(yīng)
<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
創(chuàng)建一個(gè)名為 spring-mvc.xml
文件來(lái)配置 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 自動(dòng)注冊(cè) 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>
<!-- 默認(rèn)的注解映射的支持 -->
<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>
<!-- 靜態(tài)資源映射 -->
<mvc:resources mapping="/static/**" location="/static/" cache-period="31536000"/>
</beans>
相關(guān)配置說(shuō)明:
context:property-placeholder
:動(dòng)態(tài)加載屬性配置文件以變量的方式引用需要的值context:component-scan
:當(dāng)前配置文件為 MVC 相關(guān)盅粪,故只需要掃描包含@Controller
的注解即可矢否,由于spring-context.xml
配置文件中也配置了包掃描憨闰,所以還需要排除@Controller
的注解掃描。-
InternalResourceViewResolver
:視圖文件解析器的一種厢呵,用于配置視圖資源的路徑和需要解釋的視圖資源文件類(lèi)型维费,這里有兩個(gè)需要配置的屬性prefix
(前綴)以及suffix
(后綴)。-
prefix
:配置視圖資源路徑契耿,如:/WEB-INF/views/
-
suffix
:配置視圖資源類(lèi)型,如:.jsp
-
mvc:resources
:靜態(tài)資源映射蛤吓,主要用于配置靜態(tài)資源文件存放路徑宵喂,如:JS、CSS会傲、Image 等
系統(tǒng)相關(guān)配置
在 spring-mvc.xnl
中锅棕,我們配置了 <context:property-placeholder ignore-unresolvable="true" location="classpath:myshop.properties"/>
用于動(dòng)態(tài)加載屬性配置文件,實(shí)際開(kāi)發(fā)中我們會(huì)將系統(tǒng)所需的一些配置信息封裝到 .properties
配置文件中便于統(tǒng)一的管理淌山。
創(chuàng)建一個(gè)名為 myshop.properties
的配置文件裸燎,內(nèi)容如下:
#============================#
#==== Framework settings ====#
#============================#
# \u89c6\u56fe\u6587\u4ef6\u5b58\u653e\u8def\u5f84
web.view.prefix=/WEB-INF/views/
web.view.suffix=.jsp
去掉 Spring 配置的重復(fù)掃描
由于 spring-mvc.xml
中已經(jīng)配置了 @Controller
注解的掃描而 spring-context.xml
中配置的是掃描全部注解,故在這里需要將 @Controller
注解的掃描配置排除泼疑。
修改 spring-context.xml
配置:
<!-- 使用 Annotation 自動(dòng)注冊(cè) 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>