Home > Net >  Java 8 Spring Boot - Cannot render index.html from Static folder on Websocket service
Java 8 Spring Boot - Cannot render index.html from Static folder on Websocket service

Time:08-14

I'm a relatively new Java developer that is working on a Java 8 Spring Boot project. I am trying to learn and understand WebSockets so I have created a basic Websocket server that appears to be up and running correctly.

I wanted to use the Static folder to render a basic HTML page with a JS file and CSS. From what I have researched, it should use it automatically, but I'm getting a standard whitepage error when I use the browser to connect when my research seems to indicate it should render by default?

I've heard that Thymeleaf is used to render templates, but I have also read that I can connect a basic HTML file from the Static folder instead. When I try to connect to the main server, it gives me the following logs from my different attempts to see the index.......

No mapping for GET /
No mapping for GET /index
No mapping for GET /index.html

I'm not sure what the problem could be and my Google-Fu is turning up no solutions. I feel like I must be missing something fundamental here, but I'm not sure how to properly map that route. I have Spring Web dependencies installed so I could create a RestController mapping to that route, but how would I properly return those static files?

CodePudding user response:

You need to create the endpoint for the default entry page of your application. Something like this:

@Controller
public class HomeController {
    @GetMapping("/")
    public String index() {
        return "index";
    }
}

Your index.html goes inside this path "src/main/resources/templates/".

CodePudding user response:

The solution I found through another source on Discord..

Part of the problem was I was using @RestController instead of @Controller.

The other issue was I had my index.html in the public folder instead of Templates.

And the final error was I had the incorrect Thymeleaf dependency. I needed the Thymeleaf Starter and I had accidentally added a different one from Maven Repo.

  • Related