SpringBoot+Thymeleaf配置外部静态资源访问


title: SpringBoot+Thymeleaf配置外部静态资源访问
date: 2019-06-23 10:40:29
categories:

  • Spring Boot
    tags:
  • thymeleaf

#####配置外部资源访问(html、css、js)

我的thymeleaf配置:

# thymeleaf
# html目录配置
spring.thymeleaf.prefix=file:d:/tt/templates/
spring.thymeleaf.suffix=.html
spring.thymeleaf.cache=false
spring.thymeleaf.servlet.content-type=text/html
spring.thymeleaf.enabled=true
spring.thymeleaf.encoding=UTF-8
# 一代填 spring.thymeleaf.mode=HTML5
spring.thymeleaf.mode=HTML

css与js静态资源访问通过实现WebMvcConfigurer来配置:

如下配置文件的方式无法访问到:

spring.mvc.static-path-pattern=/**
#spring.resources.static-locations= classpath:/META-INF/resources/,classpath:/resources/,classpath:/static/,classpath:/public/,file:d:/tt/static/

MVCConfig:

@Configuration //申明这是一个配置
@EnableWebMvc
public class MySrpingMVCConfig implements WebMvcConfigurer {
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("file:d:/tt/static/");
    }
}

html文件:

// 引入头
<html xmlns:th="http://www.thymeleaf.org">
<head>
  <meta charset="utf-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <title>AdminLTE 2 | Dashboardtitle>
  <meta content="width=device-width, initial-scale=1, maximum-scale=1, user-scalable=no" name="viewport">
  
// bower_components/bootstrap/dist/css/bootstrap.min.css位于static目录下
  <link rel="stylesheet" href="../static/bower_components/bootstrap/dist/css/bootstrap.min.css" th:href="@{bower_components/bootstrap/dist/css/bootstrap.min.css}">
    head>
    <body>
        
    body>
html>

这样所有的前端页面就在项目之外了。

你可能感兴趣的:(Spring)