1、项目中新建目录webapp/WEB-INF/jsp,并配置依赖
2、application.yml中添加mvc配置
spring:
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp
static-path-pattern: /** (js、css等文件放到static目录下即可)
springboot会扫描默认静态资源路径:classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/。
3、页面引用:
4、springboot打war包
(1)首部的jar改为war
<groupId>com.star</groupId>
<artifactId>yiyong</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<!--<packaging>jar</packaging>-->
(2)pom.xml中添加依赖
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
(3)修改启动类
@SpringBootApplication
public class YiyongApplication extends SpringBootServletInitializer{
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
return builder.sources(YiyongApplication.class);
}
public static void main(String[] args) {
SpringApplication.run(YiyongApplication.class, args);
}
}
关键之处:继承SpringBootServletInitializer,重写了configure方法。
(4)打war包:Maven Prohects -> clean -> package,包在target目录下。
(5)war包放在webapp目录下,启动tomcat访问即可,亲测有效!