[英]Spring Boot shows the String instead of the HTML file from an Angular2 Single Page app
[英]spring boot return string instead of .html file
我尝试运行 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
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
@Controller
VS @RestController
@Controller
用于将类标记为 Spring MVC 控制器。@RestController
是一个方便的注解,只不过是添加了@Controller
和@ResponseBody
注解。 因此,在您的情况下,只需从 HomeController.java 中的welcome()
方法中删除@ResponseBody
注释,就足以获得所需的输出。
也看看这个 Spring Guide 显示了如何使用 Spring MVC 提供 Web 内容
默认情况下,Spring Boot 查找templates
文件夹中的 html 模板static
文件夹用于其他文件,如css and js
尝试将 html 文件移动到src/main/resources/templates
文件夹中,并从控制器方法中删除@ResponseBody
并将其从您的应用程序属性spring.mvc.view.prefix=/static
。 我希望它会起作用。
当您使用注解@ResponseBody
,实际上是在告诉 spring 不要尝试使用返回的名称查找视图。 如果你想要 html,只需从控制器方法中删除注释。
我有同样的问题。 无法仅通过返回带有 html 文件名称的字符串来返回 html。 对我来说,它只能使用 ModelAndView,在你的情况下它看起来像这样:
@RequestMapping("/")
public ModelAndView welcome() {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("bakara");
return modelAndView;
}
有人说出现这样的问题是因为jdk版本。 我使用 jdk14。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.