Spring整合SpringMVC
# Spring整合SpringMVC⚡️
# 1、新建springmvc.xml配置文件
新建一个SpringMVC的springmvc.xml配置文件。
<?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 https://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc https://www.springframework.org/schema/mvc/spring-mvc.xsd">
</beans>
1
2
3
4
5
6
7
8
9
10
2
3
4
5
6
7
8
9
10
# 2、配置注解驱动
配置注解驱动后,会自动开启处理器映射器和处理器适配器。
<mvc:annotation-driven/>
1
# 3、静态资源过滤配置
让SpringMVC不处理静态资源,但是请求还是走的前端控制器,只不过是不再处理静态资源。
<mvc:default-servlet-handler/>
1
# 4、配置上下文扫描路径
这里配置上下文扫描路径,去扫描com.singerw.controller
下的组件。
<context:component-scan base-package="com.singerw.controller"/>
1
# 5、配置视图解析器
如果页面用到jsp jstl视图,需要配置视图解析器
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/jsp/"/>
<property name="suffix" value=".jsp"/>
</bean>
1
2
3
4
2
3
4
# 6、配置文件上传
需要则配置
在SpringMVC配置文件中定义一个bean 类型为CommonsMultipartResolver
SpringMVC中专门封装了一个类CommonsMultipartResolver来处理文件上传,所以需要在 SpringMVC的配置件中加入一个bean;配置bean的代码如下:
<bean id="multipartResolver" class="org.springframework.web.multipart.commons.CommonsMultipartResolver">
</bean>
1
2
3
2
3
加入一个bean用来处理文件上传,这里采用的是commons-fileupload:commons-fileupload:1.4
<!-- 文件上传 commons-fileupload -->
<dependency>
<groupId>commons-fileupload</groupId>
<artifactId>commons-fileupload</artifactId>
<version>1.4</version>
</dependency>
<!-- 文件上传 commons-io -->
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.6</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
2
3
4
5
6
7
8
9
10
11
12
# 7、配置web.xml
需要保证一个web.xml文件,且版本是4.0
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd"
version="4.0">
</web-app>
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
# 8、配置Spring文件
<!--1、配置Spring文件-->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
1
2
3
4
5
6
7
8
2
3
4
5
6
7
8
这里经常犯一个错误,把ContextLoaderListener
写出ContextCleanupListener
,导致bean无法注入,一直报错,配置时需要认真谨慎。
# 9、加载SpringMVC的核心
配置DispatcherServlet,加载SpringMVC的核心(请求分发器/前端控制器)
<servlet>
<servlet-name>springDispatcherServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<!--配置DispatcherServlet要绑定Spring的配置文件-->
<param-value>classpath:spring-mvc.xml</param-value>
</init-param>
<!--启动级别1:和服务器一起启动-->
<load-on-startup>1</load-on-startup>
</servlet>
<!-- 2.1将所有请求映射到 DispatcherServlet 进行处理 -->
<servlet-mapping>
<servlet-name>springDispatcherServlet</servlet-name>
<!--2.2配置url-pattern 路径
/:只匹配所有的请求,不会匹配jsp等页面
/*:匹配所有的请求,包括匹配jsp等页面-->
<url-pattern>/</url-pattern>
</servlet-mapping>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 10、配置Post请求过滤器
spring提供的characterEncodingFilter配置,配置后解决post请求页面乱码问题!
<!--3、Post请求过滤器-->
<!--spring提供的characterEncodingFilter配置 -->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<!--观察发现这个类 CharacterEncodingFilter 有一个属性 encoding 所以提供一个initparm以及 value -->
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<!--给CharacterEncodingFilter类的对象进行初始化的赋值request.setCharacterEncoding-->
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
<!--响应编码的设置 true 设置response -->
<init-param>
<param-name>forceEncoding</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!--3.1 过滤器对哪些资源进行过滤呀 -->
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 11、配置Session过期时间
<!--4、Session过期时间:15分钟-->
<session-config>
<session-timeout>15</session-timeout>
</session-config>
1
2
3
4
2
3
4
# 12、创建控制器进行测试
首先需求导入json的三大件jar包
com.fasterxml.jackson.core:jackson-databind:2.12.3
com.fasterxml.jackson.core:jackson-core:2.12.3
com.fasterxml.jackson.core:jackson-annotations:2.12.3
<!-- jackson-databind -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.3</version>
</dependency>
<!-- jackson-core -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.12.3</version>
</dependency>
<!-- jackson-annotations -->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.12.3</version>
</dependency>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
然后创建控制器:
@RestController
@RequestMapping("/api")
public class ArticleController {
@Autowired
private ArticleService articleService;
@GetMapping("/artAll")
public ResponseData<Article> getArticleList(
@RequestParam(name = "page", required = true, defaultValue = "0") int page,
@RequestParam(name = "limit", required = true, defaultValue = "10") int limit,
@RequestParam(name = "keywords", required = true, defaultValue = "") String keywords) {
return articleService.getArticleList(page, limit, keywords);
}
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
2
3
4
5
6
7
8
9
10
11
12
13
14
15
测试结果:
# 13、整合完毕
✌️✌️完结撒花✌️✌️
编辑 (opens new window)