C
C
ceramicthree2020-09-18 23:21:25
Spring
ceramicthree, 2020-09-18 23:21:25

Why are static resources not connected in Spring MVC?

I created a ResourceHandler in MvcConfig, it looks like this:

@Configuration
@EnableWebMvc
public class MvcConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry
                .addResourceHandler("/static/**")
                .addResourceLocations("classpath:/resources/static/");
    }
}


The CSS file and the HTML pages themselves are in the resources folder: I include
9loXe1BqDsE.jpg

the CSS in the tag like this:
<link rel="stylesheet" type="text/css" th:href="@{/static/auth.css}">


But CSS is not included, what am I doing wrong?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Krasnov, 2020-09-24
@Favorskij

It needs to be placed in the right place and pointed out correctly.
For example, I have from like this:

@Override
    public final void addResourceHandlers(final ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/resources/styles/**", "/favicon.ico") // http адрес по каторому будет получен ресурс
                .addResourceLocations("/resources/styles/css", "/resources/styles/ico/") // путь до папки где будет лежать ресурс
                .setCachePeriod(0);
    }

links in html code look like this
<link href="/resources/styles/css/bootstrap.min.css" rel="stylesheet">
    <link href="/resources/styles/css/login.css" rel="stylesheet"/>

And the directory where the resources are stored
src\main\webapp\resources\styles\css
src\main\webapp\resources\styles\ico
Note that the resources are not stored in the resources folder by default

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question