0


springboot返回html页面

方式一:
1、html文件放在resource下的static目录下
2、配置试图解析器#配置视图解析器
spring:
mvc:
view:
prefix: /
suffix: .html

方式二:
1、html文件放在resource下的templates目录下
2、配置文件
spring:
thymeleaf:
prefix:
classpath: /templates # 访问template下的html文件需要配置模板,映射
cache: false # 开发时关闭缓存,不然没法看到实时页面
3、添加依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

Controller用@Controller注解代替@RestController
@RestController注解,相当于@Controller+@ResponseBody两个注解的结合,返回json数据不需要在方法前面加@ResponseBody注解了,但使用@RestController这个注解,就不能返回jsp,html页面,视图解析器无法解析jsp,html页面

@GetMapping("/")
public String index(){
return "index";
}


本文转载自: https://blog.csdn.net/m0_68704114/article/details/131256644
版权归原作者 念兹在兹o 所有, 如有侵权,请联系我们删除。

“springboot返回html页面”的评论:

还没有评论