简单的春季启动webapp无法正常工作,whitelabel错误页面

时间:2017-03-17 16:04:33

标签: java spring spring-mvc spring-boot

我尝试运行我的简单“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";
    }
}

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的

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

我做错了什么?我尝试改变我的项目结构,玩依赖项,没有任何帮助。有人可以启发我吗?

2 个答案:

答案 0 :(得分:0)

你的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 :(得分:-1)

根据您的上述评论,您访问了错误的网址。由于您已将控制器映射到/并且您只有1方法,因此您应该像以下一样访问它:

http://localhost:8080/