java - Spring Boot Thymeleaf 显示不工作

标签 java spring 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>

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

}

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

最佳答案

这里有一个解决方法:

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

}

这非常有效...

关于java - Spring Boot Thymeleaf 显示不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59010341/

相关文章:

java - 网页异步更新有哪些方式?

Java documentlistener - 输入后程序停止工作

java - 如何验证泛型类型扩展了 Java 中的另一个类/接口(interface)?

java - 了解项目中的工作流程

spring - 为什么我的 Spring Web 应用程序项目不能正确运行 Web 和 Thymeleaf 依赖项?

java - 不兼容类型 : String cannot be converted to File

java - Jax-rs apache-cxf :No resource methods have been found for resource class

java - Spring boot + Thymeleaf - 编辑时 id 为 null 的对象

java - Spring Boot 测试中的 MockBean 注解导致 NoUniqueBeanDefinitionException

java - @RefreshScope 似乎忽略了 Mockito 的模拟