首先在application.yml文件或application.properties里添加配置
spring:
mvc:
view:
prefix: /WEB-INF/jsp/
suffix: .jsp
- 1
- 2
- 3
- 4
- 5
接着在pom.xml添加依赖包
<dependency>
<groupId>org.apache.tomcat.embed</groupId>
<artifactId>tomcat-embed-jasper</artifactId>
<scope>provided</scope>
</dependency>
- 1
- 2
- 3
- 4
- 5
最后在控制层添加一小段代码(记得给控制层添加@Controller)
//打开所有jsp页面
@RequestMapping("/{view}")
public String html(@PathVariable("view")String view){
return view;
}//其中view就是你要打开的视图哦
- 1
- 2
- 3
- 4
- 5
首先步骤jsp差不多,一样在application.yml文件添加配置
注意别漏了classpath:,会报错的
spring:
thymeleaf:
prefix: classpath:/templates/
suffix: .html
mode: LEGACYHTML5
cache: false
- 1
- 2
- 3
- 4
- 5
- 6
接着在pom.xml添加模板
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
- 1
- 2
- 3
- 4
最后一步跟都一样
//打开所有html页面
@RequestMapping("/{view}")
public String html(@PathVariable("view")String view){
return view;
}//其中view就是你要打开的视图哦
- 1
- 2
- 3
- 4
- 5
然后启动你的项目会默认打开index.html页面哦!
- 最后这是我的目录结构

注:第一次发表
本文介绍了如何在Spring Boot项目中配置并跳转到JSP和HTML页面。首先,在application.yml中设置视图解析路径,然后在pom.xml添加相应的依赖。对于JSP,引入tomcat-embed-jasper依赖;对于HTML,使用Thymeleaf并添加其starter。在控制层定义通用方法,通过@PathVariable接收视图名称并返回。启动项目后,默认打开index.html。
2165

被折叠的 条评论
为什么被折叠?



