静态资源处理

This commit is contained in:
2026-10-30 09:00:45 +08:00
parent 6e331d7e1d
commit 42d6975c10
6 changed files with 86 additions and 0 deletions
+34
View File
@@ -0,0 +1,34 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.linlangjz</groupId>
<artifactId>SpringStudy</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<artifactId>Spring-ch14-staticAndCORS</artifactId>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>jakarta.servlet</groupId>
<artifactId>jakarta.servlet-api</artifactId>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
</dependency>
<dependency>
<groupId>org.hibernate.validator</groupId>
<artifactId>hibernate-validator</artifactId>
</dependency>
</dependencies>
</project>
@@ -0,0 +1,23 @@
package com.ycjy;
import com.ycjy.config.AppConfig;
import com.ycjy.config.WebConfig;
import org.jspecify.annotations.Nullable;
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
public class MyInit extends AbstractAnnotationConfigDispatcherServletInitializer {
@Override
protected Class<?> @Nullable [] getRootConfigClasses() {
return null;
}
@Override
protected Class<?> @Nullable [] getServletConfigClasses() {
return new Class[]{AppConfig.class, WebConfig.class};
}
@Override
protected String[] getServletMappings() {
return new String[]{"/"};
}
}
@@ -0,0 +1,11 @@
package com.ycjy.config;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
@Configuration
@EnableWebMvc
@ComponentScan({"com.ycjy.web"})
public class AppConfig {
}
@@ -0,0 +1,14 @@
package com.ycjy.config;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**")
.addResourceLocations("/static/","classpath:/static/");
}
}
@@ -0,0 +1,3 @@
body{
background: #000;
}
+1
View File
@@ -22,6 +22,7 @@
<module>Spring-ch11-getData</module>
<module>Spring-ch12-verify</module>
<module>Spring-ch13-Response</module>
<module>Spring-ch14-staticAndCORS</module>
</modules>
<properties>