簡體   English   中英

簡單的Spring Boot Webapp不起作用,whitelabel錯誤頁面

[英]Simple spring boot webapp doesnt work, whitelabel error page

我嘗試運行我的簡單“ hello world” Spring Boot Web應用程序,但是它不起作用,我一直都得到“ Whitelabel錯誤頁面”。

我的控制器

package com.packt.webapp.controller;

@Controller
@RequestMapping("/")
public class HomeController {

    public String home(Model model){
        String welcome = new String("Witam");
        model.addAttribute("welcome", welcome);
        return "home";
    }
}

應用程序

package com.packt.webapp.controller;

@Configuration
@EnableAutoConfiguration
@ComponentScan
public class Application {

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

home.html

    <!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
    <head>
        <title>Test</title>
        <link rel="stylesheet" th:href="@{/css/style.css}" />
    </head>
    <body>
        <h1>Hello</h1>
        <span th:text="'Message: ' + ${welcome}"></span>
    </body>
</html>

pom.xml

<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
    http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.packt.webapp</groupId>
    <artifactId>basket</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.1.4.RELEASE</version>
    </parent>

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

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

我究竟做錯了什么? 我試圖更改我的項目結構,正在使用依賴項,沒有任何幫助。 有人可以啟發我嗎?

您的home()方法很可能在spring之前沒有注冊,因為該方法上方沒有@RequestMapping()。

如果要將房屋注冊到/,則有兩個選擇。 您可以像這樣將當前在類級別的@RequestMapping移動到home方法。

@Controller
public class HomeController {
    @RequestMapping("/")
    public String home(Model model){
        String welcome = new String("Witam");
        model.addAttribute("welcome", welcome);
        return "home";
    }
}

或者,您可以在home()方法上方添加一個附加注釋,並將其留空。

@Controller
@RequestMapping("/")
public class HomeController {
    @RequestMapping()
    public String home(Model model){
        String welcome = new String("Witam");
        model.addAttribute("welcome", welcome);
        return "home";
    }
}

根據上面的評論,您訪問的網址錯誤。 由於您已將控制器映射到/並且只有1方法,因此應按以下方式進行訪問:

http://localhost:8080/

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM