【SpringBoot框架02】SpringMVC 零配置的创建与部署
思维导图
1.创建Spring MVC web工程
2.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.lcy</groupId>
<artifactId>springmvc01</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging>
<name>springmvc01 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>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.2.16.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.2.16.RELEASE</version>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>3.0.1</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>2.3.2</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
</plugins>
</build>
</project>
3.添加源代码
@Controller
public class HelloController {
@RequestMapping("/index")
public String index(){
return "index";
}
}
4.添加视图
在Web-INF/VIEW目录下创建index.jsp
<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<html>
<head>
<title>Title</title>
</head>
<body>
<h1>超级超级喜欢LMRUI,要是失败报错,揍你</h1>
</body>
</html>
5.SpringMVC配置类添加
//配置类
@Configuration
//添加springmvc的功能
@EnableWebMvc
//扫描包范围
@ComponentScan("com.lcy.springboot")
public class MvcConfig {
// 配置JSP视图解析器
@Bean
public InternalResourceViewResolver viewResolver(){
// 获取视图解析器
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
// 设置前缀
viewResolver.setPrefix("/WEB-INF/view/");
// 设置后缀
viewResolver.setSuffix(".jsp");
// 返回视图解析器
return viewResolver;
}
}
6.入口文件添加
public class WebInitializer implements WebApplicationInitializer {
@Override
public void onStartup(ServletContext servletContext) throws ServletException {
// 得到spring上下文环境
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
// 注册MVCd的配置类
context.register(MvcConfig.class);
// 设置ServletContext的上下文环境
context.setServletContext(servletContext);
// 配置转发器
ServletRegistration.Dynamic servletRegistration = servletContext.addServlet("dispatcher",new DispatcherServlet(context));
// 设置映射路径
servletRegistration.addMapping("/");
// 启动实例化 Bean
servletRegistration.setLoadOnStartup(1);
}
}
7.部署与测试
(1)Add New Configuration(idea自带的插件)
tomcat 配置环境变量
IDEA 添加 Tomcat
成功!
(2)WebMVCConfigurationSupport父类方法重写
实现Handle放行,定义拦截器,在配置类重写
//配置类
@Configuration
//添加springmvc的功能
@EnableWebMvc
//扫描包范围
@ComponentScan("com.lcy.springboot")
public class MvcConfig extends WebMvcConfigurationSupport {
// 设置拦截器
@Override
protected void addInterceptors(InterceptorRegistry registry){
super.addInterceptors(registry);
}
// 静态资源Handle不做处理,直接响应到客户端
@Override
protected void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer){
super.configureDefaultServletHandling(configurer);
}
// 配置JSP视图解析器
@Bean
public InternalResourceViewResolver viewResolver(){
// 获取视图解析器
InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
// 设置前缀
viewResolver.setPrefix("/WEB-INF/view/");
// 设置后缀
viewResolver.setSuffix(".jsp");
// 返回视图解析器
return viewResolver;
}
}