javascript
SpringBoot笔记整理(三)
SpringBoot筆記整理(一)
SpringBoot筆記整理(二)
SpringBoot筆記整理(三)
SpringBoot筆記整理(四)
Web開(kāi)發(fā)
1、使用SpringBoot:
1)創(chuàng)建SpringBoot應(yīng)用,選中需要的模塊
2)SpringBoot已經(jīng)默認(rèn)將這些場(chǎng)景配置好了,只需要在配置文件中指定少量配置就可以運(yùn)行起來(lái)
3)自己編寫(xiě)業(yè)務(wù)代碼
自動(dòng)配置原理
xxxAutoConfiguration:幫我們給容器中自動(dòng)配置組件 xxxProperties:配置類(lèi)來(lái)封裝配置文件的內(nèi)容2、SpringBoot對(duì)靜態(tài)資源的映射規(guī)則
@ConfigurationProperties(prefix = "spring.resources",ignoreUnknownFields = false ) public class ResourceProperties {//可以設(shè)置和靜態(tài)資源有關(guān)的參數(shù),緩存時(shí)間等 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));}} }//配置歡迎頁(yè)映射 @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包的方式引入靜態(tài)資源;
https://www.webjars.org/
2)“/**”訪問(wèn)當(dāng)前項(xiàng)目的任何資源(靜態(tài)資源的文件夾)
"classpath:/META-INF/resources/", "classpath:/resources/", "classpath:/static/", "classpath:/public/" "/":當(dāng)前項(xiàng)目的根路徑3)歡迎頁(yè):靜態(tài)資源文件夾下的所有index.html頁(yè)面,被"/"映射
4)所有的/favicon.ico 都是在靜態(tài)資源文件下找
3、模板引擎
SpringBoot推薦的Thymeleaf:語(yǔ)法更簡(jiǎn)單,功能更強(qiáng)大
3.1 引入Thymeleaf
3.2 Thymeleaf使用&語(yǔ)法
@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頁(yè)面放在classpath:/templates/,thymeleaf就能自動(dòng)渲染使用:
3.2.1 導(dǎo)入thymeleaf的名稱(chēng)空間
3.2.2 使用thymeleaf語(yǔ)法
<!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里面的文本內(nèi)容設(shè)置為--><div th:text="${hello}">這里顯示歡迎信息</div> </body> </html>1)th:text:改變當(dāng)前元素里面的文本內(nèi)容
th:任意html屬性,來(lái)替換原生屬性的值
2)表達(dá)式語(yǔ)法
總結(jié)
以上是生活随笔為你收集整理的SpringBoot笔记整理(三)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 数据库高级知识——mysql架构介绍(二
- 下一篇: gradle idea java ssm