2016-11-24 116 views
0

私はとthymeleafビューエンジンを使用しています。なぜhtmlページがthymeleafに表示されないのですか?

問題はコントローラが適切なhtmlページを表示せず、常にWhitelabel Error Pageを表示していることです。

コントローラ:

package com.example.controller; 

import org.springframework.stereotype.Controller; 
import org.springframework.web.bind.annotation.RequestMapping; 


@Controller 
public class GifController { 

    @RequestMapping("/greeting") 
    public String sayhello() { 
     return "greets"; 
    } 
} 

資源でgreets.htmlファイル/テンプレートのパス:また

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="UTF-8"/> 
<title>Insert title here</title> 
</head> 
<body> 
Hello! 
</body> 
</html> 

@EnableAutoConfiguration 
@SpringBootApplication 
@ComponentScan 
public class DemoApplication { 

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

とのpom.xml:

<?xml version="1.0" encoding="UTF-8"?> 
<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.example</groupId> 
    <artifactId>demo</artifactId> 
    <version>0.0.1-SNAPSHOT</version> 
    <packaging>jar</packaging> 

    <name>demo</name> 
    <description>Demo project for Spring Boot</description> 

    <parent> 
     <groupId>org.springframework.boot</groupId> 
     <artifactId>spring-boot-starter-parent</artifactId> 
     <version>1.4.2.RELEASE</version> 
     <relativePath/> <!-- lookup parent from repository --> 
    </parent> 

    <properties> 
     <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding> 
     <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding> 
     <java.version>1.8</java.version> 
    </properties> 

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

     <dependency> 
      <groupId>org.springframework.boot</groupId> 
      <artifactId>spring-boot-starter-test</artifactId> 
      <scope>test</scope> 
     </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> 

どこに問題がありますか? localhost:8080/greetingを参照すると、greets.htmlの代わりにwhitelabel Error pageが表示されます。

答えて

0

コントローラとSpringBootアプリケーションはokです。あなたのプロジェクトをきれいにしてビルドしてみてください。あなたがDemoApplicationを実行するときは、コンソール

Mapped "{[/greeting]}" onto public java.lang.String com.example.controller.GifController.sayhello() 

に下記のように表示されるはずですそして、あなたのDemoApplicationであなただけのこの注釈が@Configurationを使用するのと同じであるよう@SpringBootApplication、@EnableAutoConfigurationと@ComponentScanが必要

+0

すべてがOKですが、それは動作していません... –

+0

私はあなたのコードをテストし、それは私のために働いています。テンプレートフォルダにindex.htmlを追加し、@ ReQuestMapping( "/")のようなGifControllerでもう1つのメソッドを追加してみてください。 public String hello(){ return "index"; }。きれいにして、プロジェクトをパッケージ化してテストしてください。 – abaghel

1

私はちょうど春のブート学習者で、問題を解決して学習しようとしています。

私は私が/src/main/webapp/WEB-INF/templates/greet.htmlでgreet.htmlページを入れて、私はConfigクラスを作成し、また、それに

@Configuration 
public class Config { 
    @Bean 
    public ServletContextTemplateResolver templateResolver() { 
     ServletContextTemplateResolver resolver = new ServletContextTemplateResolver(); 
     resolver.setPrefix("/WEB-INF/templates/"); 
     resolver.setSuffix(".html"); 
     resolver.setTemplateMode("HTML5"); 
     resolver.setOrder(1); 
     return resolver; 
    } 
} 



@SpringBootApplication 
@ComponentScan 
public class DemoApplication { 

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

} 

を、いくつかの設定を入れて、いくつかの変更を加えました は、私はあなたがあなた自身の場所を使用することができ、その場所にHTML/JSPページを入れたいだけConfig.javaクラスでその場所を交換 @Controller パブリッククラスGifController {

@RequestMapping("/greeting") 
    public String sayhello() { 
     return "greets"; 
    } 
} 

は、これらの変更を試してみて、あなたがブラウザ上で出力を見ることができるかどうかを見るよりも。

関連する問題