# Springboot 返回 html

注:Springboot 的版本 2.1.3.RELEASE

List-1 application.properties 文件

1
2
3
4
5
6
7
server.port=8080
#url中,项目的前缀
server.servlet.context-path=/project


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

整体结构如下图 1 所示,html 要放在 static 下,不是 templates 下

img

图1

List-2 HelloController 的内容如下

1
2
3
4
5
6
7
8
9
10
11
12
13
14
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

@Slf4j
@Controller
public class HelloController {

@RequestMapping(value = "/hello")
public String index() {
log.info("收到请求");
return "html/hello";
}
}

List-3 启动 springboot,之后在浏览器中输入

1
2
3
4
5
#返回index.html的内容
http://localhost:8080/project/

#返回hello.html的内容
http://localhost:8080/project/hello

网上很多关于模板的(Thymeleaf 、FreeMarker 等),但是我不需要,我只需要纯的 html。

index.html 是 springboot 的默认 welcome page。

# Reference

  1. https://www.jianshu.com/p/eb4c0fc2dfc4
  2. https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-spring-mvc-static-content
  3. https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#boot-features-spring-mvc-welcome-page

​ (adsbygoogle = window.adsbygoogle || []).push({});

更新于 阅读次数

请我喝[茶]~( ̄▽ ̄)~*

有李说不清 微信支付

微信支付

有李说不清 支付宝

支付宝

有李说不清 贝宝

贝宝