一、使用Spring Boot创建web项目
- 使用Spring Initializr创建项目时,勾选Spring Web场景启动器,同时一并选择web场景下需要的其他场景启动器。
- Spring Boot默认将这些场景配置好了,只需要在配置文件中指定少量的配置就可以运行起来。
- 编写自己的业务代码
xxxxAutoConfiguration:帮我们给容器中自动配置组件 xxxxProperties:配置类,来封装配置文件的内容
二、静态资源映射规则
2.1 webjars的形式引入静态资源
以jar包的方式引入静态资源。官网地址:http://www.webjars.org/
<!--引入jquery-webjar--><!--在访问的时候只需要写webjars下面资源的名称即可--><dependency><groupId>org.webjars</groupId><artifactId>jquery</artifactId><version>3.3.1</version></dependency>
引入webjars之后,我们通过 localhost:8080/webjars/jquery/3.3.1/jquery.js 即可访问到静态资源。 所有的/webjars/**请求,都去 classpath:/META-INF/resources/webjars/ 路径下找资源。

实际开发中,通过 2.1webjars的形式引入静态资源的操作非常少,缺点明显。我们需要哪些静态资源,只需要自己去下载好,然后放在静态资源文件夹即可,对应2.2部分。
2.2 静态资源文件夹
## Spring Boot默认的静态资源文件夹 "/" 表示当前项目的根路径"classpath:/META-INF/resources/","classpath:/resources/","classpath:/static/","classpath:/public/"
访问当前项目的任何资源,如果没有绑定映射路径,着都去静态资源文件夹找映射。 例如:localhost:8080/abc 如果没有绑定/abc请求路径,则去资源文件夹中找abc
public class WebMvcAutoConfiguration {public static final String DEFAULT_PREFIX = "";public static final String DEFAULT_SUFFIX = "";private static final String SERVLET_LOCATION = "/";@Overridepublic void addResourceHandlers(ResourceHandlerRegistry registry) {if (!this.resourceProperties.isAddMappings()) {logger.debug("Default resource handling disabled");return ;}Integer cachePeriod = this.resourceProperties.getCachePeriod();if (!registry.hasMappingForPattern("/webjars/**")) {customizeResourceHandlerRegistration(registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/").setCachePeriod(cachePeriod));}String staticPathPattern = this.mvcProperties.getStaticPathPattern();// 静态资源文件夹映射if (!registry.hasMappingForPattern(staticPathPattern)) {customizeResourceHandlerRegistration(registry.addResourceHandler(staticPathPattern).addResourceLocations(this.resourceProperties.getStaticLocations()).setCachePeriod(cachePeriod));}}// 配置欢迎页映射@Beanpublic WelcomePageHandlerMapping welcomePageHandlerMapping(ResourceProperties resourceProperties) {return new WelcomePageHandlerMapping(resourceProperties.getWelcomePage(),this.mvcProperties.getStaticPathPattern());}// 配置喜欢的图标@Configuration@ConditionalOnProperty(value = "spring.mvc.favicon.enabled", matchIfMissing = true)public static class FaviconConfiguration {private final ResourceProperties resourceProperties;public FaviconConfiguration(ResourceProperties resourceProperties) {this.resourceProperties = resourceProperties;}@Beanpublic SimpleUrlHandlerMapping faviconHandlerMapping() {SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();mapping.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);//所有 **/favicon.icomapping.setUrlMap(Collections.singletonMap("**/favicon.ico",faviconRequestHandler()));return mapping;}@Beanpublic ResourceHttpRequestHandler faviconRequestHandler() {ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();requestHandler.setLocations(this.resourceProperties.getFaviconLocations());return requestHandler;}}
欢迎页
静态资源文件夹下的所有index.html页面,被“/”映射。
图标文件
所有的 */favicon.ico 都在静态资源文件夹下找
