SpringMvc 5.x 零配置开发环境搭建

基于 Spring Mvc 5.x 使用 Maven 搭建 SpringMvc Web项目,通过 Spring 注解与相关配置对项目进行创建和部署。

创建项目

创建一个普通Maven Web项目

添加依赖及配置

  • spring-web
  • spring-webmvc
  • javax.servlet-api
pom.xml
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?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>org.example</groupId>
<artifactId>shsxt-springmvc-zero</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>

<name>shsxt-springmvc-zero 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>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.3.18</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.3.18</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
<scope>provided</scope>
</dependency>
</dependencies>

<build>
<finalName>springmvc-zero</finalName>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.1</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>

添加SpringMvc配置类

SpringMvc,原来是通过xml进行配置(视图解析器、json转换器、文件上传解析器等),这里基于注解通过继承 WebMvcConfigurerAdapter 类并重写相关方法来进行配置(注意通过 @EnableWebMvc 注解启动Mvc环境)。

SpringMvc配置类

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
package com.example.zero.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.view.InternalResourceViewResolver;

// 配置类
@Configuration
// 在 @Configuration 注解的配置类中添加,用于为该应用添加 SpringMvc 功能
@EnableWebMvc
// 扫描包范围
@ComponentScan("com.example.zero")
public class MvcConfig {
/**
* 配置 视图解析器
* @return
*/
@Bean // 将方法返回的结果交给 Ioc 容器管理
public InternalResourceViewResolver viewResolver(){
// 获取视图解析器
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
// 设置前缀
viewResolver.setPrefix("/WEB-INF/jsp/");
// 设置后缀
viewResolver.setSuffix(".jsp");

// 返回视图解析器对象
return viewResolver;
}
}

怎么加载MvcConfig类呢?原来在构建SpringMvc应用时是通过容器启动时加载web.xml文件实现配置文件加载,现在的环境 web.xml 文件不存在,此时基于注解方式构建的Mvc应用,定义 WebInitializer 实现 WebApplicationInitializer 接口(该接口用来配置 servlet 3.0+ 配置的接口,用于替代web.xml 配置),当servlet容器启动Mvc时会通过 SpringServletContainerInitializer 接口进行加载,从而加载 Mvc 应用配置。实现该接口 onStartup 方法,加载应用信息配置。

入口文件

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
package com.example.zero;

import com.example.zero.config.MvcConfig;
import org.springframework.web.WebApplicationInitializer;
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
import org.springframework.web.servlet.DispatcherServlet;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.ServletRegistration;

/**
* 实现 WebApplicationInitializer 接口的类都可以在Web应用程序启动时被加载
*/
public class WebInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
// 基于java的配置类加载Spring的应用上下文
AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext();
// 注册 mvc 配置信息
ctx.register(MvcConfig.class);
// 设置 ServletContext 上下文信息
ctx.setServletContext(servletContext);
// 配置转发器 Dispatcher
ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx));
// 设置映射路径
servlet.addMapping("/");
// 启动时即实例化 Bean
servlet.setLoadOnStartup(1);
}
}

代码实现

添加视图

在 WEB-INF/jsp 目录下创建 index.jsp

index.jsp
1
2
3
4
5
<html>
<body>
<h2>Hello Spring Mvc!</h2>
</body>
</html>

添加控制器

1
2
3
4
5
6
7
8
9
10
11
12
package com.example.zero.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class HelloController {
@RequestMapping("/index")
public String index(){
return "index";
}
}