2017-03-17 89 views
0

我尝试运行我简单的“hello world”Spring Boot web应用程序,但它不工作,我总是得到“白标签错误页面”。简单的春季启动webapp不工作,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> 

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

+0

其中是您的html模板位于您的项目结构?此外,你访问什么网址给你的错误页面? – Gregg

+0

你的主类在哪里=>应用程序?主要的spring引导类只扫描并行类和内部包注释类(默认行为),而不描述路径,以及您的html模板应放置在资源文件夹中的位置。也请发布你的application.properties文件 –

+0

@Gregg html在'src/main/resources/templates /'中,我尝试'localhost:8080/basket' – crooked

回答

0

你的家()方法是最有可能不被弹簧注册,因为没有@方法之上的RequestMapping()。

如果您想要将家注册到/,您有两种选择。您可以将当前处于类级别的家用方法,像这样@RequestMapping ...

@Controller 
public class HomeController { 
    @RequestMapping("/") 
    public String home(Model model){ 
     String welcome = new String("Witam"); 
     model.addAttribute("welcome", welcome); 
     return "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点的方法,你应该访问它想:

​​

+0

试过。没有工作:/ – crooked

+0

需要知道你的白标签错误是什么。 – Gregg

+0

'有一个意外的错误(type = Not Found,status = 404).' – crooked