我有一个 Spring boot 应用程序,并且有两个完全独立的 Angular 项目。 一个项目供最终用户使用,另一个项目供管理员使用。 出于商业原因,我被迫将它们作为两个不同的项目。我希望这两个项目都打包到同一个 WAR 中,当用户点击最终用户的 URL 时,应加载最终用户 Angular 应用程序,并且当访问管理 URL 时,应加载管理 Angular 项目。
我知道 Spring boot 从 /public
或 /static
目录加载 Angular 项目。但是,由于就我而言,我有两个不同的项目,因此我无法将它们放在同一个文件夹下,因为它们都有许多共同的文件名,包括index.html。
到目前为止,我已经完成了以下工作:
- 在
“public”
下创建了两个不同的文件夹作为“enduser”
并且“管理”
。 创建了一个具有两种方法的 Controller :
@Controller public class SampleController { @RequestMapping(method = RequestMethod.GET, produces = "text/html", value = "/enduser") public String enduser() { return "/enduser/index.html"; } @RequestMapping(method = RequestMethod.GET, produces = "text/html", value = "/admin") public String admin() { return "/admin/index.html"; } }
但是,当我这样做时,仅加载index.html文件,而未加载其他组件(我的意思是整个 Angular 项目未加载)。因此不会加载依赖的 View 和样式等。
有人知道如何处理上述情况吗?谢谢。
最佳答案
只需在下面发布解决方案可能会对其他人有所帮助。
我正在使用两个不同的文件夹,其中放置 Angular 源代码中的 dist 文件夹的内容。 我有两个不同的基于 Angular 的应用程序管理和公共(public)。
Spring 应用程序中我放置编译的 Angular 代码的位置如下:
/src/main/resources/admin-app/*
(admin-index.html and other static files from admin angluar project)
/src/main/resources/static/*
(index.html and other static files from public angluar project)
Spring启动配置
@EnableAutoConfiguration
public class AddCustomLocations {
@Bean
WebMvcConfigurer configurer () {
return new WebMvcConfigurerAdapter() {
@Override
public void addResourceHandlers (ResourceHandlerRegistry registry) {
registry.addResourceHandler("/pages/**").
addResourceLocations("classpath:/admin-app/");
}
};
}
现在就是这样,您将点击 http://localhost:8080 。它将加载公共(public) Angular 应用程序。 当你击中时 现在当我尝试打开http://localhost:8080/pages/admin-index.html时 它将加载管理 Angular 应用程序。
神奇的是静态文件夹中的所有文件默认情况下都可以工作,但如果我们想创建另一个文件夹来放置静态文件。我们必须通知 Spring Boot 应用程序。 您还可以在静态文件夹内创建子文件夹。
public & admin-app
您必须点击这样的网址
localhost:8080/public/index.html localhost:8080/public/admin-index.html
您不需要为此创建上述 spring 配置。
关于java - 从 Spring boot 加载多个 Angular 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34663085/