gpt4 book ai didi

java - spring boot 返回字符串而不是 .html 文件

转载 作者:太空宇宙 更新时间:2023-11-04 13:45:11 30 4
gpt4 key购买 nike

我尝试运行 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</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>

应用程序属性:

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

spring.mvc.view.prefix=/static
spring.mvc.view.suffix=.html

家庭 Controller .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

最佳答案

@Controller VS @RestController

  • @Controller 用于将类标记为 Spring MVC Controller。
  • @RestController 是一个方便的注解,它只是添加了 @Controller@ResponseBody 注解。

因此,在您的情况下,只需从 HomeController.java 中的 welcome() 方法中删除 @ResponseBody 注释就足以获得所需的输出。

另请参阅本 Spring 指南,其中显示了如何 Serve Web Content with Spring MVC

关于java - spring boot 返回字符串而不是 .html 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53694914/

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