简体   繁体   English

在单个服务器上运行带有Angular 2的Spring4MVC

[英]Run Spring4MVC with Angular 2 on a single server

I am new to angular2, I want to know what is the possible file structure for SpringMVC4 with angular 2? 我是angular2的新手,我想知道SpringMVC4的角度为2的可能文件结构是什么?

在此输入图像描述

As shown in image, it will work for Angular 1.x but file structure of Angular 2 is quite different and its component driven, and I am using angular 2 file structure as given below 如图所示,它适用于Angular 1.x,但Angular 2的文件结构完全不同,其组件驱动,我使用的是角度2文件结构,如下所示 在此输入图像描述

I searched a lot and I found that we can use Front end(using angular2) and back end(server- using spring/springboot) separately, but we need 2 server to run application. 我搜索了很多,我发现我们可以分别使用前端(使用angular2)和后端(服务器使用spring / springboot),但我们需要2台服务器来运行应用程序。 For example, Frontend : 192.168.100.1:4200 And Backend : 192.168.100.1:8080 例如,前端:192.168.100.1:4200和后端:192.168.100.1:8080

So is there any way or general file structure to run both angular2 and spring4MVC on same server (like 192.168.100.1:8080)? 那么有没有任何方法或一般文件结构在同一台服务器上运行angular2和spring4MVC(如192.168.100.1:8080)?

Thanks in advance. 提前致谢。 Answers will be appreciated! 答案将不胜感激!

No answers till now thats fine, I got solution. 直到现在还没有答案那很好,我得到了解决方案。 How I did ? 我怎么做的?

You need 2 contexts. 你需要2个上下文。

(1) Angular 2 project and (1)Angular 2项目和

(2) Spring MVC (2)Spring MVC

Follow below steps to achieve our main goal, to run SPRINGMVC + Angular2 on a single server. 按照以下步骤实现我们的主要目标,即在单个服务器上运行SPRINGMVC + Angular2。

  1. Create normal Dynamic web project. 创建正常的动态Web项目。
  2. Add all dependancy required for spring or user maven pom.xml 添加spring或user maven pom.xml所需的所有依赖项
  3. Open CMD, navigate to angular2 application. 打开CMD,导航到angular2应用程序。 Hit command 命中命令

    npm install and then npm安装然后

    ng build or use ng build --prod for production 构建或使用ng build --prod用于生产

this command will create a "dist" folder, copy all files including all folders. 此命令将创建一个“dist”文件夹,复制包括所有文件夹的所有文件。

  1. Paste those files and folders into WebContent directory. 将这些文件和文件夹粘贴到WebContent目录中。

  2. Last thing, you need to change basehref="./" in index.html. 最后,你需要在index.html中更改basehref =“./”。 Now you are ready to run server or you can deploy war file and serve it with tomcat or other servers. 现在您已准备好运行服务器,或者您可以部署war文件并使用tomcat或其他服务器提供服务。

If you are using Rest webservices and want to run angular2 and spring in a single server, 如果您正在使用Rest Web服务并希望在单个服务器中运行angular2和spring,

You need to put webServiceEndPointUrl as per your host url. 您需要根据主机URL放置webServiceEndPointUrl For example, If you are running app on localhost:8080, you need to keep url 例如,如果您在localhost:8080上运行应用程序,则需要保留URL

webServiceEndPointUrl= " http://localhost:8080/api/user "; webServiceEndPointUrl =“ http:// localhost:8080 / api / user ”; at angular side. 在有角的一面。 After that you can build and copy paste to your WebContent folder. 之后,您可以构建并复制粘贴到WebContent文件夹。

See below Image, File structure for springMVC+ANGULAR2 请参见下图图像,springMVC + ANGULAR2的文件结构

在此输入图像描述

I know there are many drawbacks to use these way for running application on a single server, but if it must required you can follow this. 我知道使用这些方法在单个服务器上运行应用程序有许多缺点,但如果必须要求,您可以遵循这一点。

If you change anything at angular side, you need to copy paste dist folder all time and then you can deploy it! 如果你在角边改变任何东西,你需要一直复制paste dist文件夹然后你可以部署它!

You can automate your solution - just use frontend-maven-plugin (with which you can install nodejs and build the angular project) and maven-resources-plugin to copy the contents of /angular/dist/ directory into root of .war file (see also this article ) 您可以自动化您的解决方案 - 只需使用frontend-maven-plugin (您可以使用它安装nodejs并构建角度项目)和maven-resources-plugin将/ angular / dist /目录的内容复制到.war文件的根目录中(另见本文

<plugin>
      <groupId>org.apache.maven.plugins</groupId>
      <artifactId>maven-resources-plugin</artifactId>
      <version>3.0.2</version>
      <executions>
        <execution>
          <id>default-copy-resources</id>
          <phase>process-resources</phase>
          <goals>
            <goal>copy-resources</goal>
          </goals>
          <configuration>
            <overwrite>true</overwrite>
            <outputDirectory>${project.build.directory}/${project.artifactId}-${project.version}/</outputDirectory>
            <resources>
              <resource>
                <directory>${project.basedir}/angular/dist</directory>
              </resource>
            </resources>
          </configuration>
        </execution>
      </executions>
</plugin>

And then you can use hot reload feature (while developing) on nodejs server, runned by ng serve with Angular CLI tool. 然后,您可以在nodejs服务器上使用热重新加载功能(在开发时),通过使用Angular CLI工具的ng serve

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

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