Springboot не загружает мою HTML страницу с помощью Thymeleaf - PullRequest
1 голос
/ 07 января 2020

Я уже был на эти темы, но у меня не сработало:

  1. Topic1 Нет @EnableWebMvc аннотация в моем проекте

This это зависимость, которую я использовал для Thymeleaf:


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

  • Мой новый контроллер с именем 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";
    }

}
  • HTML с именем uploadView.html
<!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

Edit1:

My @SpringBootApplication class

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

Сервер logs

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

Страница ошибки Whitelabel:

enter image description here

1 Ответ

1 голос
/ 07 января 2020

Это как сказал М. Дейн. Вы используете 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>

Тогда это работает как очаровательный.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...