gpt4 book ai didi

java - 如何从 Spring Boot 提供静态 html?

转载 作者:行者123 更新时间:2023-12-03 09:42:27 24 4
gpt4 key购买 nike

我从 here 运行了 spring-boot-sample-web-static 项目, 对 pom 做了这个改动

<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-tomcat</artifactId>
</dependency>

并添加了这个类来提供来自同一个 static 的重复页面 index2.html文件夹位置:

import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class Rester {

@RequestMapping(value = "/rand", produces = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
private RandomObj jsonEndpoint() {
return new RandomObj();
}

@RequestMapping(value = "/tw")
public String somePg() {
return "index2";
}
}

json url 工作正常,但是当我尝试访问 localhost:8080/tw 时,我得到一个空白页面,并且控制台中出现此错误:
2017-02-22 15:37:22.076 ERROR 21494 --- [nio-8080-exec-9] o.s.boot.web.support.ErrorPageFilter     : Cannot forward to error page for request [/tw] as the response has already been committed. As a result, the response may have the wrong status code. If your application is running on WebSphere Application Server you may be able to resolve this problem by setting com.ibm.ws.webcontainer.invokeFlushAfterService to false

难道我做错了什么?

最佳答案

静态文件应该由资源提供,而不是来自 Controller 。

Spring Boot will automatically add static web resources located within any of the following directories:

/META-INF/resources/  
/resources/
/static/
/public/


引用:
https://spring.io/blog/2013/12/19/serving-static-web-content-with-spring-boot
https://spring.io/guides/gs/serving-web-content/

关于java - 如何从 Spring Boot 提供静态 html?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42393211/

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