當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot怎么直接访问templates下的html页面
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot怎么直接访问templates下的html页面
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
SpringBoot直接訪問templates下的html問題
方法1:曾經:
template下文件不允許直接訪問
1、springboot項目默認是不允許直接訪問template下的文件的,是受保護的。
所以想訪問template下的html頁面,我們可以配置視圖解析器。
2、如果想要用視圖去展示,應該要設置好視圖展示頁面,比如說用一個模板語言來接收返回的數據(thymeleaf或者freemarker等), 也可以用jsp接收,但是SpringBoot官方是不推薦用jsp的,而是建議使用thymeleaf作為模板語言,這里我以thymeleaf為例。
方法2:現在(推薦的方法)
知道需要通過控制器跳轉才能訪問到templates下的html文件
因為springboot默認訪問static,resources,public這些文件夾下的文件,而沒有默認訪問templates下的
我們需要在配置加上
spring.resources.static-locations=classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/templates/
完整例子:
spring:datasource:name: druidtype: com.alibaba.druid.pool.DruidDataSourcedruid:driver-class-name: com.mysql.jdbc.Driverfilters: statpassword: 123456url: jdbc:mysql://127.0.0.1:3306/ugaoxin_db?useUnicode=true&characterEncoding=UTF-8username: rootthymeleaf:prefix: /templates/**suffix: .htmlcache: falseresources:static-locations: classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,classpath:/templates/server:port: 8082總結
以上是生活随笔為你收集整理的SpringBoot怎么直接访问templates下的html页面的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Scikit-Learn 机器学习笔记
- 下一篇: TensorFlow 笔记5--模型复用