Bla*_*ite 13 java routing spring-boot angular angular4-router
我有一个Angular 4(ES6)应用程序,我想从Spring Boot应用程序提供服务.我的Angular应用程序有一个index.html,当http:// localhost:8080的地址被命中时,Spring Boot知道要映射到在Angular中映射到"/ search"的index.html文件.
但是,我有另一条名为"adminlogin"的路线,我将通过该途径访问
但在这个例子中,它命中了我的Spring Boot应用程序,它没有映射,然后抛出错误.
如何获取http:// localhost:8080/adminLogin的地址转到我的Angular应用程序?
小智 14
我的SpringBoot 2和Angular6应用程序遇到了类似的问题.我实现了WebMvcConfigurer覆盖addResourceHandlers()方法的接口,并index.html在弹簧控制器中找不到映射时重定向到.这可以在Spring boot 1.5.x中完成,扩展(现已弃用)WebMvcConfigurerAdaptor类.这个在stackoverflow线程中有详细讨论:https://stackoverflow.com/questions/38516667/springboot-angular2-how-to-handle-html5-urls# =
我target/classes/static使用(之前)中的outputPath字段将我构建的角度应用程序放在此位置.这是示例代码:angular.json.angular-cli.json
@Configuration
public class MyAppWebMvcConfigurer implements WebMvcConfigurer {
@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler("/**/*")
.addResourceLocations("classpath:/static/")
.resourceChain(true)
.addResolver(new PathResourceResolver() {
@Override
protected Resource getResource(String resourcePath, Resource location) throws IOException {
Resource requestedResource = location.createRelative(resourcePath);
return requestedResource.exists() && requestedResource.isReadable() ? requestedResource : new ClassPathResource("/static/index.html");
}
});
}
}
Run Code Online (Sandbox Code Playgroud)
由于该路径adminlogin未在任何controllerspring 应用程序中映射,将请求分派到/error,因此您收到错误页面。
您需要将其路由回 angular,以便adminlogin页面将通过Angular.
遵循简单的路线来做到这一点。
@Controller
public class RouteToAngular implements ErrorController {
@RequestMapping("/error")
public String handleError() {
return "/";
}
@Override
public String getErrorPath() {
return "/error";
}
}
Run Code Online (Sandbox Code Playgroud)
注意:不要忘记在 Angular 中实现 404 页面。
我希望这会有所帮助。
| 归档时间: |
|
| 查看次数: |
7221 次 |
| 最近记录: |