springboot中有个ResourceProperties的类,里面就定义了静态资源的默认查找路径:
默认的静态资源路径为:
classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/
于是我就将jsp文件放在static目录下
因为springboot本身不支持使用jsp,这里需要添加下面的依赖
<!--jsp的编辑引擎-->
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>jstl</artifactId>
</dependency>
在application.properties中配置视图的前缀和后缀
spring.mvc.view.prefix=classpath:/static/
spring.mvc.view.suffix=.jsp
编写controller
@Controller
public class HelloController {
@RequestMapping("/hello")
public String hello(Model model){
System.out.println("执行了");
return "hello";
}
}
最后启动SpringBootApplication
浏览器访问http://localhost:8080/hello
后来搜了好久才看到有人说springboot只能在webapp下访问jsp页面
修改视图解析器的前缀和后缀
spring.mvc.view.prefix=/WEB-INF/
spring.mvc.view.suffix=.jsp
最后成功访问到jsp页面