1、添加jar包
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<version>1.2</version>
</dependency>
jsp不能用Application.java的main方法启动,只能用Tomcat启动,并且要打war包,所以修改打包方式为war:<packaging>war</packaging>
2、在src/main/下创建webapp文件夹,在webapp文件夹下创建WEB-INF文件夹,如图:
3、在DemoApplication里继承SpringBootServletInitializer,并添加@ServletComponentScan
package com.demo;
import org.mybatis.spring.annotation.MapperScan;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.web.servlet.ServletComponentScan;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@SpringBootApplication
@RestController
@ServletComponentScan
public class DemoApplication extends SpringBootServletInitializer{
@RequestMapping("/")
public String home(){
return "hello world";
}
public static void main(String[] args) {
SpringApplication.run(DemoApplication.class, args);
}
}
以上就完成了。在controller里添加跳转测试
@RequestMapping("/turnJsp")
public String turnJsp(){
return "login/login";
}
访问结果如图: