Spring Boot入门+深入(四)

一、Spring Boot与Web开发

1.使用Spring Boot

1.和之前一样创建Spring Boot项目,选择Web模块,XX模块等

2.可自己指定配置

3.自己编写业务代码

2.Spring Boot对静态资源的映射

WebMvcAutoConfiguration.java中的addResourceHandlers方法添加资源映射

		@Override
		public 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));
			}
		}

①Integer cachePeriod = this.resourceProperties.getCachePeriod();相关的配置文件

//可以设置和静态资源有关的参数,缓存时间等

@ConfigurationProperties(prefix = "spring.resources", ignoreUnknownFields = false)
public class ResourceProperties implements ResourceLoaderAware {

②String staticPathPattern = this.mvcProperties.getStaticPathPattern();

private String staticPathPattern = "/**";
this.resourceProperties.getStaticLocations()
	private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
			"classpath:/META-INF/resources/", "classpath:/resources/",
			"classpath:/static/", "classpath:/public/" };

③WebMvcAutoConfiguration.java中的welcomePageHandlerMapping方法添加首页映射

		@Bean
		public WelcomePageHandlerMapping welcomePageHandlerMapping(
				ResourceProperties resourceProperties) {
			return new WelcomePageHandlerMapping(resourceProperties.getWelcomePage(),
					this.mvcProperties.getStaticPathPattern());
		}
resourceProperties.getWelcomePage()
	public Resource getWelcomePage() {
		for (String location : getStaticWelcomePageLocations()) {
			Resource resource = this.resourceLoader.getResource(location);
			try {
				if (resource.exists()) {
					resource.getURL();
					return resource;
				}
			}
			catch (Exception ex) {
				// Ignore
			}
		}
		return null;
	}
getStaticWelcomePageLocations() 静态资源路径下的路径拼接上index.html
	private String[] getStaticWelcomePageLocations() {
		String[] result = new String[this.staticLocations.length];
		for (int i = 0; i < result.length; i++) {
			String location = this.staticLocations[i];
			if (!location.endsWith("/")) {
				location = location + "/";
			}
			result[i] = location + "index.html";
		}
		return result;
	}

④WebMvcAutoConfiguration.java中的FaviconConfiguration配置喜欢的图标

		@Configuration
		@ConditionalOnProperty(value = "spring.mvc.favicon.enabled", matchIfMissing = true)
		public static class FaviconConfiguration {

			private final ResourceProperties resourceProperties;

			public FaviconConfiguration(ResourceProperties resourceProperties) {
				this.resourceProperties = resourceProperties;
			}

			@Bean
			public SimpleUrlHandlerMapping faviconHandlerMapping() {
				SimpleUrlHandlerMapping mapping = new SimpleUrlHandlerMapping();
				mapping.setOrder(Ordered.HIGHEST_PRECEDENCE + 1);
                //所有 **/favicon.ico
				mapping.setUrlMap(Collections.singletonMap("**/favicon.ico",
						faviconRequestHandler()));
				return mapping;
			}

			@Bean
			public ResourceHttpRequestHandler faviconRequestHandler() {
				ResourceHttpRequestHandler requestHandler = new ResourceHttpRequestHandler();
				requestHandler
						.setLocations(this.resourceProperties.getFaviconLocations());
				return requestHandler;
			}

		}
this.resourceProperties.getFaviconLocations()也是在静态资源路径上找
	List<Resource> getFaviconLocations() {
		List<Resource> locations = new ArrayList<Resource>(
				this.staticLocations.length + 1);
		if (this.resourceLoader != null) {
			for (String location : this.staticLocations) {
				locations.add(this.resourceLoader.getResource(location));
			}
		}
		locations.add(new ClassPathResource("/"));
		return Collections.unmodifiableList(locations);
	}

1)、所有 /webjars/** ,都去 classpath:/META-INF/resources/webjars/ 找资源;

webjars:以jar包的方式引入静态资源;

webjars官网:https://www.webjars.org/

webjars官网中有不同的静态资源jar包,可以用Maven的方式引入

Spring Boot入门+深入(四)

举例: 启动项目可以通过路径访问静态资源。在访问的时候只需要写webjars下面资源的名称即可

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

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

Spring Boot入门+深入(四)

 2)、"/**" 访问当前项目的任何资源,都去(静态资源的文件夹,如下)找映射

"classpath:/META‐INF/resources/",
"classpath:/resources/",
"classpath:/static/",
"classpath:/public/"
"/":当前项目的根路径

localhost:8080/stestAdd === 去静态资源文件夹里面找stestAdd

3)、欢迎页; 静态资源文件夹下的所有index.html页面;被"/**"映射;

localhost:8080/    默认找index页面

4)、所有的 **/favicon.ico 都是在静态资源文件下找;

标题栏左侧的图标

Spring Boot入门+深入(四)

 二、模板引擎

上一篇:SpringBoot静态资源源码解析


下一篇:rabbitmq -java