在单个服务器上运行带有Angular 2的Spring4MVC [英] Run Spring4MVC with Angular 2 on a single server

查看:119
本文介绍了在单个服务器上运行带有Angular 2的Spring4MVC的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是angular2的新手,我想知道具有角度2的SpringMVC4的可能文件结构是什么?





我搜索了很多,我发现我们可以使用前端(使用angular2) )和后端(服务器 - 使用spring / springboot)分开,但我们需要2个服务器来运行应用程序。
例如,前端:192.168.100.1:4200
和后端:192.168.100.1:8080



那么有什么方法或一般文件结构在同一台服务器上运行angular2和spring4MVC(如192.168.100.1:8080)?



提前致谢。答案将不胜感激!

解决方案

直到现在还没有答案那很好,我得到了解决方案。我是怎么做的?



你需要2个上下文。



(1)Angular 2项目和



(2)Spring MVC



按照以下步骤实现我们的主要目标,在单个服务器上运行SPRINGMVC + Angular2。


  1. 创建正常动态Web项目。

  2. 添加spring或user maven pom.xml所需的所有依赖性

  3. 打开CMD,导航到angular2应用程序。点击命令



    npm install 然后



    ng build 或使用 ng build --prod 进行制作


此命令将创建一个dist文件夹,复制包括所有文件夹的所有文件。


  1. 将这些文件和文件夹粘贴到WebContent目录中。


  2. 最后,您需要在index.html中更改basehref =./。现在您已准备好运行服务器,或者您可以部署war文件并使用tomcat或其他服务器提供服务。


如果您正在使用Rest webservices并希望在单个服务器中运行angular2和spring,



您需要输入 webServiceEndPointUrl 根据您的主机网址。例如,如果您在localhost:8080上运行app,则需要保留url



webServiceEndPointUrl =



我知道使用这些方法在单个服务器上运行应用程序有许多缺点,但是如果它必须要求你可以遵循这个。



如果你在角边改变任何东西,你需要一直复制粘贴dist文件夹然后你可以部署它!


I am new to angular2, I want to know what is the possible file structure for SpringMVC4 with angular 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

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. For example, Frontend : 192.168.100.1:4200 And Backend : 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)?

Thanks in advance. Answers will be appreciated!

解决方案

No answers till now thats fine, I got solution. How I did ?

You need 2 contexts.

(1) Angular 2 project and

(2) Spring MVC

Follow below steps to achieve our main goal, to run SPRINGMVC + Angular2 on a single server.

  1. Create normal Dynamic web project.
  2. Add all dependancy required for spring or user maven pom.xml
  3. Open CMD, navigate to angular2 application. Hit command

    npm install and then

    ng build or use ng build --prod for production

this command will create a "dist" folder, copy all files including all folders.

  1. Paste those files and folders into WebContent directory.

  2. Last thing, you need to change basehref="./" in index.html. Now you are ready to run server or you can deploy war file and serve it with tomcat or other servers.

If you are using Rest webservices and want to run angular2 and spring in a single server,

You need to put webServiceEndPointUrl as per your host url. For example, If you are running app on localhost:8080, you need to keep url

webServiceEndPointUrl= "http://localhost:8080/api/user"; at angular side. After that you can build and copy paste to your WebContent folder.

See below Image, File structure for 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!

这篇关于在单个服务器上运行带有Angular 2的Spring4MVC的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆