0

I'm facing a problem similar to this one, except I'm getting 400 error:

Failed to load resource: the server responded with a status of 404 (Not Found)

I'm compiling the application as an executable. I placed css file under:

resources\static\css

and in my home.html under resources\templates I'm referencing it as:

<link rel="stylesheet" type="text/css" media="all" th:href="@{/css/my.css}" />

I also tried placing it under:

resources\public\css

Also under various locations within the folder structure, with various href values, but for any location/href I get a 400 error. What am I missing?

UPDATE:

Added WebMmvConfig.java:

@EnableWebMvc
public class WebMvcConfig extends WebMvcConfigurerAdapter {

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

and changed link to:

<link rel="stylesheet" type="text/css" media="all" th:href="@{/static/css/cc.css}" />

I'm still getting the same error...

UPDATE 2

I disabled web security completely in the application and sprinkled css files all over to see where it would read it from. It turns out that it does read the css file from webapp\css folder, not resources*... so this works, with web security disabled:

<link rel="stylesheet" type="text/css" media="all" th:href="@{css/cc.css}" href="css/cc.css"/>

But when I enable security it fails with 400 error. This is my config file (in a nutshell):

@EnableWebSecurity
@Configuration
public class WebSecurityConfig extends WebSecurityConfigurerAdapter {

    @Autowired
    private UserDetailsService userDetailsService;

    protected void configure(HttpSecurity http) throws Exception {
        http
            .csrf()
                .disable()
            .authorizeRequests()
                .antMatchers("/", "/login**", "/logout**", "/signup").permitAll()
                .anyRequest().authenticated()
        .and()
            .formLogin()
                .loginPage("/login")
                .defaultSuccessUrl("/my_account", true)
        .and()
            .logout()
                .logoutUrl("/logout")
                .logoutSuccessUrl("/");
   }

    @Autowired
    public void configureGlobal(AuthenticationManagerBuilder auth) throws Exception {
        auth
            .userDetailsService(userDetailsService);
    }        
}

So, I guess the question is, why is it loading from webapp instead of resources?

Community
  • 1
  • 1
ikcodez
  • 323
  • 2
  • 16
  • try this : http://stackoverflow.com/questions/39024767/how-to-add-static-files-using-spring-mvc-and-thymeleaf/39028314#39028314 – AchillesVan Sep 04 '16 at 15:58
  • @Georgesvanhoutte added config file and changed the link (added these in the description), but to no avail... – ikcodez Sep 04 '16 at 16:09
  • A 400 can mean lots of things. Is there a server-side stacktrace to go with that exception? – rorschach Sep 04 '16 at 17:30
  • If, as the tags suggest, you're using Spring Boot then you don't need your `WebMvcConfig` class. – Andy Wilkinson Sep 04 '16 at 17:53
  • As was said before, with error 400, an exception must have been thrown at the server side. You might want to provide that. And also, with spring boot, you don't need WebMvcConfig. – Olantobi Sep 04 '16 at 18:27
  • Just tested it again, for Spring Boot it's completely enough to put the file into resources/static to be visible, for example, with maven, /src/main/resources/static/css/my.css -> there, without extra configuration – Florian Schaetz Sep 04 '16 at 19:26
  • You should post your pom.xml and the stacktrace. And yes, WebMvcConfig is not needed but it won t harm your code since its just overriding the default config. . – AchillesVan Sep 04 '16 at 19:48

0 Answers0