文章目录
设置pom.xml依赖,添加spring-mvc依赖
<?xml version="1.0" encoding="UTF-8"?>
<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/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.bailiang</groupId>
<artifactId>myshop</artifactId>
<version>1.0.0-SNAPSHOT</version>
<packaging>war</packaging>
<properties>
<spring-version>4.3.24.RELEASE</spring-version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>${spring-version}</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-api</artifactId>
<version>1.7.26</version>
</dependency>
<dependency>
<groupId>org.apache.commons</groupId>
<artifactId>commons-lang3</artifactId>
<version>3.1</version>
</dependency>
</dependencies>
</project>
设置web.xml
添加Spring-context配置文件路径和监听器,用于Spring自动创建ApplicationContext对象
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring-context*.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
添加Spring过滤器,用于解决中文显示问题
<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>
配置Spring的核心分发器:DispatcherServlet
<servlet>
<servlet-name>springServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!-- 添加一个spring-mvc.xml文件,用于配置SpringMVC -->
<param-value>classpath*:/spring-mvc*.xml</param-value>
</init-param>
<!-- 启动优先级 指定Spring的Servlet最先启动,否则有jvm决定启动顺序 -->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springServlet</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
设置spring-mvc.xml
注意添加xmlns:mvc
命名空间和命名空间路径
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
">
<!-- 默认的注解映射的支持件 -->
<mvc:annotation-driven />
<!-- 加载本配置文件中属性值的配置文件,这里属性的配置值保存在myshop.properties文件中 -->
<context:property-placeholder ignore-unresolvable="true" location="classpath:myshop.properties" />
<!-- 设置组件扫描过滤器,只扫描包"com.bailiang.myshop"下的组件 -->
<context:component-scan base-package="com.bailiang.myshop" use-default-filters="false">
<!-- 设置扫描规则:include(包含)的过滤器是扫描Annotion(注解),这里只扫描@Controller注解 -->
<!-- 也可以设置exclude(排除),这里没有用到 -->
<context:include-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component-scan>
<!-- 定义视图文件解析器,把逻辑视图路径解析成实际的视图文件 -->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!-- 设置逻辑视图解析为实际视频文件时添加的前缀、后缀 -->
<!-- value的值引用自myshop.properties中的设置 -->
<property name="prefix" value="${web.view.prefix}" />
<property name="suffix" value="${web.view.suffix}" />
</bean>
<!-- 静态资源映射 -->
<!-- /*是路径匹配,/**是全路径匹配 -->
<!-- 如目录结构:/a/b/c/d,则/*,只能匹配/a,而/**可以匹配/a、/a/b、/a/b/c、 /a/b/c/d -->
<mvc:resources mapping="/static/**" location="/static/" cache-period="31536000"/>
</beans>
添加myshop.properties文件并添加配置
#设置逻辑视图的前缀,一般是视图的路径
web.view.prefix=/WEB-INF/views/
#设置逻辑视图的后缀,一般是真实视图文件的扩展名
web.view.suffix=.jsp
修改spring-context.xml配置,去除重复配置
原配置文件:
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
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
">
<!-- 添加注解支持 -->
<context:annotation-config />
<!-- 添加注解扫描的包 -->
<context:component-scan base-package="com.bailiang.myshop"/>
</beans>
因为在spring-mvc.xml中添加了对 @Controller
注解的支持,所以在spring-context.xml可以去掉该支持,修改如下:
<!-- 添加注解扫描的包 -->
<context:component-scan base-package="com.bailiang.myshop">
<!-- exlude(去除)对@Controller注解的支持 -->
<context:exlude-filter type="annotation" expression="org.springframework.stereotype.Controller"/>
</context:component>