简体   繁体   English

简单的Spring Boot Webapp不起作用,whitelabel错误页面

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

I try run my simple "hello world" Spring Boot web application, but it doesn't work, I get "Whitelabel error page" all the time. 我尝试运行我的简单“ hello world” Spring Boot Web应用程序,但是它不起作用,我一直都得到“ Whitelabel错误页面”。

My Controller 我的控制器

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";
    }
}

Application.java 应用程序

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 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 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>

What am I doing wrong? 我究竟做错了什么? I tried change my project structure, was playing with dependencies, nothing helps. 我试图更改我的项目结构,正在使用依赖项,没有任何帮助。 Can somebody enlighten me? 有人可以启发我吗?

Your home() method is most likely not being registered by spring since there is no @RequestMapping() above the method. 您的home()方法很可能在spring之前没有注册,因为该方法上方没有@RequestMapping()。

If you want home to be registered to /, you have two options. 如果要将房屋注册到/,则有两个选择。 You can move the @RequestMapping that is currently at the class level to the home method like so... 您可以像这样将当前在类级别的@RequestMapping移动到home方法。

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

Or you can add an addition annotation above the home() method and leave it empty. 或者,您可以在home()方法上方添加一个附加注释,并将其留空。

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

Based on your comments above, you're accessing the wrong url. 根据上面的评论,您访问的网址错误。 Since you've mapped your controller to / and you only have the 1 method, you should access it like: 由于您已将控制器映射到/并且只有1方法,因此应按以下方式进行访问:

http://localhost:8080/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM