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";
    }
}
相关推荐
hx8622714 分钟前
Java MySQL 连接
java·mysql·adb
lpfasd12316 分钟前
Kubernetes (K8s) 底层早已不再直接使用 Docker 引擎了
java·docker·kubernetes
aq553560019 分钟前
SpringBoot有几种获取Request对象的方法
java·spring boot·后端
steel80881 小时前
Spring Boot 整合 log4j2 日志配置教程
spring boot·单元测试·log4j
lierenvip1 小时前
Spring Boot中Tomcat配置
spring boot·tomcat·firefox
Detachym1 小时前
InsightFlow 服务配置优化与部署实践
java·spring boot·tomcat·maven·状态模式·jar
rainchestnut1 小时前
Spring AI 初步集成(1)-初始化
spring boot
发现一只大呆瓜1 小时前
Vue-Vue Router核心原理+实战用法全解析
前端·vue.js·面试
y = xⁿ1 小时前
【LeetCodehot100】T23:合并k个升序链表
java·数据结构·链表
流水武qin1 小时前
SpringAI多模态的基本使用
java·spring boot·spring·ai