Eclipse Springboot项目 controller跳转访问JSP
1、在main文件夹下创建/webapp/WEB_INF/JSP/oldIndex.jsp
2、设置webapp为Source Folder
- 右击项目 > build path > Configure build path
- 点击add folder
3、在pom.xml加入jsp依赖
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.apache.tomcat</groupId>
<artifactId>tomcat-jsp-api</artifactId>
</dependency>
4、在application.yml中加入mvc配置
(删除application.properties新建appliction.yml,推荐使用appliction.yml)
spring.mvc.view.prefix: /WEB_INF/JSP/
spring.mvc.view.suffix: .jsp
5、新建的controller、service、model、dao应该在springboot项目启动类的包内,否则可能出现运行错误
6、创建TestController类
package com.example.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
@Controller
public class TestController {
@RequestMapping("/TEST")
public String test() {
return "oldIndex";
}
}
7、运行启动类,浏览器输入
http://localhost:8080/TEST
如果出现下面错误
Error resolving template [oldIndex], template might not exist or might not be accessible by any of the configured Template Resolvers
注释掉
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency> -->
重新运行项目