我在春季開機時遇到了一些問題。哪一個應該返回一個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);
}
}
文件結構,
Earworm
是主要class.So據我所知在src/main/resources/templates
的HTML模板控制器檢查,我已經把模板index.html
,沒有工作。
當我要http://localhost:8080/index
它顯示字符串index
而不是index.html
頁面。與此類似,
我已經經歷了幾乎所有的類似在這裏做題了,嘗試了所有,但這些在這種情況下,沒有工作。一些線索將會有很大的幫助。謝謝。
什麼是您的viewresolver配置? – yogidilip
「@ResponseBody public String createLoginForm(){ return」index「; }」它不能返回其他任何東西,但是 – efekctive
什麼在您的index.html中?也許一個字:「索引」? :-) –