无法共享静态资源

Not able to share static resources

我正在通过 YouTube 指南学习 spring)我很想开始自己的项目,但遇到了一些问题。目录 /static/** 中的资源未提供给外部世界 - 404 错误。 /静态/位于 /src/main/resources/

我试过: 添加

spring.resources.static-locations=/js/,/css/
spring.mvc.view.prefix=/static/css/
spring.mvc.view.suffix=.css

在 application.properties - 没有变化。

当我添加

registry.addResourceHandler("/img/**").addResourceLocations("file:///"+uploadPath+"/");

public void addResourceHandlers(ResourceHandlerRegistry registry)

如果我将 .js 文件添加到 /static/ 或 /static/js IDEA 通常会将调用从我的代码链接到脚本。但是当应用程序启动时,脚本不起作用。

@Configuration

public class MvcConfig implements WebMvcConfigurer {

    public void addViewControllers(ViewControllerRegistry registry) {

        registry.addViewController("/login").setViewName("login");

    }

    @Override

    public void addResourceHandlers(ResourceHandlerRegistry registry) {

        registry.addResourceHandler("/static/**")

                .addResourceLocations("classpath:/static/");
    }}


    WebSecurityConfig

    @Configuration

    @EnableWebSecurity

    @EnableGlobalMethodSecurity(prePostEnabled = true)

    public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserService userService;

    @Autowired
    private PasswordEncoder passwordEncoder;


    @Override

    protected void configure(HttpSecurity http) throws Exception {
        http
                .authorizeRequests()

                .antMatchers("/users","/static/**").permitAll()

                //.anyRequest().authenticated()

                .anyRequest().permitAll()

                    .and()

                .formLogin()

                .loginPage("/login")

                .permitAll()

                    .and()

                .rememberMe()

                    .and()

                .logout()

                .permitAll();
    }

    @Override

    protected void configure(AuthenticationManagerBuilder auth) throws 
    Exception {

        auth.userDetailsService(userService)

                .passwordEncoder(passwordEncoder);
    }
} 

application.properties

spring.datasource.driver-class-name=org.postgresql.Driver

    spring.jpa.database-platform=org.hibernate.dialect.PostgreSQLDialect

    spring.datasource.url=jdbc:postgresql://localhost:5432/banking

    spring.session.store-type=jdbc

    spring.session.jdbc.initialize-schema=always

    spring.datasource.username=postgres

    spring.datasource.password=123456

    spring.jpa.hibernate.ddl-auto=update

    spring.jpa.generate-ddl=false

    spring.jpa.show-sql=true

    spring.jpa.properties.hibernate.jdbc.lob.non_contextual_creation=true

    spring.freemarker.expose-request-attributes=true

项目结构

├───main
│   ├───java
│   │   └───web
│   │       ├───configs
│   │       ├───controllers
│   │       ├───domain
│   │       ├───Repositories
│   │       ├───service
│   │       └───validator
│   └───resources
│       └───templates
│           ├───parts
│           └───static
│               ├───css
│               └───js
└───test
    └───java

请将您的目录从

 src/main/resources/templates/static/

 src/main/resources/static/

默认情况下 Spring Boot 将从类路径中名为 /static(或 /public 或 /resources 或 /META-INF/resources)的文件夹或从ServletContext

谢谢