0

Why styles and js are not connected to the page? When displaying a page, I get an error at these files.

Failed to load resource: the server responded with a status of 404

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/sources/css/styles.css">

</head>
<body >
<h1>Hey there</h1>
<a href="/departments">click here</a>

<script src="/sources/scripts/initial_service.js"></script>
</body>
</html>

The project structure:

image

The Web Config:

@EnableWebMvc
@Configuration
@ComponentScan(basePackages = {"controller"})
public class WebConfig  extends WebMvcConfigurerAdapter {

    @Bean
    public ViewResolver internalResourceViewResolver() {
        InternalResourceViewResolver bean = new InternalResourceViewResolver();
        bean.setViewClass(JstlView.class);
        bean.setPrefix("/WEB-INF/");
        bean.setSuffix(".html");
        return bean;
    }

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/css/**").addResourceLocations("/sources/css/");
        registry.addResourceHandler("/image/**").addResourceLocations("/sources/image/");
        registry.addResourceHandler("/scripts/**").addResourceLocations("/sources/scripts/");
        registry.addResourceHandler("/*.html").addResourceLocations("/WEB-INF/");
    }

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/main.html");
    }
}

What could be wrong?

3
  • 2
    you add the resource handler, but you don't use it. remove /sources in your HTML Commented Oct 17, 2018 at 11:59
  • Ok, I'll try now Commented Oct 17, 2018 at 11:59
  • @PhilippSander You're right, thanks! Commented Oct 17, 2018 at 12:04

2 Answers 2

3

I think this one will reduce your code

@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/source/**")
        .addResourceLocations("/source/");
    }
Sign up to request clarification or add additional context in comments.

9 Comments

I like this approach more than mine
@PhilippSander My pleasure !
@AvijitBarua But after changing on the page and in the config, I get the same error: Failed to load resource: the server responded with a status of 404 ()
@VladislavSolopov If you use my code you would not need to change your jsp page. Just keep it as it was. like <link rel="stylesheet" href="/sources/css/styles.css">
@VladislavSolopov Pleasure buddy !
|
1

you add resource handlers but you do not use them. remove /sources from the paths where you load your resources.

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <link rel="stylesheet" href="/css/styles.css">

</head>
<body >
<h1>Hey there</h1>
<a href="/departments">click here</a>

<script src="/scripts/initial_service.js"></script>
</body>
</html>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.