[英]Spring Boot controller is not returning html page
我在 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 src/main/resources/templates
,我将index.html
放在模板中,没有工作。
当我访问http://localhost:8080/index
它显示的是字符串index
而不是index.html
页面。 像这样,
我已经在 SO 中解决了几乎所有类似的问题,尝试了所有这些,但在这种情况下没有用。 一些线索会很有帮助。 谢谢你。
从您的控制器中删除响应正文,然后再试一次。
设置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 因为只有弹簧知道默认绑定的这个位置
我遇到了同样的问题,但我的问题是 JDK9。 当我尝试使用 JDK1.8 时,一切都变得很好。
解决这个问题需要三步
1. 删除@RequestBody 注释,因为我们正在返回一个 HTML 文件。
2.从maven仓库中添加与你使用的tomcat服务器对应的依赖。
https://mvnrepository.com/artifact/org.apache.tomcat/tomcat-jasper
3. 关闭所有 html 标签。 Thymleaf 期望 html 文件是有效的 xml 文件。
我有同样的问题。 无法仅通过返回带有 html 文件名称的字符串来返回 html。 对我来说,它只适用于 ModelAndView:
@RequestMapping("/")
public ModelAndView index () {
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("index");
return modelAndView;
}
有人说出现这样的问题是因为jdk版本。 我使用 jdk14。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.