web的静态资源有 js、css、图片等
Spring Boot默认提供静态资源目录位置:
定义在org.springframework.boot.autoconfigure.web.ResourceProperties类中:
@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties implements ResourceLoaderAware {
private static final String[] SERVLET_RESOURCE_LOCATIONS = { "/" };
private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
"classpath:/META-INF/resources/", "classpath:/resources/",
"classpath:/static/", "classpath:/public/" };
private static final String[] RESOURCE_LOCATIONS;
static {
RESOURCE_LOCATIONS = new String[CLASSPATH_RESOURCE_LOCATIONS.length
+ SERVLET_RESOURCE_LOCATIONS.length];
System.arraycopy(SERVLET_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS, 0,
SERVLET_RESOURCE_LOCATIONS.length);
System.arraycopy(CLASSPATH_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS,
SERVLET_RESOURCE_LOCATIONS.length, CLASSPATH_RESOURCE_LOCATIONS.length);
}
/** * Locations of static resources. Defaults to classpath:[/META-INF/resources/, * /resources/, /static/, /public/] plus context:/ (the root of the servlet context). */
private String[] staticLocations = RESOURCE_LOCATIONS;
Thymeleaf是Spring Boot提供的默认配置的模板引擎之一。模板默认的模板配置路径为:src/main/resources/templates。
这个路径配置在org.springframework.boot.autoconfigure.thymeleaf.ThymeleafProperties 类中:
@ConfigurationProperties(prefix = "spring.thymeleaf")
public class ThymeleafProperties {
private static final Charset DEFAULT_ENCODING = Charset.forName("UTF-8");
private static final MimeType DEFAULT_CONTENT_TYPE = MimeType.valueOf("text/html");
public static final String DEFAULT_PREFIX = "classpath:/templates/";
public static final String DEFAULT_SUFFIX = ".html";
/** * Prefix that gets prepended to view names when building a URL. */
private String prefix = DEFAULT_PREFIX;
<!-- 加入thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org">
<head lang="en">
<meta charset="UTF-8" />
<title></title>
</head>
<body>
<!-- 通过thymeleaf引入图片路径 -->
<img alt="test" th:src="@{images/leaf.jpg}" />
<h1 th:text="${host}">Hello World</h1>
</body>
@RequestMapping("/simple")
public String simpleIndex(ModelMap map) {
// 加入一个属性,用来在模板中读取
map.addAttribute("host", "http://blog.csdn.net/hry2015/article/");
// return模板文件的名称,对应src/main/resources/templates/index.html
return "simple/index";
}
# Enable template caching. 开发时,设置此值为false
spring.thymeleaf.cache=false
# Check that the templates location exists.
spring.thymeleaf.check-template-location=true
# Content-Type value.
spring.thymeleaf.content-type=text/html
# Enable MVC Thymeleaf view resolution.
spring.thymeleaf.enabled=true
# Template encoding.
spring.thymeleaf.encoding=UTF-8
# Comma-separated list of view names that should be excluded from resolution.
spring.thymeleaf.excluded-view-names=
# Template mode to be applied to templates. See also StandardTemplateModeHandlers.
spring.thymeleaf.mode=HTML5
# Prefix that gets prepended to view names when building a URL. 添加前缀到视图URL的前缀
spring.thymeleaf.prefix=classpath:/templates/
# Suffix that gets appended to view names when building a URL. 添加后缀到视图URL的后缀
spring.thymeleaf.suffix=.html
# Order of the template resolver in the chain.
# spring.thymeleaf.template-resolver-order=
# Comma-separated list of view names that can be resolved.
# spring.thymeleaf.view-names=
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://blog.csdn.net/hry2015/article/details/71374591
内容来源于网络,如有侵权,请联系作者删除!