當前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
SpringBoot笔记整理(三)
生活随笔
收集整理的這篇文章主要介紹了
SpringBoot笔记整理(三)
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
SpringBoot筆記整理(一)
SpringBoot筆記整理(二)
SpringBoot筆記整理(三)
SpringBoot筆記整理(四)
Web開發
1、使用SpringBoot:
1)創建SpringBoot應用,選中需要的模塊
2)SpringBoot已經默認將這些場景配置好了,只需要在配置文件中指定少量配置就可以運行起來
3)自己編寫業務代碼
自動配置原理
xxxAutoConfiguration:幫我們給容器中自動配置組件 xxxProperties:配置類來封裝配置文件的內容2、SpringBoot對靜態資源的映射規則
@ConfigurationProperties(prefix = "spring.resources",ignoreUnknownFields = false ) public class ResourceProperties {//可以設置和靜態資源有關的參數,緩存時間等 public void addResourceHandlers(ResourceHandlerRegistry registry) {if (!this.resourceProperties.isAddMappings()) {logger.debug("Default resource handling disabled");} else {Duration cachePeriod = this.resourceProperties.getCache().getPeriod();CacheControl cacheControl = this.resourceProperties.getCache().getCachecontrol().toHttpCacheControl();if (!registry.hasMappingForPattern("/webjars/**")) {this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{"/webjars/**"}).addResourceLocations(new String[]{"classpath:/META-INF/resources/webjars/"}).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));}String staticPathPattern = this.mvcProperties.getStaticPathPattern();if (!registry.hasMappingForPattern(staticPathPattern)) {this.customizeResourceHandlerRegistration(registry.addResourceHandler(new String[]{staticPathPattern}).addResourceLocations(WebMvcAutoConfiguration.getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));}} }//配置歡迎頁映射 @Bean public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext, FormattingConversionService mvcConversionService, ResourceUrlProvider mvcResourceUrlProvider) {WelcomePageHandlerMapping welcomePageHandlerMapping = new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());welcomePageHandlerMapping.setInterceptors(this.getInterceptors(mvcConversionService, mvcResourceUrlProvider));return welcomePageHandlerMapping; }1)所有/webjars/**,都去classpath:/META-INF/resources//webjars/找資源
webjars:以jar包的方式引入靜態資源;
https://www.webjars.org/
2)“/**”訪問當前項目的任何資源(靜態資源的文件夾)
"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/" "/":當前項目的根路徑3)歡迎頁:靜態資源文件夾下的所有index.html頁面,被"/"映射
4)所有的/favicon.ico 都是在靜態資源文件下找
3、模板引擎
SpringBoot推薦的Thymeleaf:語法更簡單,功能更強大
3.1 引入Thymeleaf
3.2 Thymeleaf使用&語法
@ConfigurationProperties(prefix = "spring.thymeleaf" ) public class ThymeleafProperties {private static final Charset DEFAULT_ENCODING;public static final String DEFAULT_PREFIX = "classpath:/templates/";public static final String DEFAULT_SUFFIX = ".html";private boolean checkTemplate = true;private boolean checkTemplateLocation = true;private String prefix = "classpath:/templates/";private String suffix = ".html";private String mode = "HTML";//只要把html頁面放在classpath:/templates/,thymeleaf就能自動渲染使用:
3.2.1 導入thymeleaf的名稱空間
3.2.2 使用thymeleaf語法
<!DOCTYPE html> <html lang="en" xmlns:th="http://www/thymeleaf.org"> <head><meta charset="UTF-8"><title>Title</title> </head> <body><h1>success</h1><!--th:text 將div里面的文本內容設置為--><div th:text="${hello}">這里顯示歡迎信息</div> </body> </html>1)th:text:改變當前元素里面的文本內容
th:任意html屬性,來替換原生屬性的值
2)表達式語法
總結
以上是生活随笔為你收集整理的SpringBoot笔记整理(三)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 数据库高级知识——mysql架构介绍(二
- 下一篇: SpringBoot笔记整理(一)