【发布时间】:2020-01-13 01:46:13
【问题描述】:
目前我正在尝试学习 Spring Boot,因此认为使用 Spring Boot Web MVC 编写 Web 应用程序会很好。我被困在一个问题上,在谷歌上没有找到解决方案或任何有帮助的东西。我希望这里有人可以帮助我。
该项目的目标是使用最新版本的 Spring Boot、Thymeleaf、Bootstrap 和 Hibernate 构建一个 Web 应用程序。我已经使用了几个教程,但有些东西让我感到困惑。
我使用 Thymeleafs 布局方言对视图进行了编码,但是当我在本地 Tomcat 上部署项目并浏览索引页面时,我只得到一个带有“索引”的字符串,而不是一个 html 文件。我不知道在哪里寻找问题。我认为我尝试集成布局方言的方式或尝试返回视图的方式是错误的。我希望有人有一个想法。
控制器:
package de.rune.flying.flying.web.controller;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.servlet.ModelAndView;
@Controller
public class UserController {
@GetMapping ( "/" )
public ModelAndView main ( Model model ) {
ModelAndView modelAndView = new ModelAndView ( );
modelAndView.setViewName ( "index" );
return modelAndView;
}
}
我已经尝试过另一种方法(见下文),但没有任何改变。
其他方法:
@GetMapping ( "/" )
public String main ( Model model ) {
return "index.html";
}
pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.7.RELEASE</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>de.rune.flying</groupId>
<artifactId>flying</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>flying</name>
<properties>
<java.version>1.8</java.version>
<bootstrap.version>4.3.1</bootstrap.version>
<thymeleaf.dialect.version>2.4.1</thymeleaf.dialect.version>
</properties>
<dependencies>
<!-- web mvc -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- thymeleaf -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>
<dependency>
<groupId>nz.net.ultraq.thymeleaf</groupId>
<artifactId>thymeleaf-layout-dialect</artifactId>
<version>${thymeleaf.dialect.version}</version>
</dependency>
<!-- test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
</dependency>
<!-- dev tools -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-devtools</artifactId>
</dependency>
<!-- optional -->
<dependency>
<groupId>org.webjars</groupId>
<artifactId>bootstrap</artifactId>
<version>${bootstrap.version}</version>
</dependency>
</dependencies>
<build>
<finalName>flying</finalName>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<addResources>true</addResources>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.0</version>
</plugin>
</plugins>
</build>
</project>
layout.html:
<!DOCTYPE html>
<html lang="de"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">
<title layout:title-pattern="$LAYOUT_TITLE - $CONTENT_TITLE">Layout</title>
<link rel="stylesheet" th:href="@{webjars/bootstrap/4.3.1/css/bootstrap.min.css}"/>
<link rel="stylesheet" th:href="@{/static/css/main.css}">
</head>
<body>
<header>
MY Header
</header>
<section layout:fragment="content">
<p>Content goes here</p>
</section>
<footer>
<p>My footer</p>
<p layout:fragment="footer">Footer goes here</p>
</footer>
<script type="text/javascript" th:src="@{webjars/bootstrap/4.3.1/js/bootstrap.min.js}"></script>
</body>
</html>
index.html
<!DOCTYPE html>
<html lang="de"
xmlns:layout="http://www.ultraq.net.nz/thymeleaf/layout"
xmlns:th="http://www.thymeleaf.org"
layout:decorate="~{layouts/layout.html}">
<head>
<title>Index title</title>
</head>
<body>
<section layout:fragment="content">
<p>Index content</p>
</section>
</body>
</html>
如果缺少某些信息,请告诉我,然后我会上传它们并感谢您的帮助
已编辑:
当我在浏览器中检查视图的源代码时,我只看到文本“索引”,所以看起来他只是返回字符串并且找不到具有名称的视图。
【问题讨论】:
-
您能否提供您的项目结构的屏幕截图。
-
当然,我做到了
-
你试过我的解决方案了吗?
-
感谢您的帮助,我已经尝试了您的解决方案,但还是一样。同样没有布局方言的依赖,我该如何使用模板引擎?
-
哦,是的,我忘了....现在可以了。似乎它也适用于布局方言和装饰。谢谢
标签: spring spring-boot model-view-controller web-applications thymeleaf