kopn9k
kopn9k

Reputation: 21

Can't access angular pages through spring-boot application

I want to build spring-boot application that includes angular files in resources/static/ folder.

I have built angular files and placed them into static directory.

And login page works ok (i think because of redirect), after successful login user should be redirected on main-page but i got 404 eror, i think problem in my angular app.routing file, i have tried several variants of mapping such as /main-page and even http://localhost:8080/main-page, but it's still not working. Any ideas what should i do?

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},

Upvotes: 1

Views: 1267

Answers (1)

kopn9k
kopn9k

Reputation: 21

So as i understand that happens, because when we send request to localhost:8080/main-page, it try to found resource /main-page in static folder, but we have only index.html in static. To solve that we should add next configuration.

@Configuration
public class MvcConfig implements WebMvcConfigurer {

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

Also we should change our app-routing.module.ts file on

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

Now when we will send any request that match /ui/** pattern we will be redirected on index.html (that generated by angular).

Upvotes: 1

Related Questions