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

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

问题描述

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

如图所示,它适用于 Angular 1.x,但 Angular 2 的文件结构完全不同,它的组件是驱动的,我使用的是 angular 2 文件结构,如下所示

我搜索了很多,我发现我们可以分别使用前端(使用 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. 创建普通的动态网络项目.
  2. 添加 spring 或用户 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,

您需要根据您的主机 url 放置 webServiceEndPointUrl.例如,如果您在 localhost:8080 上运行应用程序,则需要保留 url

webServiceEndPointUrl="

我知道使用这些方式在单个服务器上运行应用程序有很多缺点,但如果必须这样做,您可以遵循此方法.

如果你在angular方面有任何改变,你需要一直复制paste 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天全站免登陆