首页 文章

页面刷新使用springboot应用程序在angular5中给出404

提问于
浏览
1

我有一个在Springboot上运行的端口8080上的web服务 . 当我点击下面的url它工作正常:

http://localhost:8080/app,我将网址重定向到以下位置:

http://localhost:8080/myHome/home

现在当我刷新页面时,我得到404

下面是我的index.html的内容:

<base href="/myHome">

的package.json:

"scripts": {
    "ng": "ng",
    "start": "ng serve --proxy-config proxy.conf.json",
    "build": "ng build --dev --deploy-url=\"/app/\"",
    "test": "ng test",
    "lint": "ng lint",
    "e2e": "protractor"
},

我曾尝试使用中提到的LocationStrategy

Angular 2: 404 error occur when I refresh through the browser,但它不适合我 .

2 回答

  • 1

    由于 spring 检查资源/ myHome / home而导致错误,但未找到

    资源位置是/ app所以你可以修复 -

    1) Use hashRouting in angular application
    

    要么

    2) Redirect to '/' in springboot aplication when requesting came from another url /**
    
  • 0

    我在spring boot 2中遇到了同样的问题 . 我已将资源解析器位置添加为静态目录,如果没有与任何文件匹配,则加载index.html

    @Configuration
    public class WebConfig implements WebMvcConfigurer {
    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
    
            registry.addResourceHandler("/**/*")
                    .addResourceLocations("classpath:/static/")
                    .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/index.html");
                        }
                    });
    
    
    
    }
    

相关问题