XxxAutoConfiguration:帮我们给容器中自动配置组件
XxxProperties:配置类,封装配置文件中的内容
@ConfigurationProperties(
prefix = "spring.resources",
ignoreUnknownFields = false
)
/*
* ResourceHandlerRegistry存储用于通过Spring MVC服务静态资源的资源处理程序的注册
* 允许设置为在Web浏览器中高效加载而优化的缓存头
* 可以在Web应用的目录下,类路径等位置之外的位置提供资源
*/
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(getResourceLocations(this.resourceProperties.getStaticLocations())).setCachePeriod(this.getSeconds(cachePeriod)).setCacheControl(cacheControl));
}
}
}
/**:访问当前项目的任何资源(静态资源的文件夹)
classpath:/META-INF/resources/
classpath:/resources/
classpath:/static/
classpath:/public/
/ # 当前项目的根路径
@Bean
public WelcomePageHandlerMapping welcomePageHandlerMapping(ApplicationContext applicationContext) {
return new WelcomePageHandlerMapping(new TemplateAvailabilityProviders(applicationContext), applicationContext, this.getWelcomePage(), this.mvcProperties.getStaticPathPattern());
}
配置欢迎页的映射:
@Configuration
@ConditionalOnProperty(
value = {"spring.mvc.favicon.enabled"},
matchIfMissing = true
)
/*
* ResourceLoaderAware是一个标记接口
* 用于通过ApplicationContext上下文注入ResourceLoader
* 有setResourceLoader()方法
*/
public static class FaviconConfiguration implements ResourceLoaderAware {
private final ResourceProperties resourceProperties;
/*
* ResourceLoader用于返回Resource对象和ClassLoader对象
* - getResource(String location)方法根据提供的location参数返回相应的Resource对象
* - getClassLoader()方法则返回加载这些Resource的ClassLoader
*/
private ResourceLoader resourceLoader;
public FaviconConfiguration(ResourceProperties resourceProperties) {
this.resourceProperties = resourceProperties;
}
public void setResourceLoader(ResourceLoader resourceLoader) {
this.resourceLoader = resourceLoader;
}
/*
* SimpleUrlHandlerMapping是SpringMVC中适应性最强的Handler Mapping类
* 允许明确指定URL模式和Handler的映射关系.有两种声明方式:
* - prop:
* - key: URL模式
* — value: Handler的ID或者名字
* - value:
* - 等号左边是URL模式
* - 等号右边是HandlerID或者名字
*/
@Bean
public SimpleUrlHandlerMapping faviconHandlerMapping() {
SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
mapping.setOrder(-2147483647);
mapping.setUrlMap(Collections.singletonMap("**/favicon.ico", this.faviconRequestHandler()));
return mapping;
}
配置喜欢的图标(标签的图标):
优点 | 缺点 | |
---|---|---|
jsp | 1. 功能强大,可以写Java代码2. 支持jsp标签 - jsp tag3. 支持表达式语言 - EL表达式4. 官方标准,使用广泛,丰富的第三方jsp标签库5. 性能良好 ,jsp编译成class文件执行,有很好的性能表现 | 1. jsp没有明显的缺点2. 由于可以编写Java代码,使用不当容易破坏MVC结构 |
velocity | 1. 不编写Java代码,实现严格的MVC分离2. 性能良好,比jsp优越3. 使用表达式语言 - EL表达式 | 1. 不是官方标准2. 使用范围小,第三方标签库较少3. 对jsp标签的支持不够友好 |
freemarker | 1. 不编写Java代码,实现严格的MVC分离2. 性能非常好3. 对jsp标签支持良好4. 内置大量常用功能,使用非常方便5. 宏定义(类似jsp标签)非常方便6. 使用表达式语言 - EL表达式 | 1.不是官方标准2. 使用范围小,第三方标签库较少 |
thymeleaf | 1. 静态html嵌入标签属性,浏览器可以直接打开模板文件,便于后端联调2. SpringBoot框架推荐模板 | 1.模板必须符合xml规范2. 需要加入js脚本 |
<properties>
<!-- 切换thymeleaf版本 -->
<thymeleaf.version>3.0.2.RELEASE</thymeleaf.version>
<!-- 布局功能支持程序-thymeleaf3==layout2 thymeleaf2==layout1 -->
<thymeleaf-layout-dialect.version>2.1.1</thymeleaf-layout-dialect.version>
</properties>
<dependency>
<!-- 引入thymeleaf依赖 -->
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
@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";
private Charset encoding;
<html xmlns:th="http://www.thymeleaf.org">
2.使用thymeleaf语法:
thymeleaf | jsp | |
---|---|---|
片段包含 | th:insertth:replace | include |
遍历 | th:each | c:forEach |
条件判断 | th:ifth:unlessth:switchth:case | c:if |
声明变量 | th:objectth:with | c:set |
任意属性修改 | th:attrth:attrprepend(前面)th:attrappend(后面) | |
修改指定属性默认值 | th:valueth:hrefth:src | |
修改标签体文本内容 | th:text(转义)th:utext(不转义) | |
声明片段 | th:fragment | |
移除声明片段 | th:remove |
Simple expressions: (表达式语法)
Variable Expressions: ${...} (获取变量值-OGNL)
1.获取对象的属性,调用方法
2.使用内置的基本对象:
#ctx : the context object.
#vars: the context variables.
#locale : the context locale.
#request : (only in Web Contexts) the HttpServletRequest object.
#response : (only in Web Contexts) the HttpServletResponse object.
#session : (only in Web Contexts) the HttpSession object.
#servletContext : (only in Web Contexts) the ServletContext object.
3.内置的工具对象:
#execInfo : information about the template being processed.
#messages : methods for obtaining externalized messages inside variables expressions, in the same way as they would be obtained using #{…} syntax.
#uris : methods for escaping parts of URLs/URIs
#conversions : methods for executing the configured conversion service (if any).
#dates : methods for java.util.Date objects: formatting, component extraction, etc.
#calendars : analogous to #dates , but for java.util.Calendar objects.
#numbers : methods for formatting numeric objects.
#strings : methods for String objects: contains, startsWith, prepending/appending, etc.
#objects : methods for objects in general.
#bools : methods for boolean evaluation.
#arrays : methods for arrays.
#lists : methods for lists.
#sets : methods for sets.
#maps : methods for maps.
#aggregates : methods for creating aggregates on arrays or collections.
#ids : methods for dealing with id attributes that might be repeated (for example, as a result of an iteration).
Selection Variable Expressions: *{...} (选择表达式,和${}在用法上大体一致)
补充:配合th:object="${session.user}"
<div th:object="${session.user}">
<p>Name: <span th:text="*{firstName}">Sebastian</span>.</p>
<p>Surname: <span th:text="*{lastName}">Pepper</span>.</p>
<p>Nationality: <span th:text="*{nationality}">Saturn</span>.</p>
</div>
Which is exactly equivalent to:
<div>
<p>Name: <span th:text="${session.user.firstName}">Sebastian</span>.</p>
<p>Surname: <span th:text="${session.user.lastName}">Pepper</span>.</p>
<p>Nationality: <span th:text="${session.user.nationality}">Saturn</span>.</p>
</div>
Message Expressions: #{...} (获取国际化内容)
Link URL Expressions: @{...} (定义url)
<a href="details.html"
th:href="@{http://localhost:8080/gtvg/order/details(orderId=${o.id})}">view</a>
<!-- Will produce '/gtvg/order/details?orderId=3' (plus rewriting) -->
<a href="details.html" th:href="@{/order/details(orderId=${o.id})}">view</a>
<!-- Will produce '/gtvg/order/3/details' (plus rewriting) -->
<a href="details.html" th:href="@{/order/{orderId}/details(orderId=${o.id})}">view</a>
Fragment Expressions: ~{...} (片段引用表达式)
Literals(字面量)
Text literals: 'one text' , 'Another one!' ,…
Number literals: 0 , 34 , 3.0 , 12.3 ,…
Boolean literals: true , false
Null literal: null
Literal tokens: one , sometext , main ,…
Text operations (文本操作)
String concatenation: +
Literal substitutions: |The name is ${name}|
Arithmetic operations (数学运算)
Binary operators: + , - , * , / , %
Minus sign (unary operator): -
Boolean operations (布尔运算)
Binary operators: and , or
Boolean negation (unary operator): ! , not
Comparisons and equality (比较运算)
Comparators: > , < , >= , <= ( gt , lt , ge , le )
Equality operators: == , != ( eq , ne )
Conditional operators(条件运算)
If-then: (if) ? (then)
If-then-else: (if) ? (then) : (else)
Default: (value) ?: (defaultvalue)
Special tokens(特殊操作)
No-Operation: _
@Import({WebMvcAutoConfiguration.EnableWebMvcConfiguration.class})
EnableWebMvcConfiguration:
@Configuration
public static class EnableWebMvcConfiguration extends DelegatingWebMvcConfiguration {
DelegatingWebMvcConfiguration:
//
@Autowired(
required = false
)
public void setConfigurers(List<WebMvcConfigurer> configurers) {
if (!CollectionUtils.isEmpty(configurers)) {
this.configurers.addWebMvcConfigurers(configurers);
}
}
参考实现:
//将所有的WebMvcConfigurer相关的配置都调用一遍
public void addViewControllers(ViewControllerRegistry registry) {
Iterator var2 = this.delegates.iterator();
while(var2.hasNext()) {
WebMvcConfigurer delegate = (WebMvcConfigurer)var2.next();
delegate.addViewControllers(registry);
}
}
@Import({DelegatingWebMvcConfiguration.class})
public @interface EnableWebMvc {
}
DelegatingWebMvcConfiguration:
@Configuration
public class DelegatingWebMvcConfiguration extends WebMvcConfigurationSupport {
WebMvcAutoConfiguration:
@Configuration
@ConditionalOnWebApplication(
type = Type.SERVLET
)
@ConditionalOnClass({Servlet.class, DispatcherServlet.class, WebMvcConfigurer.class})
@ConditionalOnMissingBean({WebMvcConfigurationSupport.class}) //当容器中没有此组件时,此自动配置类才生效
@AutoConfigureOrder(-2147483638)
@AutoConfigureAfter({DispatcherServletAutoConfiguration.class, TaskExecutionAutoConfiguration.class, ValidationAutoConfiguration.class})