【问题标题】:spring-boot-starter-web vs spring-boot-starter-thymeleafspring-boot-starter-web vs spring-boot-starter-thymeleaf
【发布时间】:2015-07-06 18:51:52
【问题描述】:

我尝试了解 Spring Boot 如何处理 html 页面。我开始遵循指南from spring.io。本指南展示了如何使用 html 页面和查看技术 Thymeleaf。它有页面:

<!DOCTYPE HTML>
<html xmlns:th="http://www.thymeleaf.org">
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <p th:text="'Hello, ' + ${name} + '!'" />
</body>
</html>

我已经改成简单了

<!DOCTYPE HTML>
<html>
<head>
    <title>Getting Started: Serving Web Content</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    Hello
</body>
</html>

spring boot reference documentation 之后将spring-boot-starter-thymeleaf(现在不需要)更改为spring-boot-starter-web,之后我就看不到网页了。我看到了结果:

There was an unexpected error (type=Not Found, status=404). 
No message available.

当我将 Gradle 依赖项返回到 thymeleaf 时,一切正常。

控制器src/main/java/hello/GreetingController.java

package hello;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Controller
public class GreetingController {

@RequestMapping("/greeting")
public String greeting() {
    return "greeting";
}

}

申请src/main/java/hello/Application.java

package hello;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Application {

public static void main(String[] args) {
    SpringApplication.run(Application.class, args);
}

}

有人能解释一下webthymeleaf 依赖关系对于来自src/main/resources/templates/greeting.html 的唯一HTML 页面有什么区别吗?

【问题讨论】:

  • spring-boot-starter-webspring-boot-starter-thymeleaf 的依赖项。所以thymeleaf是基于web

标签: rest spring-boot dependency-management


【解决方案1】:

它还应该使用 gradle 依赖项 org.springframework.boot:spring-boot-starter-web:1.2.5.RELEASE 而不是 org.springframework.boot:spring-boot-starter-thymeleaf:1.2.5.RELEASE

spring-boot-starter-web 的功能类似于使用 spring 开发 web 应用程序所需的一组基本依赖项。这些基本依赖项是:

  • 杰克逊数据绑定
  • 休眠验证器
  • 弹簧芯
  • 弹簧网
  • spring-webmvc
  • spring-boot-starter
  • spring-boot-starter-tomcat

spring-boot-starter-thymeleaf 基于 spring-boot-starter-web 并添加了一些额外的依赖项,例如 thymeleaf 模板引擎:

  • 百里香布局方言
  • 弹簧芯
  • spring-boot-starter
  • spring-boot-starter-web
  • 百里香弹簧4

您可以在 mvnrepository.com 上查找此内容(spring-boot-starter-thymeleafspring-boot-starter-web)。

【讨论】:

  • 好的,如果 org.springframework.boot:spring-boot-starter-thymeleaf:1.2.5.RELEASE 只是将 thymeleaf 依赖项添加到 org.springframework.boot:spring-boot-starter-web:1.2.5.RELEASE,那么我应该在依赖项中添加什么以正确使用像 thymeleaf 依赖项这样的 html 页面?为什么只是 thymeleaf 依赖项与 html 页面一起工作?我在问我忘记添加什么才能正常工作?
  • 所以你只有一个 index.html。没有控制器怎么办?
  • 我写的,所以我使用了来自spring.io. 的指南,只是更改了页面和spring-boot-starter。当我使用thymeleaf 时一切正常,但为什么不能使用starter-web
  • 你的问题是什么?
猜你喜欢
  • 2014-04-07
  • 2016-01-29
  • 2023-04-04
  • 2017-02-19
  • 1970-01-01
  • 2019-10-28
  • 2015-07-07
  • 2020-07-02
  • 2017-03-23
相关资源
最近更新 更多