Vadim Sheremetov
Vadim Sheremetov

Reputation: 590

Spring - Cannot resolve MVC "view" thymeleaf

I got a simple HomeController.class:

package com.example.tacos;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HomeController {

    @GetMapping("/")
    public String home() {
        return "home";
    }
}

Also I got a template called home.html

<!DOCTYPE HTML>
    <head>
    <title>Getting Started: Serving Web Content</title>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    </head>
    <body>
        <p>Hey</p>
    </body>
</html>

Anyway I'm getting 404 in the browser and IDE's telling me Cannot resolve MVC "view" as you can see on the screen

Folder Structure: Folder Structure

Browser: Browser

Upvotes: 8

Views: 24401

Answers (11)

Salvador Valencia
Salvador Valencia

Reputation: 1400

I had all the dependencies and settings for IntelliJ. What I was missing was settings for Thymeleaf on the application.properties (or applications.yml):

application.properties

spring.thymeleaf.prefix=classpath:/templates/
spring.thymeleaf.suffix=.html

application.yml

spring:   
  thymeleaf:
    prefix: classpath:/templates/  
    suffix: .html 

Upvotes: 0

djh
djh

Reputation: 11

I just encountered the same problem, but I managed to resolve it.

I simply copied the following code snippet exists in pom.xml:

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>

and modified artifactId to spring-boot-starter-thymeleaf,then, I faced the same problem.

In my case, I removed the scope element and reloaded the configuration. It seems to be working fine for now.

Upvotes: 0

Shark Deng
Shark Deng

Reputation: 1058

I previously added dependency

<!-- Thymeleaf -->
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
        <version>3.1.4</version>
    </dependency>

It doesn't work, even if I reload the Maven project.

Then I added <version>3.1.4</version> to reload again, it works!

Upvotes: 0

umutbilalokur
umutbilalokur

Reputation: 21

To anyone who use Gradle, I had this in my build.gradle file;

implementation 'org.thymeleaf:thymeleaf:3.1.2.RELEASE'

I have changed it to

implementation('org.springframework.boot:spring-boot-starter-thymeleaf:3.0.4')

and it worked for me.

Upvotes: 1

artimarti
artimarti

Reputation: 11

I found that my current project was created in another previous project.

Error was fixed after changing the project location.

Upvotes: 1

invzbl3
invzbl3

Reputation: 6440

I had the same issue in the project as:

"Cannot resolve MVC view"

and the cause of it was misspelling with naming of the folder.

The structure was:

   |   |-- resources
   |   |   |-- template
   |   |   |   |-- customer.html
   |   |   |   |-- customerForm.html
   |   |   |   |-- customers.html
   |   |   |   |-- index.html

I needed to use the name as templates of the folder, but I had template.

Maybe, it'd be helpful for someone also.

Upvotes: 2

Ando
Ando

Reputation: 1

I change the version of tomcat from 10.0.20 to 8.5.78 and sovle it.

Upvotes: 0

Mr Orange
Mr Orange

Reputation: 301

To solve this problem you should add path to web app package into Web Resource Directories.

  1. Alt + Ctrl + Shift + S ("Project Structure" window opens)
  2. Go to tab "Facets"
  3. Then click tab "Web"
  4. Add new package to "Web Resource Directories"

enter image description here

Upvotes: 7

Bolek
Bolek

Reputation: 81

I had the correct entry suggested by gumira in my pom.xml and I was still getting the error.

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

I had to fix Maven Plugin not found in IntelliJ IDE instead and rebuild.

Upvotes: 0

gumira
gumira

Reputation: 136

Hi I had the same issue and I fixed it by delete the version of Thymeleaf so the pom file will be like :

 <dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

Upvotes: 12

Vadim Sheremetov
Vadim Sheremetov

Reputation: 590

The problem was that in pom.xml I had this:

<dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf</artifactId>
            <version>3.0.11.RELEASE</version>
</dependency>

instead of this:

 <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-thymeleaf</artifactId>
        <version>2.2.2.RELEASE</version>
    </dependency>

Just started learning spring and don`t get dependencies well enough yet:)

Upvotes: 2

Related Questions