Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot returns string and not the view

Currently I'm trying to learn Spring Boot and therefore thought it would be good to write a web-application with Spring Boot Web MVC. I got stuck on a problem and have not found a solution or anything on Google which helped. I hope someone here could help me.

Goal of the project is to build a web application with Spring Boot, Thymeleaf, Bootstrap and Hibernate, all with the newest version. I've used several tutorials but there are some things which are confusing me.

I coded the views with Thymeleafs layout-dialect, but when I deploy the project on my local Tomcat and browse the index page I just get a String with "index" and not an html-file. I don't know where to look for the problem. I assume the way I try to integrate the layout-dialect or the way I try to return the view is wrong. I hope someone has an idea.

Controller:

package de.rune.flying.flying.web.controller;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class UserController {

    @GetMapping ( "/" )
    public ModelAndView main ( Model model ) {

        ModelAndView modelAndView = new ModelAndView ( );
        modelAndView.setViewName ( "index" );
        return modelAndView;
    }

}

I've already tried another method (see below) but it didn't changed anything.

Other kind of method:

        @GetMapping ( "/" )
        public String main ( Model model ) {
            return "index.html";
        }

pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.7.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>de.rune.flying</groupId>
    <artifactId>flying</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>
    <name>flying</name>

    <properties>
        <java.version>1.8</java.version>
        <bootstrap.version>4.3.1</bootstrap.version>
        <thymeleaf.dialect.version>2.4.1</thymeleaf.dialect.version>
    </properties>

    <dependencies>

        <!-- web mvc -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>

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

        <dependency>
            <groupId>nz.net.ultraq.thymeleaf</groupId>
            <artifactId>thymeleaf-layout-dialect</artifactId>
            <version>${thymeleaf.dialect.version}</version>
        </dependency>

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

        <!-- dev tools -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>

        <!-- optional -->
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>${bootstrap.version}</version>
        </dependency>

    </dependencies>

    <build>
        <finalName>flying</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <addResources>true</addResources>
                </configuration>
            </plugin>

            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-surefire-plugin</artifactId>
                <version>2.22.0</version>
            </plugin>
        </plugins>
    </build>

</project>

layout.html:

<!DOCTYPE html>
<html lang="de"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
    <title layout:title-pattern="$LAYOUT_TITLE - $CONTENT_TITLE">Layout</title>

    <link rel="stylesheet" th:href="@{webjars/bootstrap/4.3.1/css/bootstrap.min.css}"/>
    <link rel="stylesheet" th:href="@{/static/css/main.css}">
</head>
<body>
    <header>
        MY Header
    </header>
    <section layout:fragment="content">
        <p>Content goes here</p>
    </section>

    <footer>
        <p>My footer</p>
        <p layout:fragment="footer">Footer goes here</p>
    </footer>

    <script type="text/javascript" th:src="@{webjars/bootstrap/4.3.1/js/bootstrap.min.js}"></script>
</body>
</html>

index.html

<!DOCTYPE html>
<html lang="de"
      xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
      xmlns:th="http://www.thymeleaf.org"
    layout:decorate="~{layouts/layout.html}">
<head>
    <title>Index title</title>
</head>
<body>
    <section layout:fragment="content">
        <p>Index content</p>
    </section>
</body>
</html>

Let me know if some informations are missing, then I will upload them and thanks for help

edited:

project structure

Screenshot of resulted view

When I inspect the source code of the view in my browser I just see the text "index", so it seems like he just returns the string and can't find a view with the name.

like image 601
André Schmidt Avatar asked Oct 28 '25 15:10

André Schmidt


1 Answers

I tried to get your project run and first I had the same problem.

But I found out that some warnings occur during building the project.

WARNING: An illegal reflective access operation has occurred
WARNING: Illegal reflective access by org.codehaus.groovy.vmplugin.v7.Java7$1 (file:/C:/Users/Constantin%20Beer/.m2/repository/org/codehaus/groovy/groovy/2.5.7/groovy-2.5.7.jar) to constructor java.lang.invoke.MethodHandles$Lookup(java.lang.Class,int)
WARNING: Please consider reporting this to the maintainers of org.codehaus.groovy.vmplugin.v7.Java7$1
WARNING: Use --illegal-access=warn to enable warnings of further illegal reflective access operations
WARNING: All illegal access operations will be denied in a future release

To get rid of these I had to comment out following dependencies in the pom file:

<dependency>
            <groupId>nz.net.ultraq.thymeleaf</groupId>
            <artifactId>thymeleaf-layout-dialect</artifactId>
            <version>${thymeleaf.dialect.version}</version>
        </dependency>

As a result the index.html was displayed correctly.

Seems like Thymeleaf Layout Dialect is not working right with Spring Boot 2.1.7. I also tried other versions of 2.xx but couldn't get it worked.

So for you just comment out Thymeleaf Layout Dialect and it should work.

like image 185
Constantin Beer Avatar answered Oct 31 '25 06:10

Constantin Beer