问题解决
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);
}},
解决方法:
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/");