基于 Spring Mvc 5.x 使用 Maven 搭建 SpringMvc Web项目,通过 Spring 注解与相关配置对项目进行创建和部署。
创建项目
创建一个普通Maven Web项目
添加依赖及配置
- spring-web
- spring-webmvc
- javax.servlet-api
pom.xml1 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> <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
@EnableWebMvc
@ComponentScan("com.example.zero") public class MvcConfig {
@Bean 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;
public class WebInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.register(MvcConfig.class); ctx.setServletContext(servletContext); ServletRegistration.Dynamic servlet = servletContext.addServlet("dispatcher", new DispatcherServlet(ctx)); servlet.addMapping("/"); servlet.setLoadOnStartup(1); } }
|
代码实现
添加视图
在 WEB-INF/jsp 目录下创建 index.jsp
index.jsp1 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"; } }
|