弹簧启动百里香叶显示不工作



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

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

我尝试升级我的Java JDK,不起作用(甚至不确定它与它有什么关系(,目前使用的是STS 4,我也尝试了Netbeans 11,结果相同。我已经将所有存在的百里香叶依赖项添加到我的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";
}
}

我的索引.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;
}
}

这非常有效...

如果您使用 Spring Boot 启动器,则不需要单独包含 Thymeleaf 依赖项。兼容版本的所有必要依赖项都已包含在 Thymeleaf 启动器中。 我会首先尝试删除这些代码,因为您的其余代码乍一看似乎很好。

最新更新