java - Springboot 不会使用 Thymeleaf 加载我的 HTML 页面

标签 java spring spring-boot thymeleaf

已经研究过这些主题,但对我来说没有用:

  1. Topic1我的项目中没有 @EnableWebMvc 注释

这是我用于 Thymeleaf 的依赖项:


<!-- https://mvnrepository.com/artifact/org.thymeleaf/thymeleaf -->
<dependency>
    <groupId>org.thymeleaf</groupId>
    <artifactId>thymeleaf</artifactId>
    <version>3.0.11.RELEASE</version>
</dependency>

  • 我的新 Controller 名为ViewController
package myrest.Viewcontroller;

//Imports

@Controller
public class ViewController {


    public static String uploadDirectory = System.getProperty("user.dir")+"/uploads";
    @RequestMapping("/uploadendpoint")
    public String uploadPage(Model model)
    {
        return "uploadView";
    }

}
  • 名为 uploadView.htmlHTML
<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8"></meta>
<title>Insert title here</title>
</head>
<body>

<form action = "/upload" method="post" enctype="multipart/form-data">
    <input type="file" name="files" multiple>
    <input type="submit" value="Upload Files"></input>

</form>

</body>
</html>

  • 项目结构:

enter image description here

我的评论:我仍然收到 Whitelabel 错误页面,位于 localhost:8082/uploadendpoint

编辑1:

我的@SpringBootApplication

package myrest;

import java.io.File;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;

import myrest.Viewcontroller.ViewController;
import myrest.controller.MainController;

@SpringBootApplication
public class RemoteapiApplication {

    public static void main(String[] args) {
        new File(ViewController.uploadDirectory).mkdir();
        SpringApplication.run(RemoteapiApplication.class, args);
    }

}

application.properties 文件:

server.port:8082
spring.servlet.multipart.max-file-size=15MB
spring.servlet.multipart.max-request-size=15MB

服务器的日志

2020-01-07 14:37:56.708  INFO 9317 --- [  restartedMain] myrest.RemoteapiApplication              : Starting RemoteapiApplication on ZenbookPro with PID 9317 (/home/pihill/Documents/workspace-sts-3.9.9.RELEASE/remoteapi/target/classes started by pihill in /home/pihill/Documents/workspace-sts-3.9.9.RELEASE/remoteapi)
2020-01-07 14:37:56.708  INFO 9317 --- [  restartedMain] myrest.RemoteapiApplication              : No active profile set, falling back to default profiles: default
2020-01-07 14:37:56.770  WARN 9317 --- [  restartedMain] org.apache.tomcat.util.modeler.Registry  : The MBean registry cannot be disabled because it has already been initialised
2020-01-07 14:37:56.781  INFO 9317 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat initialized with port(s): 8082 (http)
2020-01-07 14:37:56.782  INFO 9317 --- [  restartedMain] o.apache.catalina.core.StandardService   : Starting service [Tomcat]
2020-01-07 14:37:56.782  INFO 9317 --- [  restartedMain] org.apache.catalina.core.StandardEngine  : Starting Servlet engine: [Apache Tomcat/9.0.29]
2020-01-07 14:37:56.784  INFO 9317 --- [  restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring embedded WebApplicationContext
2020-01-07 14:37:56.784  INFO 9317 --- [  restartedMain] o.s.web.context.ContextLoader            : Root WebApplicationContext: initialization completed in 75 ms
2020-01-07 14:37:56.801  INFO 9317 --- [  restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor  : Initializing ExecutorService 'applicationTaskExecutor'
2020-01-07 14:37:56.814  INFO 9317 --- [  restartedMain] o.s.b.d.a.OptionalLiveReloadServer       : LiveReload server is running on port 35729
2020-01-07 14:37:56.820  INFO 9317 --- [  restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer  : Tomcat started on port(s): 8082 (http) with context path ''
2020-01-07 14:37:56.820  INFO 9317 --- [  restartedMain] myrest.RemoteapiApplication              : Started RemoteapiApplication in 0.119 seconds (JVM running for 2930.389)
2020-01-07 14:37:56.821  INFO 9317 --- [  restartedMain] .ConditionEvaluationDeltaLoggingListener : Condition evaluation unchanged
2020-01-07 14:37:59.725  INFO 9317 --- [nio-8082-exec-1] o.a.c.c.C.[Tomcat].[localhost].[/]       : Initializing Spring DispatcherServlet 'dispatcherServlet'
2020-01-07 14:37:59.726  INFO 9317 --- [nio-8082-exec-1] o.s.web.servlet.DispatcherServlet        : Initializing Servlet 'dispatcherServlet'
2020-01-07 14:37:59.727  INFO 9317 --- [nio-8082-exec-1] o.s.web.servlet.DispatcherServlet        : Completed initialization in 1 ms

白标错误页面:

enter image description here

最佳答案

正如M.Deinum所说。您正在使用 Spring Boot,因此可以从使用入门包中获益。 spring-boot-starter-thymleaf 包中包含的依赖项比仅在 org.thymeleaf 3.0.11.RELEASE 中包含的依赖项更多。

将其替换为您的 pom.xml:

<dependency>
  <groupId>org.thymeleaf</groupId>
  <artifactId>thymeleaf</artifactId>
  <version>3.0.11.RELEASE</version>
</dependency>

这样:

<dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

然后它就很迷人。

关于java - Springboot 不会使用 Thymeleaf 加载我的 HTML 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59628385/

相关文章:

java - 如何在android中将一个字符串添加到另一个字符串数组?

java - 在二维字符数组中查找单词

java - 在 Eclipse 中创建 Web 服务

java - 将字符串 x = "[]"映射到 JsonNode 会导致额外的引号(Spring Boot)

spring - 使用cron在Spring中安排周期性的 react 任务?

spring-boot - 如何使用Java中的Rest API在 Elasticsearch 中索引对象数组

mysql - Spring Boot应用程序MySQL SocketTimeoutException

java - 条件数量未知的 If 语句

java - "Cycle found"与 Spring Data Mongo 1.5

java - 从主类中的静态字段捕获异常