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