springboot整合vue,将vue项目整合到springboot项目中

将vue项目打包后,与springboot项目整合。

第一步,使用springboot中的thymeleaf模板引擎

导入依赖

复制代码
        <!-- thymeleaf 模板 -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-thymeleaf</artifactId>
        </dependency>

在resources目录下建立static文件夹和templates文件夹

在yml中配置thymeleaf

复制代码
spring:
  # 模板引擎
  thymeleaf:
    mode: HTML5
    encoding: utf-8
    # 禁用缓存
    cache: false

在配置中打开访问静态文件的权限

复制代码
public class ResourceConfig implements WebMvcConfigurer {
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
    }
}

第二步:将vue项目打包后中的静态文件放入static文件夹中,将index.html放入templates文件夹中

第三步:在controller中写路由,让其跳转index.html页面

复制代码
@Controller
@CrossOrigin
public class IndexController {
    @GetMapping("/")
    public String index(){
        return "index";
    }
}
相关推荐
x-cmd13 分钟前
[x-cmd] 终端里的飞书:lark-cli,让 AI Agent 拥有“实体办公”能力
java·人工智能·ai·飞书·agent·x-cmd
吾日三省Java27 分钟前
SpringBoot锁设计:让你的系统不再“抢”出问题!
java·spring boot·设计思路
XXOOXRT33 分钟前
Ubuntu搭建Java项目运行环境(JDK17+MySQL8.0)超详细教程
java·linux·mysql·ubuntu
m0_7190841134 分钟前
启动命令111
java
客卿1231 小时前
用两个栈实现队列
android·java·开发语言
java1234_小锋1 小时前
Java高频面试题:谈谈你对SpringBoot的理解?
java·开发语言·spring boot
空空潍1 小时前
Spring AI 实战系列(三):多模型共存+双版本流式输出
java·人工智能·spring
彭于晏Yan1 小时前
SpringBoot整合ECC实现文件签名与验签
java·spring boot·后端
pupudawang1 小时前
Spring EL 表达式的简单介绍和使用
java·后端·spring
jiankeljx1 小时前
Spring Initializr创建springboot项目,提示java 错误 无效的源发行版:16
java·spring boot·spring