简体   繁体   English

Spring Boot Thymeleaf 显示不工作

[英]Spring Boot Thymeleaf display not working

I've just created a very basic Spring app using Thymeleaf, and I have no idea why it's not working.我刚刚使用 Thymeleaf 创建了一个非常基本的 Spring 应用程序,但我不知道它为什么不起作用。 I can't even display my <h1> tag.我什至无法显示我的<h1>标签。

I basically get everything displayed but what I need to be displayed via Thymeleaf.我基本上可以显示所有内容,但需要通过 Thymeleaf 显示。 The thing is I've used it before and it worked just fine.问题是我以前用过它,它工作得很好。 I have no idea what's wrong here and I've literaly spent the whole day looking for the solution.我不知道这里出了什么问题,我确实花了一整天的时间寻找解决方案。

I tried to upgrade my Java JDK, doesn't work (not even sure it has anything to do with it), currently using STS 4, I also tried on Netbeans 11, same result.我尝试升级我的 Java JDK,不起作用(甚至不确定它与它有什么关系),目前使用的是 STS 4,我也在 Netbeans 11 上尝试过,结果相同。 I've added all the Thymeleaf dependencies that exist to my pom.xml , and I just ran out of ideas.我已将所有存在的 Thymeleaf 依赖项添加到我的pom.xml中,但我只是没有想法。 Even in my console log I don't get anything, no exception, no warning, just nothing.即使在我的控制台日志中,我什么也没有得到,没有例外,没有警告,什么也没有。

I'm aiming to program something a bit complex, so if I can't even start with such a basic stuff, I won't go that far.我的目标是编写一些有点复杂的东西,所以如果我什至不能从这样一个基本的东西开始,我就不会走那么远。

Hopefully someone will help me find the solution cause I just don't know what to do anymore.希望有人会帮助我找到解决方案,因为我只是不知道该怎么做。

NB: I'm using Ubuntu 18.04注意:我使用的是 Ubuntu 18.04

The dependencies in my pom.xml:我的 pom.xml 中的依赖项:

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
        </dependency>
        <dependency>
            <groupId>org.apache.jena</groupId>
            <artifactId>apache-jena-libs</artifactId>
            <type>pom</type>
            <version>3.13.0</version>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf</artifactId>
            <version>3.0.9.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.thymeleaf</groupId>
            <artifactId>thymeleaf-spring4</artifactId>
            <version>3.0.9.RELEASE</version>
        </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.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </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>

My Controller :我的控制器:

    import java.util.List;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;


import com.semweb.bikeproject.model.Station;

@Controller
public class BikeController {

    private FusekiService fusekiService;

    @RequestMapping("/index")
    public String bike(Model model) {

        model.addAttribute("top", "TOP TOP MANI");


        return "index";
    }

}

my index.html template我的 index.html 模板

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<meta charset="UTF-8">
<body>

    <h1 th:text="${top}"></h1>

    <div id="googleMap" style="width:100%;height:700px;">
    </div>
    <form>
        <input type="button" value="Click me" onclick="msg()">
    </form>

    <script>

        var mapProp;
        var map;
        function myMap() {
        mapProp= {
            center:new google.maps.LatLng(51.508742,-0.120850),
            zoom:5,
        };

        map = new google.maps.Map(document.getElementById("googleMap"),mapProp);
        }

        function msg() {
            map.setCenter({lat: -34, lng: 151});
            map.setZoom(12);
        }
    </script>

    <script src="https://maps.googleapis.com/maps/api/js?key=AIzaSallback=myMap"></script>

</body>
</html> 

Here is a workaround:这是一种解决方法:

@Controller
public class BikeController {

    private FusekiService fusekiService;

    @RequestMapping("/index")
    public ModelAndView bike() {

        ModelAndView modelAndView = new ModelAndView("index");
        modelAndView.addObject("top", "TOP TOP MANI");

        //...

        return modelAndView;
    }

}

This works perfectly...这完美地工作......

You don't need to include the Thymeleaf dependencies separately if you are using the Spring Boot starter for it.如果您使用 Spring Boot 启动器,则不需要单独包含 Thymeleaf 依赖项。 All necessary dependencies on compatible versions are already included in the Thymeleaf starter. Thymeleaf 启动器中已经包含了对兼容版本的所有必要依赖项。 I would first try removing these, because the rest of your code seems fine at first sight.我会首先尝试删除这些,因为您的其余代码乍一看似乎很好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM