简体   繁体   English

从Spring启动加载多个Angular项目

[英]Load multiple Angular projects from Spring boot

I have a Spring boot application and I have two completely independent angular projects. 我有一个Spring启动应用程序,我有两个完全独立的角度项目。 One project for the end user and another project for the admin. 最终用户的一个项目和管理员的另一个项目。 For business reasons, I am forced to have both of them as two different projects. 出于商业原因,我被迫将它们都作为两个不同的项目。 I want both these projects to be packaged into the same WAR and when the user hits the URL for the end user, the end user angular application should be loaded and when the admin URL is accessed, the admin angular project should be loaded. 我希望将这些项目打包到同一个WAR中,当用户点击最终用户的URL时,应加载最终用户角度应用程序,当访问管理URL时,应加载管理角度项目。

I know that Spring boot loads the angular project from /public or /static directories. 我知道Spring引导从/public/static目录加载角度项目。 However, since in my case, I have two different projects, I cant put them under the same folder since both of them have many file names in common including index.html. 但是,因为在我的情况下,我有两个不同的项目,我不能把它们放在同一个文件夹下,因为它们都有许多共同的文件名,包括index.html。

So far, I have done the following: 到目前为止,我已经完成了以下工作:

  1. Created two different folders under "public" as "enduser" and "manage" . "public"下创建了两个不同的文件夹,分别为"enduser""manage"
  2. Created a controller which has two methods : 创建了一个控制器,它有两种方法:

     @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"; } } 

However, when I do like this only the index.html file is loaded and the other components are not loaded(by which I mean the entire angular project is not loaded). 但是,当我这样做时,只加载index.html文件,而不加载其他组件(我的意思是没有加载整个角度项目)。 So the dependent views and styles etc are not loaded. 因此不会加载相关的视图和样式等。

Does anybody have an idea on how to handle the above scenario? 有没有人知道如何处理上述情况? Thanks. 谢谢。

Resolved it the following way: 通过以下方式解决了它:

Created an MVC controller, added two different methods for each URL pattern.Copied the dist files of each project into aa separate folder under static folder.Added a different tag to each project.This way your angular project wont work on grunt serve.So you will have to set url as empty when you run on grunt serve and set it to a value only when you run for build. 创建了一个MVC控制器,为每个URL模式添加了两种不同的方法。将每个项目的dist文件复制到静态文件夹下的一个单独的文件夹中。为每个项目添加了一个不同的标记。这样你的角项目就不能用于grunt serve.So you当您在grunt服务器上运行时,必须将url设置为空,并且仅在运行构建时将其设置为值。

Just posting the solution below it might help somebody else. 只需在其下方发布解决方案,就可以帮助其他人。

I am using two different folder where I am placing the contents of dist folder from angular source code. 我正在使用两个不同的文件夹,我从角度源代码放置dist文件夹的内容。 I have two different angular based applications admin and public. 我有两个不同的基于角度的应用程序admin和public。

Locations in spring app where I am placing the compiled angular code is as: spring app中我放置编译的角度代码的位置如下:

/src/main/resources/admin-app/* / src目录/主/资源/管理,应用程序/ *

(admin-index.html and other static files from admin angluar project) (admin-index.html和来自admin angluar项目的其他静态文件)

/src/main/resources/static/* / src目录/主/资源/静态/ *

(index.html and other static files from public angluar project) (来自public angluar项目的index.html和其他静态文件)

Spring boot config Spring启动配置

@EnableAutoConfiguration
public class AddCustomLocations {
    @Bean
    WebMvcConfigurer configurer () {
        return new WebMvcConfigurerAdapter() {
            @Override
            public void addResourceHandlers (ResourceHandlerRegistry registry) {
                registry.addResourceHandler("/pages/**").
                          addResourceLocations("classpath:/admin-app/");
            }
        };
    }

That's it now when you will hit http://localhost:8080 . 现在就是你点击http:// localhost:8080 It will load the public angular app. 它将加载公共角度应用程序。 And when you will hit Now when I am trying to open http://localhost:8080/pages/admin-index.html It will load the admin angular app. 当你试图打开http:// localhost:8080 / pages / admin-index.html时,你会点击Now它将加载管理角度应用程序。

The magic is all files inside static folder will work by default but if we want to create another folder for placing our static files. 神奇的是静态文件夹中的所有文件都默认工作,但是如果我们想要创建另一个文件夹来放置我们的静态文件。 We will have to notify the spring boot app. 我们将不得不通知spring boot应用程序。 You can also create a sub folder inside the static folder. 您还可以在静态文件夹中创建子文件夹。

public & admin-app public&admin-app

You will have to hit the url like this 你必须像这样打网址

localhost:8080/public/index.html localhost:8080/public/admin-index.html localhost:8080 / public / index.html localhost:8080 / public / admin-index.html

You will not need to create the above spring configuration for this. 您无需为此创建上述弹簧配置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM