Spring Boot Thymeleaf显示无法正常工作

时间:2019-11-23 17:32:01

标签: java spring spring-boot thymeleaf

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

我基本上可以显示所有内容,但是需要通过Thymeleaf显示。事情是我以前用过,而且效果很好。我不知道这里出了什么问题,我整天都在花时间寻找解决方案。

我试图升级我的Java JDK,它不起作用(甚至不能确定它与它有什么关系),目前使用STS 4,我也尝试对Netbeans 11进行同样的测试。我已经将所有Thymeleaf依赖项添加到我的pom.xml中,而我的想法就用光了。即使在控制台日志中,我也一无所获,没有异常,没有警告,只是一无所有。

我的目标是编写一些复杂的东西,因此,即使我什至不能从这样的基础知识入手,也不会走太远。

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

NB:我正在使用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> 

2 个答案:

答案 0 :(得分:1)

这是一种解决方法:

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

}

这很好用...

答案 1 :(得分:0)

如果您使用的是Spring Boot启动器,则不需要单独包含Thymeleaf依赖项。 Thymeleaf启动器中已包含对兼容版本的所有必要依赖关系。 我会首先尝试删除这些内容,因为乍看之下您的其余代码看起来还不错。