首页 > 解决方案 > Spring Boot Thymeleaf 显示不工作

问题描述

我刚刚使用 Thymeleaf 创建了一个非常基本的 Spring 应用程序,但我不知道它为什么不起作用。我什至无法显示我的<h1>标签。

我基本上可以显示所有内容,但需要通过 Thymeleaf 显示。问题是我以前用过它,它工作得很好。我不知道这里出了什么问题,我确实花了一整天的时间寻找解决方案。

我尝试升级我的 Java JDK,不起作用(甚至不确定它与它有什么关系),目前使用的是 STS 4,我也在 Netbeans 11 上尝试过,结果相同。我已将所有存在的 Thymeleaf 依赖项添加到我pom.xml的 . 即使在我的控制台日志中,我什么也没有得到,没有例外,没有警告,什么也没有。

我的目标是编写一些有点复杂的东西,所以如果我什至不能从这样一个基本的东西开始,我就不会走那么远。

希望有人会帮助我找到解决方案,因为我只是不知道该怎么做。

注意:我使用的是 Ubuntu 18.04

我的 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>

我的控制器:

    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";
    }

}

我的 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> 

标签: javaspringspring-bootthymeleaf

解决方案


这是一种解决方法:

@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;
    }

}

这完美地工作......


推荐阅读