gpt4 book ai didi

java - Spring Boot Controller 不返回 html 页面

转载 作者:行者123 更新时间:2023-12-01 20:19:38 26 4
gpt4 key购买 nike

我在 Spring Boot 中遇到了一些 Controller 问题。它应该返回一个 index.html 文件,但它所做的是返回字符串“index”。

这是您想要寻找的东西,

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的 Controller

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 是主类。到目前为止,我知道 Controller 检查 src/main/resources/templates 中的 html 模板,我已将 模板中的index.html不起作用。

当我访问http://localhost:8080/index时,它向我显示字符串index而不是index.html > 页面。像这样,

enter image description here

我已经在SO中经历了几乎所有类似的问题,尝试了所有这些,但在这种情况下不起作用。一些线索会有很大帮助。谢谢。

最佳答案

从 Controller 中删除响应正文,然后重试。

关于java - Spring Boot Controller 不返回 html 页面,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45111794/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com