本文共 1486 字,大约阅读时间需要 4 分钟。
1、添加jar包
jsp不能用Application.java的main方法启动,只能用Tomcat启动,并且要打war包,所以修改打包方式为war:<packaging>war</packaging>org.springframework.boot spring-boot-starter-tomcat provided org.apache.tomcat.embed tomcat-embed-jasper provided javax.servlet jstl 1.2
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@ServletComponentScanpublic 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"; }
访问结果如图: