小编Luc*_*cke的帖子

Angular 与 Spring boot 静态不起作用

我有一个 Spring Boot 应用程序,其中包含有角度的前端

像这样:

src/main/resources/static/zanori2

ng build在 zanori2 中,我得到了类似的结果:

index.html、index.js、favico.ico 等

我尝试了这个资源句柄:

@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer {
    /*@Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {*/
        //registry.addResourceHandler("/**/*")
        /*.addResourceLocations("classpath:/static/zanori2/")
        .resourceChain(true)
        .addResolver(new PathResourceResolver() {
            @Override
            protected Resource getResource(String resourcePath,
                Resource location) throws IOException {
                Resource requestedResource = location.createRelative(resourcePath);
                return requestedResource.exists() && requestedResource.isReadable() ? requestedResource
                : new ClassPathResource("/static/zanori2/index.html");
            }
        });
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,当我转到:时localhost:8080/zanori2/index.html,它会让我返回localhost:8080并且 js 文件可以工作。

然而,这很奇怪,因为我不允许共享网址,因为如果我直接访问 localhost:8080 我会得到一个未找到的页面。

并使用其他配置:

@Configuration
@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurer { …
Run Code Online (Sandbox Code Playgroud)

static static-files spring-boot angular

5
推荐指数
1
解决办法
2812
查看次数

标签 统计

angular ×1

spring-boot ×1

static ×1

static-files ×1