Imeshka sam
Imeshka sam

Reputation: 31

Cannot display the index.html page in Spring boot

I tried to create a simple spring boot application

I created spring boot application class, configuration class, controller class and the index.html.

I added Thymeleaf dependencies and put html page under resources folder (\src\main\resources\templates\index.html)

But when I run the application it gives an error

org.thymeleaf.exceptions.TemplateInputException:
Error resolving template "index.html", template might not exist or might not be accessible by any of the configured Template Resolvers

Please give me a solution.

@SpringBootApplication
@ComponentScan(basePackages = {"com.mail"})
public class SpringBootWebApplication {

    public static void main(String[] args) {
        SpringApplication.run(SpringBootWebApplication.class, args);
    }
}

Controller class

@RestController
public class IndexController {

    @RequestMapping(value="/", method = RequestMethod.GET)
    String index(){
        System.out.println("..............hit");
        return "index";
    }

WebConfiguration for Thymeleaf

@Configuration
public class WebConfiguration extends WebMvcConfigurerAdapter{
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index.html");
        registry.setOrder(Ordered.HIGHEST_PRECEDENCE);
    }
}

index.html page

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head lang="en">
    <title>Spring Framework</title>
</head>
<body>
    <h1> Hello Spring Boot </h1>
</body>
</html>

Upvotes: 3

Views: 8786

Answers (5)

singh09iet
singh09iet

Reputation: 11

Read point number 2 here.
Add the following dependency in your pom:

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>

Upvotes: 0

Dhaval Simaria
Dhaval Simaria

Reputation: 1964

As described by previous answers, @RestController should be replaced with @Controller.

When we use @RestController, return statement value will be sent as String Response. But when we use @Controller the return statement value will be considered as the view to be rendered.

Refer Diff. b/w @RestController and @Controller

Also, Spring Boot will automatically look for index.html in '\src\main\resources\templates\' which is the default location for views in Spring Boot when we are using Template Engines like Thymeleaf. Refer Spring Boot and Template Engines.

Hence we do not need to explicitly define WebConfiguration class because then we're required to define view-resolvers that are configured in the XML file or in Java class using ViewRegistry. Spring Boot eliminates the need for both this XML and Java View Registry. So you can also do away with WebConfiguration class.

Upvotes: 0

andrey4623
andrey4623

Reputation: 81

Try to do the following:

  1. Replace @RestController by @Controller;
  2. I think you don't need WebConfiguration; the controller returns “index” which means “render the index.html template”. Thymeleaf will find the template in the resources/templates folder.

Upvotes: 6

Denys
Denys

Reputation: 1478

Looks like static resources are not accessible by default. Try to provide access to static resources adding resource handler, something like:

@Configuration
public class StaticResourceConfiguration extends WebMvcConfigurerAdapter {

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

Upvotes: 0

so-random-dude
so-random-dude

Reputation: 16475

Try replacing @RestController with @Controller. I would start from the template generated by start.spring.io. And incrementally add functionalities one step at a time.

Or

If you just do some googling, you will be easily able to find some sample thymeleaf projects that actually works, start from there.

Upvotes: 1

Related Questions