【问题标题】:Can't access angular pages through spring-boot application无法通过spring-boot应用程序访问角度页面
【发布时间】:2019-08-27 17:19:00
【问题描述】:

我想构建包含资源/静态/文件夹中的角度文件的spring-boot应用程序。

我已经构建了角度文件并将它们放入静态目录。

并且登录页面工作正常(我认为是因为重定向),成功登录后用户应该被重定向到主页但我得到 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},

【问题讨论】:

    标签: java angular spring-boot


    【解决方案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 生成的)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2021-12-03
      • 1970-01-01
      • 2017-02-08
      • 2021-11-15
      • 2019-03-13
      • 2021-01-26
      • 1970-01-01
      相关资源
      最近更新 更多