SPRINGBOOT-5:前端嵌入

由于对前端开发并不精通,

因此计划使用一个静态的调用关系来调用HTML文件

 

1. Controller

package com.ais.devops;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;


@Controller
public class MainController {

    @RequestMapping("/index")
    public String root() {
        return "index";
    }

    @GetMapping("/index2")
    public  String root2() {
        return "index";
    }

}

说明:RequestMapping和GetMapping两种方案均未发现异常。可以正确读取静态资源

说明:class类注解不能使用RestController注解

2.配置thymeleaf

2.1 POM中增加dependency

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

2.2 在application.properties中增加配置项


spring.thymeleaf.mode=HTML5
spring.thymeleaf.encoding=UTF-8
spring.thymeleaf.content-type=text/html
##开发时关闭缓存,不然没法看到实时页面
spring.thymeleaf.cache=false
spring.mvc.view.prefix=classpath:/templates/
spring.mvc.view.suffix=.html

2.3 增加纯静态index.html文件

<!DOCTYPE html> 
<html> 
<head> 
<meta charset="UTF-8"/> 
<title>Insert title here</title> 
</head> 
<body> 
 <h1>springboot访问第一个html页面</h1> 
</body> 
</html>

说明:

1.实际index.html放置的路径是bootstrap-start/src/main/resources/templates/index.html

2.template不行,必须使用templates文件夹

3.有一种说法是静态页面应当把prefix指向bootstrap-start/src/main/resources/static/pages/index.html,实测无法调用

4.meta以及其他参数最后都要增加 / 否则thymeleaf无法正确解析

5.代码中不能使用&&和& (原因不明)

即可调用该index.html

参考

https://blog.csdn.net/wangmx1993328/article/details/81054474

https://www.cnblogs.com/ityouknow/p/5833560.html

https://segmentfault.com/a/1190000011149325

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值