Skip to content Skip to sidebar Skip to footer

Why Can't I Load Js And Css Files On My Pages?

I'm running a SpringMVC + Thymeleaf application and would like to load javascript and CSS on my HTML5 pages. My login.html is: <

Solution 1:

I have developed same application like you. Just developed it and working fine for me.

Here is config:

@EnableWebMvc@Configuration@ComponentScan(basePackages = { "org.tnt.base.api" })
publicclassMvcConfigextendsWebMvcConfigurerAdapter {

    @OverridepublicvoidaddResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/css/**").addResourceLocations("/css/");
    }

    @OverridepublicvoidconfigureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }

    @BeanpublicInternalResourceViewResolverjspViewResolver() {
        InternalResourceViewResolver bean = newInternalResourceViewResolver();
        bean.setPrefix("/WEB-INF/views/");
        bean.setSuffix(".html");
        return bean;
    }

    ....

}

Here's a screenshot of my structure that works:

enter image description here

Here's my login.html code:

<!DOCTYPE html><htmllang="en"><head><metacharset="UTF-8"><title>TnT Quick Login</title><linkhref="../css/bootstrap.min.css" /></head><body>
....

Solution 2:

I think problem is in this line.

registry.addResourceHandler("/WEB-INF/assets/**").addResourceLocations("/WEB-INF/assets/");

you can try this instead of:

registry.addResourceHandler("assets/**").addResourceLocations("/WEB-INF/assets/");

and then try to link your css or js, like this:

<scriptsrc="assets/js/jquery-2.0.3.min.js"></script>

or

<scriptsrc="/assets/js/jquery-2.0.3.min.js"></script>

Post a Comment for "Why Can't I Load Js And Css Files On My Pages?"