spring-boot - 为Spring-boot应用程序获取whitelabel错误

标签 spring-boot gradle thymeleaf

我是Spring Boot开发的新手,请帮我解决以下错误

目录结构(仅包括重要文件的结构)

src
   -main
       -java
           -com.example.handlingformsubmission
               -Greeting.java
               -GreetingController.java
               -HandlingFormSubmissionApplication.java
       -resources
           -templates
               -greeting.html
               -result.html
           -application.properties
   -test
       -java
           -com.example.handlingformsubmission
               -HandlingFormSubmissionApplicationTest.java
   -build.gradle


在执行期间,在浏览器上运行该错误时遇到了此错误
Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Wed Mar 11 19:08:44 IST 2020
There was an unexpected error (type=Not Found, status=404).
No message available

当我终止或停止申请时
Execution failed for task ':HandlingFormSubmissionApplication.main()'.
> Process 'command '/usr/lib/jvm/java-11-amazon-corretto/bin/java'' finished with non-zero exit value 143

目前我正在使用springboot版本2.2.2

这是我的代码

build.gradle
plugins {
    id 'org.springframework.boot' version '2.2.2.RELEASE'
    id 'io.spring.dependency-management' version '1.0.8.RELEASE'
    id 'java'
    id 'war'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '1.8'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-thymeleaf'
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation('org.springframework.boot:spring-boot-starter-test') {
        exclude group: 'org.junit.vintage', module: 'junit-vintage-engine'
    }
}

test {
    useJUnitPlatform()
}



HandlingFormSubmissionApplication.java
package com.example.handlingformsubmission;

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

@ComponentScan(basePackages = {"com.example.handlingformsubmission.greeting"})
@SpringBootApplication
public class HandlingFormSubmissionApplication {

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

}

GreetingController.java

package com.example.handlingformsubmission.greeting;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class GreetingController {

    @GetMapping("/greeting")
    public String greetingForm(Model model) {
        model.addAttribute("greeting", new Greeting());
        return "greeting";
    }

    @PostMapping("/greeting")
    public String greetingSubmit(@ModelAttribute Greeting greeting, Model model) {
        model.addAttribute("greeting", greeting);
        return "result";
    }

}


application.properties
server.port=8000


greeting.html
<!DOCTYPE HTML>
<html xmlns:th="https://www.thymeleaf.org">
<head> 
    <title>Getting Started: Handling Form Submission</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h1>Form</h1>
    <form action="#" th:action="@{/greeting}" th:object="${greeting}" method="post">
        <p>Id: <input type="text" th:field="*{id}" /></p>
        <p>Message: <input type="text" th:field="*{content}" /></p>
        <p><input type="submit" value="Submit" /> <input type="reset" value="Reset" /></p>
    </form>
</body>
</html>


result.html
<!DOCTYPE HTML>
<html xmlns:th="https://www.thymeleaf.org">
<head> 
    <title>Getting Started: Handling Form Submission</title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>
    <h1>Result</h1>
    <p th:text="'id: ' + ${greeting.id}" />
    <p th:text="'content: ' + ${greeting.content}" />
    <a href="/greeting">Submit another message</a>
</body>
</html>

最佳答案

该问题未指定,但我猜您要执行GET http://localhost:8000/greeting
我不会修复您的表单,但是 404 可以通过修复@ComponentScan来解决,例如...

// wrong: '.greeting' excludes the 'GreetingController'
@ComponentScan(basePackages = {"com.example.handlingformsubmission.greeting"})

// fixed: specifies a valid path that includes the controller
@ComponentScan(basePackages = {"com.example.handlingformsubmission"})

解决此问题后,您将可以继续解决下一个问题:

There was an unexpected error (type=Internal Server Error, status=500). An error happened during template parsing (template: "class path resource [templates/greeting.html]")



您还可以通过完全删除@ComponentScan来解决此问题-在此初始方案中不需要。

关于spring-boot - 为Spring-boot应用程序获取whitelabel错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60623300/

相关文章:

java - 获取某个日期范围内的记录

java - Tomcat 不提供静态文件

java - 使用 Proguard 转换类和资源时出错

mysql - 如何在 thymeleaf springboot 中填充选择元素?

spring-mvc - Spring Thymeleaf Bootstrap CSS

java - Spring Boot Actuator - 无法禁用/信息端点

java - Spring @ComponentScan 不适用于 @Repository

java - 提取 Spring Boot 用户属性

gradle - FileNotFoundException: `generated/source/apollo/generatedIR/main`

java - 如何替换 thymeleaf 链接中的 URL 参数?