Answer the question
In order to leave comments, you need to log in
How to set up Angular 2 + Spring routing?
Good day everyone! Having made Angular 2 friends with Spring, I ran into a routing problem, and I had a few questions: It is
necessary that for all requests to the server it returns index.html, i.e. my start page. And then Angular itself "resolved" everything. Angular communicates with my server via /api. But I'm sure that I don't fully understand all the sections of my chain, because everything simply doesn't work the way I want =) Below I will give the code and what I think it should do:
Through the address bar, I enter the address of my site ( in this case it is spinning on my local machine : localhost:8080/). Next, the controller processes the request:
In theory, in this case, index.html will be returned for any request, unless the request consists of several "/", I mean: /smth/veryusefullinformation. In this case, the request should not be processed by this controller.
@RequestMapping(value = "/**", method = RequestMethod.GET)
public String welcome() {
return "views/index.html";
@RequestMapping(value = "/{value}", method = RequestMethod.GET)
public String welcome(@PathVariable("value") String value) {
if (value instanceof String)
return "views/index.html";
else return "views/index.html";
}
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**").addResourceLocations("/");
}
import {Component} from "angular2/core";
import {ROUTER_DIRECTIVES} from "angular2/router";
import {RouteConfig} from "angular2/router";
import {SiteRoot} from "./site/siteRoot.component";
import {AdminPanelRoot} from "./adminPanel/adminPanelRoot.component";
@Component({
selector: 'root-app',
template: `
<a [routerLink]="['/SiteRoot']">Site</a>
<a [routerLink]="['/AdminPanelRoot']">Admin</a>
<router-outlet></router-outlet>
`,
directives: [ROUTER_DIRECTIVES]
})
@RouteConfig([
{path: "/site", name: "SiteRoot", component: SiteRoot,useAsDefault: true},
{path: "/administrationPanel", name: "AdminPanelRoot", component: AdminPanelRoot}
])
export class AppComponent{
}
Answer the question
In order to leave comments, you need to log in
Maybe I didn't understand something... But all static resources (HTML, CSS, JS + img, etc.) in RESTful architecture are processed by a proxy server - nginx for example. That is, your proxy server gives static pages with Angular and JS code, and Angular already communicates with the server on Spring
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question