当前位置: 首页 > news >正文

前后端分离项目,整合成jar包,刷新404或空白页,解决方法

问题解决

1、注销遇到404,或刷新遇到404

# 添加错误跳转
@Component
public class ErrorConfig implements ErrorPageRegistrar {@Overridepublic void registerErrorPages(ErrorPageRegistry registry) {ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/");  registry.addErrorPages(error404Page);}
}# 这个"/"为之前设置过的
@Controller
public class loginController {
//    首页@GetMapping("/")public String index(){return "index";}
}

2、遇到刷新后空白页

前提提要:前后端分离项目,单独运行时毫无问题,路由可以正常跳转,但是我将前端vue打包后放入后端的static文件夹下,并在后端使用Thymeleaf来调用静态资源,添加了路由跳转的controller

@Controllerpublic class loginController {    首页@GetMapping("/")public String index(){return "index";}}

以及mvc配置

 public class pictureConfig extends WebMvcConfigurationSupport{/*** springboot 2.0配置WebMvcConfigurationSupport之后,会导致默认配置被覆盖,要访问静态资源需要重写addResourceHandlers方法*/
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {registry.addResourceHandler("/**").addResourceLocations("classpath:/resources/").addResourceLocations("classpath:/static/").addResourceLocations("classpath:/public/");registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");registry.addResourceHandler("/uploads/**")    // 虚拟路径// file: 表示以本地的路径方式去访问绝对路径。.addResourceLocations("file:D:\\bus_system\\service\\bus_service\\src\\main\\resources\\static\\");    // 绝对路径// 添加Swagger的访问路径registry.addResourceHandler("swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");super.addResourceHandlers(registry);
}

和发生404跳转回index也就是登录页

@Componentpublic class ErrorConfig implements ErrorPageRegistrar {
@Override
public void registerErrorPages(ErrorPageRegistry registry) {ErrorPage error404Page = new ErrorPage(HttpStatus.NOT_FOUND, "/");registry.addErrorPages(error404Page);
}}

现在我登录进页面,点击列表,url=http://localhost:8013/Books/list,可以访问,但是当我点击页面的刷新,页面空白路径还是http://localhost:8013/Books/list,但是访问的css和js的路径资源为http://localhost:8013/Books/static/js/chunk-6dbb.969838d0.js,多了Books,请问怎么解决

解决方法:

# mvc配置添加
registry.addResourceHandler("/Books/static/**").addResourceLocations("classpath:/static/");

但是这又会有个问题,当我有多个路由,Books/list,Teacher/list等等,我都要加上registry.addResourceHandler(“/Teacher/static/**”)
.addResourceLocations(“classpath:/static/”);吗

最终解决:

 registry.addResourceHandler("/{module}/static/**")  //"{module}"部分可以匹配任何路由的名称.addResourceLocations("classpath:/static/");
http://www.lryc.cn/news/158988.html

相关文章:

  • 前端、后端面试集锦
  • Web存储
  • 字节对齐(C++,C#)
  • 使用mybatisplus查询sql时,报Error attempting to get column ‘ID‘ from result set错误
  • ElementUI浅尝辄止32:NavMenu 导航菜单
  • @Value的注入与静态注入 与 组件中静态工具类的注入
  • Qt--自定义搜索控件,QLineEdit带前缀图标
  • 8月AI实战:工业视觉缺陷检测
  • Kubernetes的ExternalName详解
  • 使用 Pandera 的 PySpark 应用程序的数据验证
  • README
  • Excel周报制作
  • Qt QtCreator 所有官方下载地址
  • C++包含整数各位重组
  • 数学建模--模型总结(5)
  • JavaScript 中的原型到底该如何理解?
  • 【MySQL基础】事务隔离03
  • 2023高教社杯数学建模C题思路分析 - 蔬菜类商品的自动定价与补货决策
  • 【MySQL】初见数据库
  • 选择合适的帧率和分辨率:优化RTSP流视频抓取(java)
  • HTTP协议都有哪些方法?
  • 数学建模--非整数规划求解的Python实现
  • LeetCode 48题: 旋转图像
  • 集成快递物流平台(快递100、快递鸟、闪送)连通多个应用
  • 搭建hadoop集群的常见问题及解决办法
  • virtualbox centos 使用NAT模式上网
  • 蓝桥杯官网填空题(梅森素数)
  • IBM Spectrum LSF Application Center 以应用程序为中心的工作负载提交和管理
  • 同步FIFO的verilog实现(2)——高位扩展法
  • 数据结构与算法面试