vue3项目打包后整合到springboot项目中运行

这篇具有很好参考价值的文章主要介绍了vue3项目打包后整合到springboot项目中运行。希望对大家有所帮助。如果存在错误或未考虑完全的地方,请大家不吝赐教,您也可以点击"举报违法"按钮提交疑问。

概述

一般来说,前后端分离项目,比如vue3+springboot的前后端分离项目,一般把vue3项目打包后部署到nginx或者tomcat上面,springboot项目单独打包。

那如果想把vue3项目打包后直接部署到springboot项目中,如何做那?

VUE3项目

创建vue项目

创建项目

npm init vue@latest

安装依赖

cd 项目名

npm install

启动开发服务器(项目目录)

npm run dev

如果要部署到springboot项目中,打包前要修改vite.config.js文件,如下

添加  base:'./'

import { fileURLToPath, URL } from 'node:url'

import { defineConfig } from 'vite'
import vue from '@vitejs/plugin-vue'

// https://vitejs.dev/config/
export default defineConfig({
  plugins: [vue()],
  resolve: {
    alias: {
      '@': fileURLToPath(new URL('./src', import.meta.url))
    }
  },
  base:'./'
})

打包vue项目

npm run build

springboot项目

我采用手动搭建springboot项目的方式

创建maven项目

修改pom.xml文件

需要添加的依赖

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

全部代码

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.7.8</version>
        <relativePath/>
    </parent>

    <groupId>org.example</groupId>
    <artifactId>springboot-demo1</artifactId>
    <version>1.0-SNAPSHOT</version>

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


        <!--lombok-->
        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <optional>true</optional>
        </dependency>
        <!--springMVC-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <!--springBoot起步依赖-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter</artifactId>
        </dependency>
        <!--test-->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>



</project>

修改application.yml文件

server:
  port: 8080

spring:
  # 打成jar包必须添加如下配置才能找到页面
  thymeleaf:
    mode: HTML
    cache: false
    prefix: classpath:/templates

编写启动类

package jkw;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class Main {
    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }
}

编写跳转控制器

package jkw.controller;

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

/**
 * 页面跳转控制器
 */
@Controller
public class PageController {
    //后台页面
    @RequestMapping("/back/{backPage}")
    public String backPage(@PathVariable String backPage) {
        return "/back/" + backPage;
    }

    //前台页面
    @RequestMapping("/front/{frontPage}")
    public String frontPage(@PathVariable String frontPage) {
        return "/front/" + frontPage;
    }
    
}

整合

springboot项目

resources下面创建static和templates两个文件夹,分别存放静态文件和动态页面

连个文件夹中都创建back和front文件,一个是前台,一个是后台

vue项目

把打包好的vue项目中的dist文件中assets和favicon.ico放在static中back

index.html放在templates中back

修改index.html中引入资源的文置,如下

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8">
    <link rel="icon" href="/back/favicon.ico">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Vite App</title>
    <script type="module" crossorigin src="/back/assets/index-1973819a.js"></script>
    <link rel="stylesheet" href="/back/assets/index-4afc3c58.css">
  </head>
  <body>
    <div id="app"></div>
    
  </body>
</html>

启动springboot项目,访问

localhost:8081/back/index文章来源地址https://www.toymoban.com/news/detail-754305.html

到了这里,关于vue3项目打包后整合到springboot项目中运行的文章就介绍完了。如果您还想了解更多内容,请在右上角搜索TOY模板网以前的文章或继续浏览下面的相关文章,希望大家以后多多支持TOY模板网!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处: 如若内容造成侵权/违法违规/事实不符,请点击违法举报进行投诉反馈,一经查实,立即删除!

领支付宝红包 赞助服务器费用

相关文章

  • SpringBoot学习——项目用maven打包成jar包 & windows + Linux平台运行 & Linux安装java & 遇到的问题

    1.maven打包springboot项目,jar包; 2.windows安装java环境,以及运行jar包; 3.Linux安装java环境,以及运行jar包; 4.运行jar包template might not exist报错及解决; Maven 构建生命周期定义了一个项目构建跟发布的过程。 一个典型的 Maven 构建(build)生命周期是由以下几个阶段的序列组成的

    2024年02月16日
    浏览(53)
  • Vue3 - 解决 build / dev 打包运行时突然出现一堆 ts 相关的报错,强制关闭整个项目的 ts 代码语法校验和验证(webpack / vite 均可)

    无论您是 vite 还是 webpack,都可以 100% 去掉运行或打包时的 ts 语法验证。 本文 实现了在 vue3 + ts 开发中,关闭运行、打包部署命令时出现的各种 ts 校验报错,去掉对 ts 的验证, 有些朋友对 ts 不是很了解(所以在写代码时没注意一些语法),导致最终 build 打包时出现了很多

    2024年02月11日
    浏览(39)
  • 04 SpringBoot整合Druid/MyBatis/事务/AOP+打包项目

    项目结构: 引入依赖: 启动类: 实体类: Controller类: Druid配置文件application.yaml : 启动项目后进入8080/user/getUser即可获得返回的json. 先看看总体的步骤: 导入依赖:在pom.xml中添加MyBatis和数据库驱动的相关依赖。 配置数据源: application.yml 中配置 数据库连接信息,包括数据

    2024年01月21日
    浏览(31)
  • 一键打包,随时运行,Python3项目虚拟环境一键整合包的制作(Venv)

    之前我们介绍了如何使用嵌入式 Python3 环境给项目制作一键整合包,在使用嵌入式 Python 环境时,通常是作为另一个应用程序的一部分,而Python3虚拟环境是为了在开发过程中隔离项目所需的 Python 环境。虚拟环境允许我们在同一台计算机上的不同项目中使用不同的 Python 版本和

    2024年02月04日
    浏览(39)
  • vue前端打包Docker镜像并nginx运行

    首先说明咱们的前端项目是基于Vue的,反向代理使用的是nginx 1.打包vue前端项目生成dist文件夹上传至服务器 新建一个文件夹,叫vueDockerTest,下面的文件都需要。 cert是你存放ssl证书的文件夹,nginx.conf 是nginx的配置文件,dist是你打包的前端静态文件 2.修改Dockerfile文件 # 基于

    2024年02月01日
    浏览(43)
  • 前端自测运行vue打包后的dist文件

    在Vue项目中,dist目录是代码打包之后生成的文件夹,其中包含了静态资源文件和打包后的JavaScript、CSS等文件。如果要在本地运行打包后的项目文件,可以使用简单的静态服务器来启动。 下面介绍一种使用Node.js中的http-server模块搭建本地服务器的方法: 先查看是否安装node 如

    2024年02月06日
    浏览(33)
  • vue3项目打包和上线

    在vite.config.ts文件中添加esbuild:{drop:[\\\"console\\\",\\\"debugger\\\"]} 在package.json文件中scripts对象中 \\\"build\\\": \\\"run-p type-check build-only\\\"改为\\\"build\\\": \\\"run-p build-only\\\" (1)echarts首次渲染没问题,第二次渲染却成空白 产生原因:echarts插件自带的bug 解决方法 (2)控制台输出404 产生原因:未找到图标文

    2024年02月03日
    浏览(31)
  • vue3 整合 springboot 打完整jar包

    .env.developmen .env.production axios 配置 package.json vite.config.js pom.xml

    2024年02月09日
    浏览(26)
  • tauri+vite+vue3开发环境下创建、启动运行和打包发布

    目录  1.创建项目  2.安装依赖   3.启动项目  4.打包生成windows安装包   5.安装打包生成的安装包  运行下面命令创建一个tauri项目 我创建该项目时的node版本为16.15.0  兼容性注意 Vite 需要 Node.js 版本 14.18+,16+。然而,有些模板需要依赖更高的 Node 版本才能正常运行,当你

    2024年01月19日
    浏览(36)
  • vue3 项目打包后白屏

    根据Vue3.x文档,在 vue.config.js/vite.config.ts 统一对webpack、跨域、端口号等属性进行配置。 1.在 vue.config.js/vite.config.ts添加publicPath属性并将值更改成 ‘./’ 在这里插入图片描述 2.若还没有解决就去路由中将history模式设置成默认的Hash模式, 一般是服务器后端配置之后才使用hist

    2024年02月16日
    浏览(35)

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

博客赞助

微信扫一扫打赏

请作者喝杯咖啡吧~博客赞助

支付宝扫一扫领取红包,优惠每天领

二维码1

领取红包

二维码2

领红包