前后端分离项目,整合成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/");
相关推荐
努力写代码的熊大14 分钟前
c++异常和智能指针
java·开发语言·c++
山岚的运维笔记18 分钟前
SQL Server笔记 -- 第15章:INSERT INTO
java·数据库·笔记·sql·microsoft·sqlserver
Yvonne爱编码19 分钟前
JAVA数据结构 DAY5-LinkedList
java·开发语言·python
小王不爱笑1321 小时前
LangChain4J 整合多 AI 模型核心实现步骤
java·人工智能·spring boot
西凉的悲伤1 小时前
spring-boot-starter-validation使用注解进行参数校验
java·spring boot·参数校验·validation·注解校验参数
LucDelton1 小时前
Java 读取无限量文件读取的思路
java·运维·网络
夹锌饼干1 小时前
mysql死锁排查流程--(处理mysql阻塞问题)
java·mysql
小信丶1 小时前
@EnableTransactionManagement注解介绍、应用场景和示例代码
java·spring boot·后端
To Be Clean Coder1 小时前
【Spring源码】createBean如何寻找构造器(四)——类型转换与匹配权重
java·后端·spring
-孤存-2 小时前
SpringBoot核心注解与配置详解
java·spring boot·后端