2017-07-14 35 views
0

我在春季開機時遇到了一些問題。哪一個應該返回一個index.html文件,但它做的是返回字符串「index」。春季開機控制器沒有返回html頁面

這裏有您想要尋找的東西,

的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.earworm</groupId> 
    <artifactId>earworm</artifactId> 
    <version>1.0-SNAPSHOT</version> 

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

    <properties> 
     <java.version>1.8</java.version> 
    </properties> 

    <build> 
     <plugins> 
      <plugin> 
       <groupId>org.springframework.boot</groupId> 
       <artifactId>spring-boot-maven-plugin</artifactId> 
      </plugin> 
     </plugins> 
    </build> 
</project> 

命名LoginController.java

package com.earwormproject.earwormcontrollers; 

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

@Controller 
public class LoginController { 

    @RequestMapping(value = "/index", method = RequestMethod.GET) 
    @ResponseBody 
    public String createLoginForm(){ 
     return "index"; 
    } 

    @RequestMapping(value = "/profile", method = RequestMethod.GET) 
    @ResponseBody 
    public String addDataToLoginForm() { 
     return "profile"; 
    } 
} 

這裏是主類,

控制器
package com.earwormproject; 

import org.springframework.boot.SpringApplication; 
import org.springframework.boot.autoconfigure.SpringBootApplication; 

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

文件結構,

enter image description here

Earworm是主要class.So據我所知在src/main/resources/templates的HTML模板控制器檢查,我已經把模板index.html,沒有工作。

當我要http://localhost:8080/index它顯示字符串index而不是index.html頁面。與此類似,

enter image description here

我已經經歷了幾乎所有的類似在這裏做題了,嘗試了所有,但這些在這種情況下,沒有工作。一些線索將會有很大的幫助。謝謝。

+0

什麼是您的viewresolver配置? – yogidilip

+2

「@ResponseBody public String createLoginForm(){ return」index「; }」它不能返回其他任何東西,但是 – efekctive

+0

什麼在您的index.html中?也許一個字:「索引」? :-) –

回答

1

刪除您的控制器的響應身體,請再試一次。

+0

試過了,它沒有工作。感謝您的回答。 –

+0

我也試過了,但是它會拋出一個異常,說輸入字符串「index」的「NumberFormatException」。 – KingKari

1

在標題中設置內容類型,刪除註釋和@ResponseBody改變「指數」,以「index.html的」

@RequestMapping(value = "/index", method = RequestMethod.GET) 
public String createLoginForm(HttpServletResponse response){ 
    response.setHeader("Content-Type","text/html"); 
    return "index"; 
} 

而且靜態資源的類路徑(將文件從模板)必須在下一個位置:/靜態或/公共或/資源或/ META-INF /資源 ,因爲只有這個位置春天知道默認綁定

+0

沒有必要添加.html。這是邏輯視圖,必須由視圖解析器解決。 – fg78nc

+0

沒有.html將返回字符串...與Spring MVC也許有沒有必要添加HTML後綴 –

+0

感謝您的答案,但我沒有得到靜態資源在下一個位置的東西。據我所知,它默認在src/main/resources/templates中查找。困惑了一下,:-) –

0

我遇到了同樣的問題,但我的問題是JDK9。當我嘗試使用JDK1.8時,一切都變得很好。