Spring Boot —Web开发


@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties {
//可以这只和静态资源有关的参数,比如缓存时间等
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
if (!this.resourceProperties.isAddMappings()) {
logger.debug("Default resource handling disabled");
return;
}
Duration cachePeriod = this.resourceProperties.getCache().getPeriod();
CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();
if (!registry.hasMappingForPattern("/webjars/**")) {
customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**")
.addResourceLocations("classpath:/META-INF/resources/webjars/")
.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
String staticPathPattern = this.mvcProperties.getStaticPathPattern();
if (!registry.hasMappingForPattern(staticPathPattern)) {
customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern)
.addResourceLocations(getResourceLocations(this.resourceProperties.getStaticLocations()))
.setCachePeriod(getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
}


<!-- 引入jquery的webjar -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>jquery</artifactId>
<version>3.3.1</version>
</dependency>

http://localhost:8080/webjars/jquery/3.3.1/jquery.js









#自定义静态文件夹位置,但会是原有Resources下的静态文件夹失效
spring.resources.static-locations=classpath:/hello,classpath:/guke/
3、模板引擎
JSP、Velocity、Freemarker、Thymeleaf

SpringBoot推荐Thymeleaf;
语法更简单,功能更强大;
引入thymeleaf:
<properties>
<!-- thymeleaf版本控制-->
<thymeleaf.version>3.0.9.RELEASE</thymeleaf.version>
<!-- 布局功能的支持程序 thymeleaf3主程序 layout2版本以上 -->
<!-- thymeleaf2 layout1 -->
<thymeleaf-layout-dialect.version>2.2.2</thymeleaf-layout-dialect.version>
</properties>
<!-- thymeleaf依赖 -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
2、Thymeleaf语法与使用





本文深入探讨SpringBoot中Web开发的配置细节,包括如何设置静态资源路径、自定义缓存控制策略,以及如何集成Thymeleaf作为模板引擎。通过具体示例,读者将学会如何优化Web应用的静态资源管理和页面渲染。
2602

被折叠的 条评论
为什么被折叠?



