我找到了解决方案..
从浏览器访问以下网址
1. http://localhost:8080/pageone
2. http://localhost:8080/pagetwo
请求将直接到达 spring 控制器并由下面的控制器处理
@Controller // should not be @RestController
public class BaseController implements ErrorController { // implement ErrorController to handle 404 (error pages)
// Always redirect to index.html page (only option)
// Can handle all request (which end up in 404)
@GetMapping("/error")
public String error() {
return "forward:/index.html";
}
// Specific request
@GetMapping("/pageone")
public String pageone() {
return "forward:/index.html";
}
// Specific request with pathvariable and requestparam
@GetMapping("/pagetwo" + "/{id}")
public String pagetwo(@PathVariable("id") String id,
@RequestParam("param1") String param1,
@RequestParam("param2") String param2 ) {
// both pathvariable and requestparam will be sent to front end
return "forward:/index.html";
}
}
索引页面将在该角度自动加载请求的页面后调度,例如:'/pageone'或'pagetwo'
应用程序路由.modules.ts
const routes: Routes = [
{
path: 'pageone',
component: PageOneComponent
},
{
path: 'pagetwo/:id', // to handle pathvariables
component: PageTwoComponent
},
{
path: '**',
component: HomeComponent
}
]
您可以访问接收到的路径变量和请求参数,如下所示
PageTwoComponent.ts
ngOnInit(): void {
console.log(this.route.snapshot.paramMap.get('id')); // get pathparms
this.route.queryParams.subscribe(params => { // get queryparams
console.log(params['param1']);
console.log(params['param2']);
});
}
简而言之,@Controller
and implements ErrorController
@GetMapping("/error")
public String error() {
return "forward:/index.html";
}
After that angular automatically load requested page (routing url)