【问题标题】:Spring Boot controller is not returning html pageSpring Boot 控制器不返回 html 页面
【发布时间】:2017-07-14 21:22:49
【问题描述】:

我在 Spring Boot 中遇到了一些控制器问题。它应该返回一个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 的控制器

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

当我要去http://localhost:8080/index 时,它会显示字符串index 而不是index.html 页面。像这样,

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

【问题讨论】:

  • 您的视图解析器配置是什么?
  • "@ResponseBody public String createLoginForm(){ return "index"; }" 除了那个,它不能返回任何东西
  • 你的 index.html 中有什么?也许一个词:“索引”? :-)
  • 另外,为什么您的资源文件夹中没有 application.properties 文件?你删除了吗?它是有效的 Spring Boot 应用程序吗?你是如何创建它的?

标签: java spring maven spring-mvc spring-boot


【解决方案1】:

请从您的控制器中删除响应正文,然后重试。

【讨论】:

  • 我也试过了,但是它抛出了一个异常,说输入字符串“index”的“NumberFormatException”。
【解决方案2】:

在header中设置内容类型,删除@ResponseBody注解,将“index”改为“index.html”

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

静态资源(模板中的文件)的类路径也必须位于以下位置:/static 或 /public 或 /resources 或 /META-INF/resources 因为只有这个位置 spring 知道默认绑定

【讨论】:

  • 无需添加.html。它是逻辑视图,必须由视图解析器解析。
  • 没有.html会返回字符串...使用Spring MVC可能不需要添加html后缀
  • 感谢您的回答,但我没有得到静态资源在下一个位置的信息。据我所知,它默认位于 src/main/resources/templates 中。有点困惑,:-)
【解决方案3】:

我遇到了同样的问题,但我的问题是 JDK9。当我尝试使用JDK1.8时,一切都变得很好。

【讨论】:

    【解决方案4】:

    解决这个问题的三个步骤
    1. 当我们返回一个 HTML 文件时,移除 @RequestBody 注释。
    2.添加一个maven仓库的依赖,对应你正在使用的tomcat服务器。
    https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper
    3.关闭所有html标签。 Thymleaf 期望 html 文件是有效的 xml 文件。

    【讨论】:

      【解决方案5】:

      我遇到了同样的问题。仅通过返回带有 html 文件名称的 String 是无法返回 html 的。对我来说,它只能使用 ModelAndView:

      @RequestMapping("/")
      public ModelAndView index () {
          ModelAndView modelAndView = new ModelAndView();
          modelAndView.setViewName("index");
          return modelAndView;
      }
      

      有人说出现这样的问题是因为jdk版本。我用jdk14。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2016-12-06
        • 2019-05-07
        • 2021-05-25
        • 1970-01-01
        • 2018-07-14
        • 2020-11-13
        • 2016-12-31
        • 1970-01-01
        相关资源
        最近更新 更多