0
votes

I am new to Spring boot and I have learned that I can load HTML pages through Controller classes with the help of Thymeleaf dependency. and it did really work initially but it isn't now. Here's is my Controller class

package com.example.demo2;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class Controller2 {
    @GetMapping("/test2")
    public String sdf() {
        return "index";
    }
}

and here is my pom.xml

4.0.0 org.springframework.boot spring-boot-starter-parent 2.3.5.RELEASE com.example demo2 0.0.1-SNAPSHOT demo2 Demo project for Spring Boot

<properties>
    <java.version>11</java.version>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-data-jpa</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <scope>runtime</scope>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.apache.derby</groupId>
        <artifactId>derby</artifactId>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
        <exclusions>
            <exclusion>
                <groupId>org.junit.vintage</groupId>
                <artifactId>junit-vintage-engine</artifactId>
            </exclusion>
        </exclusions>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

and here is my project structure

enter image description here

1
hey can you move index.html file under the templates folderisadev41
@IsaToltar Yes This works! could you please explain why ?kevin godfrey
static folder is for your css,image and js files, you need the put your view files into the templates folderisadev41

1 Answers

2
votes

The @RestController annotation does set different defaults to be used for REST Apis (like content types and so on). Try using @Controller for the class and @RequestMapping for the method.