【问题标题】:Spring single page for every url route and subroute "/a/** => /a/index.html except /a/static/**"每个 url 路由和子路由的 Spring 单页 "/a/** => /a/index.html 除了 /a/static/**"
【发布时间】:2018-02-10 13:05:01
【问题描述】:

我正在构建 spring 网站,该网站在子路由下反应单页应用程序,我当前的 url 结构应该看起来像

localhost/admin/** => react app
localhost/**       => spring thymeleaf/rest/websocket app for everything else

react app mapping:
localhost/admin/static/**  => static react files
localhost/admin/**         => react index.html for everything else

Example of project resources structure:  
resources/
    admin/         <= my admin react files is here
        index.html
        static/    <= react css, js,  statics
    templates/     <= thymeleaf templates
    static/        <= theamleaf static
    ...

所以我需要为每个 url 路由及其子路由转发 index.html 文件。除了静态文件之外,基本上都是单页应用程序

看起来像一个常见的任务,这是我已经尝试过的一些事情:


项目 spring + react + gradle 如何构建项目的完整工作演示以及为什么我不能将 react 文件放在不同的目录中(例如 /resources/static):https://github.com/varren/spring-react-example

不能将forward:/admin/index.html 用于/admin/**,因为这将创建递归,因为admin/index.html 也在admin/** 之下并且必须以某种方式拦截admin/static/**


不能在WebMvcConfigurerAdapter 中使用addResourceHandlers
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("/admin/**")
            .addResourceLocations("classpath:/admin/");
}

index.html 仅映射到 /admin/index.html url,并且此选项几乎可以工作,但前提是您从 localhost/admin/index.html 访问 react 应用程序


看到thisthisthis 和许多其他链接,我也有一些解决方案,但也许有我看不到的通用选项

【问题讨论】:

    标签: java spring reactjs spring-mvc single-page-application


    【解决方案1】:

    现在我正在使用自定义 ResourceResolver 来解决这个问题

    演示:https://github.com/varren/spring-react-example

    @Configuration
    public class BaseWebMvcConfigurerAdapter extends WebMvcConfigurerAdapter {
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
    
            ResourceResolver resolver = new AdminResourceResolver();
            registry.addResourceHandler("/admin/**")
                    .resourceChain(false)
                    .addResolver(resolver);
    
    
            registry.addResourceHandler("/admin/")
                    .resourceChain(false)
                    .addResolver(resolver);
        }
    
    
        private class AdminResourceResolver implements ResourceResolver {
            private Resource index = new ClassPathResource("/admin/index.html");
    
            @Override
            public Resource resolveResource(HttpServletRequest request, String requestPath, List<? extends Resource> locations, ResourceResolverChain chain) {
                return resolve(requestPath, locations);
            }
    
            @Override
            public String resolveUrlPath(String resourcePath, List<? extends Resource> locations, ResourceResolverChain chain) {
                Resource resolvedResource = resolve(resourcePath, locations);
                if (resolvedResource == null) {
                    return null;
                }
                try {
                    return resolvedResource.getURL().toString();
                } catch (IOException e) {
                    return resolvedResource.getFilename();
                }
            }
    
            private Resource resolve(String requestPath, List<? extends Resource> locations) {
    
                if(requestPath == null) return null;
    
                if (!requestPath.startsWith("static")) {
                    return index;
                }else{
                    return new ClassPathResource("/admin/" + requestPath);
                }
            }
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2023-03-20
      • 1970-01-01
      • 2017-04-29
      • 1970-01-01
      • 2021-10-28
      • 2016-12-11
      • 1970-01-01
      • 1970-01-01
      • 2020-11-09
      相关资源
      最近更新 更多