POM.xml
<?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.shsxt</groupId>
<artifactId>springmvc_1</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>springmvc_1 Maven Webapp</name>
<!-- FIXME change it to the project‘s website -->
<url>http://www.example.com</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<dependencies>
<!--spring web-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
<!--springMvc-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.4.RELEASE</version>
</dependency>
<!--web servlet 创建servlet或jsp使用-->
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
<scope>provided</scope>
</dependency>
<!-- 添加json 依赖jar包 -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.10.0</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.10.0</version>
</dependency>
<!--添加commons-fileupload 文件上传-->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.3.2</version>
</dependency>
</dependencies>
<build>
<finalName>springmvc_1</finalName>
<!--编译环境插件-->
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
<!--jetty插件(或者tomcat也行)-->
<plugin>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-maven-plugin</artifactId>
<version>9.4.27.v20200227</version>
<configuration>
<scanIntervalSeconds>10</scanIntervalSeconds>
<!--设置端口-->
<httpConnector>
<port>8080</port>
</httpConnector>
<!--设置项目路径-->
<webAppConfig>
<contextPath>/</contextPath>
</webAppConfig>
</configuration>
</plugin>
</plugins>
</build>
</project>
Web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="3.0"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
<!-- 编码过滤 utf-8 -->
<filter>
<description>char encoding filter</description>
<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>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<!-- servlet请求分发器:中央控制器(项目一启动,对应的项目就启动了) -->
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:servlet-context.xml</param-value>
</init-param>
<!-- 表示启动容器时初始化该Servlet 值越小越先-->
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<!-- 这是拦截请求(满足以下请求的才会被中央控制器拦截), "/"代表拦截所有请求,"*.do"拦截所有.do请求 -->
<!-- <url-pattern>/</url-pattern> -->
<url-pattern>/</url-pattern>
</servlet-mapping>
</web-app>
servlet-context.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
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:component-scan base-package="com.shsxt.springmvc.controller"/>
<!-- 使?默认的 Servlet 来响应静态?件 -->
<mvc:default-servlet-handler/>
<!-- 开启注解驱动-->
<mvc:annotation-driven/>
<!-- mvc 请求映射 处理器与适配器配置 -->
<mvc:annotation-driven>
<mvc:message-converters>
<bean
class="org.springframework.http.converter.StringHttpMessageConverter" />
<bean
class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
</mvc:message-converters>
</mvc:annotation-driven>
<!-- 配置视图解析器 -->
<bean
class="org.springframework.web.servlet.view.InternalResourceViewResolver"
id="internalResourceViewResolver">
<!-- 前缀:在WEB-INF?录下的jsp?录下 -->
<property name="prefix" value="/WEB-INF/jsp/"/>
<!-- 后缀:以.jsp结尾的资源 -->
<property name="suffix" value=".jsp"/>
</bean>
<!-- ?件上传 -->
<bean id="multipartResolver"
class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
<!-- 允许?件上传的最?尺? -->
<property name="maxUploadSize">
<value>104857600</value>
</property>
<!--设置?件放?临时?件夹的最???限制。
此值是阈值,低于此值,则保存在内存中,如?于此值,则?成硬盘上的临时?件。
-->
<property name="maxInMemorySize">
<value>4096</value>
</property>
</bean>
<!--拦截器配置方式1: 对所有controller有效-->
<!-- <mvc:interceptors>
<bean id="MyInterceptor" class="com.shsxt.springmvc.interceptor.MyInterceptor"></bean>
</mvc:interceptors>
-->
<mvc:interceptors>
<!--拦截器链
先配置的拦截器先执行,
先配置拦截器的preHandle方法,先执行
先配置拦截器的posthandle和afterComoletion方法,后执行
-->
<!--指定拦截器-->
<mvc:interceptor>
<!--设置拦截的路径 /**代表拦截所有资源,拦截路径可以使用通配符-->
<mvc:mapping path="/**"/>
<!--设置不拦截的路径 不拦截url下的所有资源,支持通配符可以定义多个-->
<mvc:exclude-mapping path="/url/*"/>
<!--不拦截hello.do这个资源 .do是配置文件配置的资源格式-->
<mvc:exclude-mapping path="/hello.do"/>
<bean id="myInterceptor01" class="com.shsxt.springmvc.interceptor.MyInterceptor"/>
</mvc:interceptor>
<mvc:interceptor>
<!--设置拦截的路径 /**代表拦截所有资源,拦截路径可以使用通配符-->
<mvc:mapping path="/**"/>
<!--设置不拦截的路径 不拦截url下的所有资源,支持通配符可以定义多个-->
<mvc:exclude-mapping path="/url/*"/>
<!--不拦截hello.do这个资源 .do是配置文件配置的资源格式-->
<mvc:exclude-mapping path="/hello.do"/>
<bean id="myInterceptor02" class="com.shsxt.springmvc.interceptor.MyInterfactor02"/>
</mvc:interceptor>
</mvc:interceptors>
</beans>
原文:https://www.cnblogs.com/areUkiding/p/14389066.html