Vue-cli3与springboot项目整合打包
一、需求
二、Vue项目具体配置
NODE_ENV = 'production'
VUE_APP_BASE_URL= /app_name/v1
VUE_APP_CONTEXT=/pre-lcas
VUE_APP_ASSETS=static
- VUE_APP_BASE_URL
- VUE_APP_CONTEXT和VUE_APP_ASSETS
module.exports = {
// 设置路径
publicPath: process.env.VUE_APP_CONTEXT,
assetsDir: process.env.VUE_APP_ASSETS,
......
}
dist
--static
----css
----fonts
----img
----js
--favicon.ico
--index.html
/src
--main
--|--java
--|--|--com.xx.xx.WebApplication
--|--resource
--|--|--static
--|--|--|--css
--|--|--|--fonts
--|--|--|--img
--|--|--|--js
--|--|--|--favicon.ico
--|--|--|--index.html
--|--|--application.properties
三、springboot项目配置
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
WebMvcConfigurer.super.addResourceHandlers(registry);
}
/**
* 路由过滤,如果路径中包含‘home’关键词(前端所有路由都包含‘home’)
* @return
*/
@Bean
public FilterRegistrationBean filterRegistration() {
FilterRegistrationBean<RewriteFilter> registration = new FilterRegistrationBean<>();
//注册rewrite过滤器
registration.setFilter(new RewriteFilter());
registration.addUrlPatterns("/*");
registration.addInitParameter(RewriteFilter.REWRITE_TO,"/index.html");
registration.addInitParameter(RewriteFilter.REWRITE_PATTERNS, "/home/*");
registration.setName("rewriteFilter");
registration.setOrder(1);
return registration;
}
}
RewriteFilter.java代码
import org.springframework.util.StringUtils; import javax.servlet.*;
import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashSet;
import java.util.Set;
import java.util.regex.Pattern; /**
* 过滤后端请求,不属于后端的请求,交由前端路由处理
* @author: 2019/11/1 9:50
* @since: 0.0.1-SNAPSHOT
* @modified By:
*/
public class RewriteFilter implements Filter {
/**
* 需要rewrite到的目的地址
*/
public static final String REWRITE_TO = "rewriteUrl"; /**
* 拦截的url,url通配符之前用英文分号隔开
*/
public static final String REWRITE_PATTERNS = "urlPatterns"; /** 配置url通配符 */
private Set<String> urlPatterns = null; private String rewriteTo = null;
@Override
public void init(FilterConfig cfg) throws ServletException {
//初始化拦截配置
rewriteTo = cfg.getInitParameter(REWRITE_TO);
String exceptUrlString = cfg.getInitParameter(REWRITE_PATTERNS);
if (!StringUtils.isEmpty(exceptUrlString)) {
urlPatterns = Collections.unmodifiableSet(
new HashSet<>(Arrays.asList(exceptUrlString.split(";", 0))));
} else {
urlPatterns = Collections.emptySet();
}
} @Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain) throws IOException, ServletException {
HttpServletRequest request = (HttpServletRequest) req;
String servletPath = request.getServletPath();
//匹配到后端路径标识,放行,否则,交给前端路由
if (isMatches(urlPatterns, servletPath)) {
req.getRequestDispatcher(rewriteTo).forward(req, resp);
}else{
chain.doFilter(req, resp);
}
} @Override
public void destroy() { } /**
* 匹配返回true,不匹配返回false
* @param patterns 正则表达式或通配符
* @param url 请求的url
* @return
*/
private boolean isMatches(Set<String> patterns, String url) {
if(null == patterns){
return false;
}
for (String str : patterns) {
if (str.endsWith("/*")) {
String name = str.substring(0, str.length() - 2);
if (url.contains(name)) {
return true;
}
} else {
Pattern pattern = Pattern.compile(str);
if (pattern.matcher(url).matches()) {
return true;
}
}
}
return false;
}
}
RewriteFilter.java
四、说明
参考文章:
Vue-cli3与springboot项目整合打包的更多相关文章
- springboot项目,打包时携带所有依赖
springboot项目,打包时携带所有依赖 本文主要解决springboot打包时,如何设置才能把当前项目的所有依赖都打进去. Springboot 的自带spring-boot-maven-plu ...
- Flowable与springBoot项目整合及出现的问题
Flowable与springBoot项目整合及出现的问题 单纯地将Flowable和springBoot整合,使用mysql作为数据库,整合中踩了两个坑,见文末. 在pom中添加依赖 <?xm ...
- springboot项目整合druid数据库连接池
Druid连接池是阿里巴巴开源的数据库连接池项目,后来贡献给Apache开源: Druid的作用是负责分配.管理和释放数据库连接,它允许应用程序重复使用一个现有的数据库连接,而不是再重新建立一个: D ...
- Springboot项目与vue项目整合打包
我的环境 * JDK 1.8 * maven 3.6.0 * node环境 1.为什么需要前后端项目开发时分离,部署时合并? 在一些公司,部署实施人员的技术无法和互联网公司的运维团队相比,由于各种不定 ...
- vue和cordova项目整合打包,并实现vue调用android的相机的demo
经过网上查找很多资料,发现很多只有vue+cordova的项目整合,但是vue使用cordova插件的文章很少,现在把从创建cordova和创建vue到vue使用插件到项目打包到android手机运行 ...
- VUE CLI3.X 创建项目
Node.js环境搭建 Node.js基于V8引擎,可以让js代码脱离浏览器运行 Vue CLI3.0 需要Node.js 8.9或者更高版本. 用nvm或者nvm-windows在同一台电脑中管理多 ...
- vue cli3以上的项目中如何使用axois请求本地json文件
首先明确一点,在vue cli3以上的版本中,存放静态资源的文件是public 我刚开始以为是和vue cli2一样需要放在static文件夹下,但是项目中没有这个文件夹,我就自己创建了一个,结果请求 ...
- 使用maven构建项目时,SSM和springboot项目的打包与云服务器部署
下面讲讲如何打包SSM和springboot项目,并部署到云服务器上. 由于使用的IDE不同,有的使用eclipse,有的使用idea,所以如果在IDE中按照 maven clean 再 maven ...
- SpringBoot项目整合Retrofit最佳实践,这才是最优雅的HTTP客户端工具!
大家都知道okhttp是一款由square公司开源的java版本http客户端工具.实际上,square公司还开源了基于okhttp进一步封装的retrofit工具,用来支持通过接口的方式发起http ...
随机推荐
- Git 工作区、暂存区和版本库、操作流程
Git 工作区.暂存区和版本库 基本概念 我们先来理解下Git 工作区.暂存区和版本库概念 工作区:就是你在电脑里能看到的目录. 暂存区:英文叫stage, 或index.一般存放在 ".g ...
- lc 0222
目录 ✅ 191. 位1的个数 描述 解答 cpp py ✅ 107. 二叉树的层次遍历 II 描述 解答 c todo 观赏 0222 py ✅ 806. 写字符串需要的行数 描述 解答 cpp j ...
- leetcode 0210
目录 ✅ 1207 独一无二的出现次数 描述 解答 java hashMap api java my final solution: c other's solution, 用两个 数组 统计 ✅ 4 ...
- 零基础入门深度学习(6) - 长短时记忆网络(LSTM)
代码: def forward(self, x): ''' 根据式1-式6进行前向计算 ''' self.times += 1 # 遗忘门 fg = self.calc_gate(x, self.Wf ...
- 【Fine学习笔记】python 文件l操作方法整理
python脚本可以对excel进行创建.读.写.保存成指定文件名,保存到指定路径的操作.整理了以下处理方法: 首先区别几个操作方式: "r" 以读方式打开,只能读文件 , 如 ...
- 关于 UIDatePicker 在iOS9 系统上的一个坑
在使用 UIDatePicker时,在iOS9系统上上遇到一个很奇怪的问题,在其他系统版本中没发现,设置年月日格式显示的视图,在iOS9设备上出现中间月份无法显示的问题: 检查代码没问题,这个视图是使 ...
- SQL Server 消息队列,处理程序错误
SQL Server 消息队列,处理程序错误存储过程书写错误,会导致消息处理队列停用,此时只需将错误修正,再将队列处理状态启用即可. ALTER QUEUE OrdBomPurQty_ReivceQu ...
- Do You Know These Plastic Bottle Processing Terms?
The molding process of a plastic bottle refers to a process of making a final plastic article from a ...
- SpringBoot 获得 properties 文件中数据方式
参考:https://blog.csdn.net/qq_37171353/article/details/78005845
- RESTFul Client入门实例
client.html文件内容为: <!DOCTYPE html> <html> <head> <title>RESTFul Client test p ...