1.在https://start.spring.io/中生成项目,添加web依赖
2.在application.properties中添加
spring.mvc.view.prefix= /WEB-INF/jsp/ spring.mvc.view.suffix=.jsp
3.在XXApplication.kt中
@SpringBootApplication class WebappApplication : SpringBootServletInitializer() { override fun configure(builder: SpringApplicationBuilder?): SpringApplicationBuilder? { return builder?.sources(WebappApplication::class.java) } }
4.XXController:(注意需要在XXApplication.kt所在包的子包下,要不可能扫描不到controller)
@Controller class LoginController { @RequestMapping("/login") fun login() :String{ return "index" } }
5.在main下新建一webapp/WEB-INF/jsp,里面放.jsp页面
*在intellij idea中,在project structure/Modules/Web模块下添加web resource directory路径,可以新建jsp file
6.gradle需要添加:
//jsp compile group: 'javax.servlet',name:"jstl",version:'1.2' runtime group: 'org.apache.tomcat', name: 'tomcat-jasper', version: '9.0.2'
*需要runtime编译,否则jar包会冲突报错
参考官方项目:https://github.com/spring-projects/spring-boot/tree/master/spring-boot-samples/spring-boot-sample-web-jsp
参考:1.http://blog.csdn.net/cszhang570221322/article/details/78129565