无法通过Spring Boot应用程序访问角度页面

时间:2019-08-27 17:19:00

标签: java angular spring-boot

我想构建一个spring-boot应用程序,该应用程序在resources / static /文件夹中包含角度文件。

我已经构建了角度文件并将其放置到静态目录中。

登录页面工作正常(我认为是由于重定向),成功的登录用户应在主页上重定向后,但我得到404错误,我认为我的角度app.routing文件存在问题,我尝试了以下几种方法映射,例如/main-page甚至是http://localhost:8080/main-page,但仍然无法正常工作。任何想法我该怎么办?

app-routing.module.ts file

{ path: '', redirectTo: '/login', pathMatch: 'full', canActivate: [AuthGuard]},
{ path: 'cars/add', component: CarEditorComponent},
{ path: 'login', component: LoginComponent},
{ path: 'main-page', component: WelcomeComponent},
{ path: 'stocks', component: StockDashboardComponent},

1 个答案:

答案 0 :(得分:1)

据我所知,发生这种情况的原因是,当我们向localhost:8080/main-page发送请求时,它会尝试在静态文件夹中找到资源/main-page,但静态只有index.html。为了解决这个问题,我们应该添加下一个配置。

@Configuration
public class MvcConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/ui/**").setViewName("/");
    }
}

我们还应该更改app-routing.module.ts file

{ path: 'ui/cars/add', component: CarEditorComponent},
{ path: 'ui/login', component: LoginComponent},
{ path: 'ui/main-page', component: WelcomeComponent},
{ path: 'ui/stocks', component: StockDashboardComponent},

现在,当我们发送任何与/ui/**模式匹配的请求时,我们将被重定向到index.html(由angular生成)。

相关问题