Spring Boot返回字符串而不是.html文件

我尝试运行spring boot应用程序,它将返回我静态文件夹中的HTML静态文件,问题是:每次加载页面:127.0.0.1我得到的是字符串“

bakara”,而不是HTML文件bakara.html 。当我加载127.0.0.1/bakara.html时,我得到了bakara.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>il.mda.ks</groupId>

<artifactId>mdaForm</artifactId>

<version>0.0.1-SNAPSHOT</version>

<packaging>war</packaging>

<name>mdaForm</name>

<description>Demo project for Spring Boot" title="Spring Boot">Spring Boot</description>

<parent>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-parent</artifactId>

<version>2.1.1.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>

<!-- This is a web application -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-web</artifactId>

</dependency>

<!-- Tomcat embedded container -->

<dependency>

<groupId>org.springframework.boot</groupId>

<artifactId>spring-boot-starter-tomcat</artifactId>

<scope>provided</scope>

</dependency>

<!-- JSTL for JSP -->

<dependency>

<groupId>javax.servlet</groupId>

<artifactId>jstl</artifactId>

</dependency>

<!-- Need this to compile JSP -->

<dependency>

<groupId>org.apache.tomcat.embed</groupId>

<artifactId>tomcat-embed-jasper</artifactId>

<scope>provided</scope>

</dependency>

<!-- Need this to compile JSP, tomcat-embed-jasper version is not working,

no idea why -->

<dependency>

<groupId>org.eclipse.jdt.core.compiler</groupId>

<artifactId>ecj</artifactId>

<version>4.6.1</version>

<scope>provided</scope>

</dependency>

<!-- Optional, test for static content, bootstrap CSS -->

<dependency>

<groupId>org.webjars</groupId>

<artifactId>bootstrap</artifactId>

<version>3.3.7</version>

</dependency>

</dependencies>

application.properties:

#spring.mvc.view.prefix=/static/

#spring.mvc.view.suffix=.html

spring.mvc.view.prefix=/static

spring.mvc.view.suffix=.html

HomeController.java:

import org.springframework.stereotype.Controller;

import org.springframework.web.bind.annotation.*;

@Controller

public class HomeController {

@RequestMapping("/")

@ResponseBody

public String welcome() {

return "bakara";

}

}

项目结构:

|── src

│   ├── main

│   │   ├── java

│   │   │   └── il

│   │   │   └── mda

│   │   │   └── ks

│   │   │   └── mdaForm

│   │   │   ├── BakaraController.java

│   │   │   ├── HomeController.java

│   │   │   └── MdaFormApplication.java

│   │   └── resources

│   │   ├── application.properties

│   │   ├── static

│   │   │   ├── assets

│   │   │   ├── bakara.html

│   │   │   ├── succeed.html

│   │   │   └── TokenDenied.html

│   │   └── templates

回答:

默认情况下,Spring Boot Looks对于templates文件夹staticfolder中的html模板适用于其他文件,例如。css

and js尝试将文件src/main/resources/templates夹中的html文件移动并@ResponseBody

从控制器方法中删除,然后从应用程序属性中删除spring.mvc.view.prefix=/static。我希望它能起作用。

以上是 Spring Boot返回字符串而不是.html文件 的全部内容, 来源链接: utcz.com/qa/420828.html

回到顶部