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

问题解决

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

shell 复制代码
# 添加错误跳转
@Component
public class ErrorConfig implements ErrorPageRegistrar {

    @Override
    public 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

java 复制代码
@Controller
 public class loginController {
     首页
 @GetMapping("/")
 public String index(){
 return "index";
 }
 },

以及mvc配置

java 复制代码
 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也就是登录页

java 复制代码
@Component
 public 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,请问怎么解决

解决方法:

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

但是这又会有个问题,当我有多个路由,Books/list,Teacher/list等等,我都要加上registry.addResourceHandler("/Teacher/static/**")

.addResourceLocations("classpath:/static/");吗

最终解决:

shell 复制代码
 registry.addResourceHandler("/{module}/static/**")  //"{module}"部分可以匹配任何路由的名称
         .addResourceLocations("classpath:/static/");
相关推荐
一 乐4 分钟前
校园实验室|基于springboot + vue校园实验室管理系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端
Lisonseekpan15 分钟前
Spring Boot Email 邮件发送完全指南
java·spring boot·后端·log4j
sheji341619 分钟前
【开题答辩全过程】以 基于Springboot的体检中心信息管理系统设计与实现为例,包含答辩的问题和答案
java·spring boot·后端
天河归来23 分钟前
本地windows环境升级dify到1.11.1版本
java·spring boot·docker
超级种码1 小时前
Java:JavaAgent技术(java.instrument和java.attach)
java·开发语言·python
甜鲸鱼1 小时前
【Spring AOP】操作日志的完整实现与原理剖析
java·spring boot·spring
狗头大军之江苏分军1 小时前
年底科技大考:2025 中国前端工程师的 AI 辅助工具实战盘点
java·前端·后端
一 乐1 小时前
酒店客房预订|基于springboot + vue酒店客房预订系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·后端
计算机毕设指导61 小时前
基于Spring Boot的防诈骗管理系统【源码文末联系】
java·spring boot·后端·spring·tomcat·maven·intellij-idea
a程序小傲2 小时前
饿了吗Java面试被问:Redis的持久化策略对比(RDBVS AOF)
java·redis·面试