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";
    }
}
相关推荐
v***8574 分钟前
Spring Boot 集成 MyBatis 全面讲解
spring boot·后端·mybatis
幻灭行度5 分钟前
docker镜像导入到K8S的containerd中
java·docker·kubernetes
武昌库里写JAVA8 分钟前
Java如何快速入门?Java基础_Java入门
java·vue.js·spring boot·后端·sql
Irene199110 分钟前
Element UI 及其 Vue 3 版本 Element Plus 发展现状
前端·vue.js·ui
Account_Ray11 分钟前
vue3 的专属二维码组件 vue3-next-qrcode 迎来 4.0.0 版本
前端·vue.js·nuxt.js
h***346312 分钟前
SpringBoot(整合MyBatis + MyBatis-Plus + MyBatisX插件使用)
spring boot·tomcat·mybatis
那我掉的头发算什么17 分钟前
【javaEE】多线程进阶--CAS与原子类
android·java·jvm·java-ee·intellij-idea
SteveCode36 分钟前
血赚不亏!Java 17 9 个炸裂特性,程序员看完直呼:太香了!
java
帧栈41 分钟前
开发避坑指南(70):Vue3 Http请求头携带token下载pdf文件解决方案
前端·vue.js
BLOB_1010011 小时前
关于懒人复制idea项目的坑
java·ide·intellij-idea