springboot整合Thymeleaf web开发出现Whitelabel Error Page

背景

在做java端上应用开发的时候,从资源和部署操作成本两方面考虑,一般会将前端的静态资源直接与后端应用一起打包,通过springboot内嵌的Tomcat提供web服务。进入web首页登录一直到后续业务流程正向操作,页面都能正常加载静态资源,但触发页面刷新操作的时候,就出现了Whitelabel Error Page,访问不成功。本文针对此问题做解答。

1. springboot web项目搭建开发

在pom文件中增加thymeleaf依赖

复制代码
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-thymeleaf</artifactId>
</dependency>

配置静态资源处理器:

java 复制代码
public class BaseWebConfig implements WebMvcConfigurer {
    private LoginInterceptor loginInterceptor;
    private PermissionInterceptor permissionInterceptor;

    public List<String> whiteUrl = new ArrayList<>(Arrays.asList(
            "/login",
            "/doLogin",
            "/css/**", // 排除css目录下的所有资源
            "/js/**",  // 排除js目录下的所有资源
            "/images/**", // 排除images目录下的所有资源
            "/favicon.ico", // 排除favicon.ico
            "/static/**" // 排除static目录下的所有资源
    ));
    
    // 登录拦截器,后端所有的接口都是api/开头
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(loginInterceptor)
               .addPathPatterns("/api/**")
               .excludePathPatterns(whiteUrl)
               .order(1);
    }

    // 静态资源放到html目录下
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**")
               .addResourceLocations("classpath:/html/");
    }

    // 默认进入登录页
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("forward:/index.html");
    }
}

配置启动thymeleaf

java 复制代码
spring.thymeleaf.prefix=classpath:/html/
spring.thymeleaf.suffix=.html
spring.thymeleaf.cache=false

静态文件结构:

|-- src

| |--main

| | |-- resources

| | |-- application.properties

| | | |-- html

| | | | |-- index.html

| | | | |-- app.js

...

2. 问题暴露

访问后端localhost:8080/login,正常跳转到登录页。登录成功后,浏览器中的地址因为前端自动的路由规则,变成了

localhost:8080/edge?ownerId=jcknuxh&tab=sub1-data-management

看网络请求也都正常

在浏览器页面刷新,直接进入Whitelabel Error Page

查看很多网上的教程,有建议直接拦截所有的请求,转发到index.html视图,如下代码:

java 复制代码
@Controller
public class IndexController{

    @GetMapping("{/path})
    public String index(@PathVariable String path){
        return "forward:/index.html";
    }

}

然后就出现了错误:

circular view path

3. 问题解决

解题的思路是参照NGINX加载静态页面,当有不识别的页面的时候,直接跳转到index.html页面。Nginx中的配置如下:

XML 复制代码
server {
    listen 80;

    server_name localhost;

    root /usr/share/nginx/html;

    index index.html;

    location / {
        try_files $uri $uri/ /index.html;
    }

}

在springboot中怎么配置呢?

java 复制代码
 // 默认进入登录页
    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/{path;[^\\.]*}").setViewName("forward:/index.html");
    }

问题就解决了。原理是当遇到不识别的页面的时候,就跳转到首页,首页有对应的js、css请求路由,从而在刷新页面的时候,正常渲染。

相关推荐
麦兜*4 小时前
Spring Boot启动优化7板斧(延迟初始化、组件扫描精准打击、JVM参数调优):砍掉70%启动时间的魔鬼实践
java·jvm·spring boot·后端·spring·spring cloud·系统架构
KK溜了溜了5 小时前
JAVA-springboot 整合Redis
java·spring boot·redis
大只鹅5 小时前
解决 Spring Boot 对 Elasticsearch 字段没有小驼峰映射的问题
spring boot·后端·elasticsearch
天河归来5 小时前
使用idea创建springboot单体项目
java·spring boot·intellij-idea
IT_10245 小时前
Spring Boot项目开发实战销售管理系统——数据库设计!
java·开发语言·数据库·spring boot·后端·oracle
武昌库里写JAVA6 小时前
Oracle如何使用序列 Oracle序列使用教程
java·开发语言·spring boot·学习·课程设计
超级小忍7 小时前
Spring Boot 中常用的工具类库及其使用示例(完整版)
spring boot·后端
程序员张37 小时前
SQL分析与打印-p6spy组件
spring boot·sql·mybatis·mybatisplus·p6spy
CHENWENFEIc8 小时前
SpringBoot论坛系统安全测试实战报告
spring boot·后端·程序人生·spring·系统安全·安全测试
代码老y10 小时前
Spring Boot + 本地部署大模型实现:优化与性能提升
java·spring boot·后端