问题解决
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/");