`
wushq
  • 浏览: 4412 次
  • 性别: Icon_minigender_1
  • 来自: 上海
社区版块
存档分类
最新评论

springboot - 静态资源路径配置

 
阅读更多

   

    静态资源配置

 

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

    其中,其中addResourceHandler为添加映射路径,addResourceLocations为添加资源路径。staticPathPattern,staticLocations这两个属性分别来自于WebMvcProperties与ResourceProperties。

    WebMvcProperties

	/**
	 * Path pattern used for static resources.
	 */
	private String staticPathPattern = "/**";

     ResourceProperties

	private static final String[] SERVLET_RESOURCE_LOCATIONS = { "/" };

	private static final String[] CLASSPATH_RESOURCE_LOCATIONS = {
			"classpath:/META-INF/resources/", "classpath:/resources/",
			"classpath:/static/", "classpath:/public/" };

	private static final String[] RESOURCE_LOCATIONS;

	static {
		RESOURCE_LOCATIONS = new String[CLASSPATH_RESOURCE_LOCATIONS.length
				+ SERVLET_RESOURCE_LOCATIONS.length];
		System.arraycopy(SERVLET_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS, 0,
				SERVLET_RESOURCE_LOCATIONS.length);
		System.arraycopy(CLASSPATH_RESOURCE_LOCATIONS, 0, RESOURCE_LOCATIONS,
				SERVLET_RESOURCE_LOCATIONS.length, CLASSPATH_RESOURCE_LOCATIONS.length);
	}

	/**
	 * Locations of static resources. Defaults to classpath:[/META-INF/resources/,
	 * /resources/, /static/, /public/] plus context:/ (the root of the servlet context).
	 */
	private String[] staticLocations = RESOURCE_LOCATIONS;

     可以看出来,SpringBoot通过WebMvcAutoConfiguration的内部类WebMvcAutoConfigurationAdapter中addResourceHandlers方法,把类路径下/static、/public、/resources和/META-INF/resources文件夹下的静态资源文件直接映射为/**。

 

 

 

分享到:
评论

相关推荐

    springboot-example02

    - `src/main/resources`:资源文件目录,包括配置文件(`application.properties`或`application.yml`)、静态资源(如HTML、CSS、JS)和模板文件(如Thymeleaf或FreeMarker)。 - `src/test/java`:测试代码目录,...

    springboot-web-material.rar

    9. **静态资源处理**:Spring Boot默认支持对静态资源(如JavaScript、CSS、图片等)的处理,可以通过配置`spring.web.resources.static-locations`自定义静态资源路径。 10. **错误处理**:Spring Boot提供了一套...

    springboot-web-demo

    这个项目主要展示了如何利用 SpringBoot 来创建一个具备基本 Web 功能的应用,包括与 JSP 页面的集成、自定义视图解析器的配置以及静态资源路径的设置。以下是这个项目涉及的关键知识点: 1. **SpringBoot 初始化**...

    springboot-ueditor.zip

    - `application.properties`或`application.yml`中的配置:可能涉及到静态资源路径、上传文件路径、MultipartFile处理等相关配置。 - 静态资源处理:Spring Boot默认支持静态资源处理,但可能需要自定义配置以支持...

    springboot security 静态资源

    因此,理解和配置Spring Security处理静态资源的访问是非常重要的。 首先,Spring Boot在`src/main/resources`目录下存放静态资源,包括`static/`、`public/`或`resources/`等子目录。这些目录下的内容将被自动处理...

    狂神springboot静态资源.zip

    标题“狂神springboot静态资源.zip”表明这是一个关于Spring Boot框架中处理静态资源的教程或示例集合。Spring Boot是Java开发中一个流行的微服务框架,它简化了配置,提供了快速构建可生产级应用的方式。在Spring ...

    springboot-bootstrap 后台管理系统.zip

    2. `src/main/resources`:资源配置,如application.properties或yml,数据库连接配置,以及静态资源目录。 3. `src/main/webapp`(如果使用Maven)或`src/main/resources/static`(如果使用Gradle):存放前端HTML...

    ueditor-springboot-demo

    6. **静态资源处理**:UEditor的JavaScript和CSS文件需要被应用服务器作为静态资源提供,SpringBoot可以通过配置`application.properties`或`application.yml`文件来设置静态资源目录。 7. **安全配置**:使用...

    SpringBoot静态资源路径配置及主页显示

    SpringBoot静态资源路径配置及主页显示 SpringBoot静态资源路径配置是指在SpringBoot应用程序中配置静态资源的路径,以便在应用程序中正确加载静态资源,如HTML、CSS、JavaScript、图片等文件。静态资源路径配置...

    springboot-demo,springboot简单案例

    - `src/main/resources`:存放资源文件,如配置文件(application.properties 或 application.yml)、静态资源(static 目录)和模板文件(templates 目录)。 - `pom.xml`(或 `build.gradle`):项目构建文件,...

    springboot-web.zip

    "springboot-web"项目应该遵循Spring Boot的标准目录结构,包括`src/main/java`(存放源代码)、`src/main/resources`(存放资源配置文件、静态资源和视图解析路径)、以及`pom.xml`或`build.gradle`(构建文件)。...

    管理系统系列--springboot-bootstrap 后台管理系统.zip

    - `src/main/resources`:资源文件,如配置文件、静态资源等。 - `application.properties`或`application.yml`:Spring Boot的配置文件。 - `static`或`public`目录:存放Bootstrap的CSS、JavaScript和图片等静态...

    springmvc转为springboot--干货.docx

    - `application.properties`:配置文件中包含了服务器端口、环境选择、静态资源路径、模板引擎设置、数据源配置、连接池参数等。例如,关闭Thymeleaf缓存和启用,设置数据源连接信息等。 - `spring.profiles....

    CGB-SPRINGBOOT-1.01.docx

    - **Web资源进阶**:包括定义静态资源路径,配置URL映射规则,以及控制层方法的定义和测试。 6. **Spring Boot应用加强** - **mybatis-plus插件**:这是一个MyBatis的增强工具,提供了一些便利的CRUD操作。 - **...

    springboot-jsp-thymeleaf.zip

    作者提供了完整的配置代码和步骤,对于初学者来说,这是一个很好的学习资源。 总的来说,SpringBoot为开发者提供了丰富的选择,可以根据项目需求灵活选择JSP或Thymeleaf。JSP适合对Java语法熟悉、需要更多控制权的...

    springboot-demo.zip

    - **src/main/resources**:存放配置文件、静态资源和模板文件。 - **pom.xml**(或 build.gradle):构建文件,定义项目依赖和构建规则。 3. **主启动类**: - 通常包含 `@SpringBootApplication` 注解,这会...

    springboot-mybatis-flex.zip

    - `main/resources`: 存放非Java代码资源,如配置文件、静态资源、国际化文件等。 - `main/webapp`: 如果是Web项目,会包含Web应用的目录结构,如WEB-INF、static、templates等。 - `test/java`: 测试代码目录,...

    springboot-03-web.zip

    在这个员工管理系统中,我们可以在`java`目录下找到主要的业务逻辑和控制器,`resources`目录则可能包含应用的配置文件(如application.properties或yml)以及静态资源(如HTML、CSS、JavaScript)。 5. `.idea`...

    springboot-demo

    - `src/main/resources`:资源文件夹,包括配置文件、静态资源和模板文件。 - `pom.xml`(或`build.gradle`):构建文件,管理项目依赖和构建过程。 3. **SpringBoot-Demo项目**: - 项目可能包含一个名为`...

    springboot-redis.zip

    这些文件是Eclipse IDE的工作空间配置文件,`.classpath`包含了项目所需的类路径信息,`.project`定义了项目的属性和构建设置,`.settings`目录则包含特定于工作空间的配置。 7. **target**: 在Maven构建过程中...

Global site tag (gtag.js) - Google Analytics