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";
    }
}
相关推荐
WL_Aurora3 分钟前
Java基础知识超详细总结(从入门到精通)
java
咖啡八杯6 分钟前
GoF设计模式——抽象工厂模式
java·后端·spring·设计模式·抽象工厂模式
Thanks_ks9 分钟前
分布式锁:Redis 与 Redisson 的工程实践与避坑指南
java·redis·分布式锁·redisson·微服务架构·并发编程·高可用
掉鱼的猫15 分钟前
agentscope-harness vs solon-ai-harness:Java 智能体「马具引擎」的双雄对决
java·openai
RainCity21 分钟前
Java Swing 自定义组件库分享(四)
java·笔记·后端
带刺的坐椅33 分钟前
agentscope-harness vs solon-ai-harness:Java 智能体「马具引擎」的双雄对决
java·ai·llm·solon·agentscope·harness
Seven9736 分钟前
Paxos算法:如何解决分布式系统中的共识问题?
java
Lan.W1 小时前
vue3-element-admin里新增mock接口一直没有生成,不生效
前端·javascript·vue.js·mock
仙古.梦回~1 小时前
vue-skills
前端·javascript·vue.js
铁皮哥1 小时前
【力扣题解】LeetCode 25. K 个一组翻转链表
java·数据结构·windows·python·算法·leetcode·链表