angular - 重新加载在 docker 中运行的 Angular 应用程序会出现 500 内部服务器错误
问题描述
我正在开发一个 Angular 应用程序,BE 在 Java/Spring-Boot 中。不知何故,我能够在除 /management/** 之外的每个视图中执行重新加载或直接在浏览器中直接复制地址。
www.site-name.com/inscription(工作) www.site-name.com/management/utilisateurs(不工作)
这些是我在 Angular 中的路由配置
const routes: Routes = [
{
path: '',
redirectTo: '/home',
pathMatch: 'full'
},
{
path: 'home',
component: IntroPageComponent,
canActivate: [HomeGuard]
},
{
path: '',
loadChildren: () => import('../user-auth/').then(module => module.UserAuthModule),
},
{
path: 'inscription',
loadChildren: () => import('../inscription').then(module => module.InscriptionModule),
canActivateChild: [AuthenticatedGuard, InscriptionGuard]
},
{
path: 'management',
loadChildren: () => import('../management').then(module => module.ManagementModule),
canActivateChild: [AuthenticatedGuard, ManagementGuard]
},
{
path: '**',
redirectTo: '/home'
}
];
然后管理模块有自己的路由配置
const routes: Routes = [
{
path: 'utilisateurs',
component: UsersComponent,
resolve: { man : ManagementPreload }
},
"more routes here"
];
提供 index.html 的 Spring 控制器
@GetMapping({
"/",
"/home",
"/incription",
"/management/**",
......
})
public String index() {
return "index";
}
和 dockerfile
FROM openjdk:8-jdk-alpine
VOLUME /tmp
ARG JAR_FILE
ARG APP_NAME
COPY ${JAR_FILE} app.jar
ENTRYPOINT ["java","-Djava.security.egd=file:/dev/./urandom","-jar","/app.jar", "--file:/app/config/application-siteinscription.properties", "-XX:+UseG1GC"]
解决方案
我设法弄清楚问题是什么并实施了一个我不确定它是否是最合适的解决方案,但我会在这里为其他有同样问题的人发布它,也许可以就如何实施更好的解决方案获得一些反馈.
值得一提的是,角度路由配置没有任何问题。
该问题与 SpringMVC ViewResolver(s) 有关
根据我的配置
@GetMapping({
"/",
"/home",
"/incription",
"/management/**",
......
})
public String index() {
return "index";
}
当 Spring ViewResolver 看到类似的路由时
/management/**
它将尝试查找视图索引并根据配置返回它,例如
/path/to/resource/management/index.html
我所需要的只是无论路线总是返回
/path/to/resource/index.html
我设法使应用程序按照我的需要工作而没有太多开销的方法就是简单地实现控制器,如
@GetMapping({
"/",
"/home",
"/auth",
"/inscription",
...
})
public String index() {
return "index";
}
@GetMapping("/management/**")
public ModelAndView forwardToHome() {
return new ModelAndView("forward:/home", new ModelMap());
}
推荐阅读
- git - 带有敏感数据的 cordova package.json 项目的 Git 提交
- python - 在另一个类中访问一个类的属性和方法。继续上一个话题
- python - Google 距离矩阵 API:如何指定起点-终点对?
- python - 通过将其中一个键的值固定为第一个元素来自定义排序有序字典的列表
- c++ - 类外的成员函数定义
- python - 使用异步/等待打开/关闭数据库连接的最佳方式
- r - 如何隐藏(或删除)箱线图中的点?
- android - 任务 ':app:transformClassesWithInstantRunVerifierForDebug' 执行失败
- javascript - 通过js添加html标签/语义
- javascript - swiper.js 控制两个实例