Springboot访问templates html页面的实现方法

发布时间:2020-07-22 10:22:48 作者:小猪
来源:亿速云 阅读:384

这篇文章主要讲解了Springboot访问templates html页面的实现方法,内容清晰明了,对此有兴趣的小伙伴可以学习一下,相信大家阅读完之后会有帮助。

springboot项目默认是不允许直接访问templates下的文件的,是受保护的。

如果要访问templates下的文件,推荐使用thymeleaf。

注:使用thymeleaf这一点要牢牢记住!

如何使用:

1、pom依赖

<!--thymeleaf 模板依赖-->
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>

2、配置文件

#模板热部署、禁用 thymeleaf 缓存
spring.thymeleaf.cache=false

3、html文件位于resources的templates/目录下,例如

Springboot访问templates html页面的实现方法

注意html文件名,这里使用goodsShow,在不区分大小写的情况下与后台返回字符串匹配

4、后台返回字符串形式访问html(也可以使用ModelAndView,这里不做展示)

@Controller
@RequestMapping("/goods")
public class GoodsController {
  private static final Logger log = LoggerFactory.getLogger(GoodsController.class);

  @GetMapping
  public String goodsShow() {
    return "goodsShow";
  }
}

5、浏览器访问输入:ip:端口号/上下文根/goods

本地访问:localhost:端口号/上下文根/goods

例如:localhost:8080/goods

看完上述内容,是不是对Springboot访问templates html页面的实现方法有进一步的了解,如果还想学习更多内容,欢迎关注亿速云行业资讯频道。

推荐阅读:
  1. Springboot怎么读取templates文件html
  2. springboot使用thymeleaf模板访问html页面的案例

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

spring boot templates

上一篇:angularjs外部文件中的控制器使用

下一篇:使用linux的chroot命令改变根目录

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》