Integrate Spring4MVC With Angular on One Server: Easy Tutorial

Easy and simple trick to run angular and springMVC on single server.6 min


learn angular beingcoders

TL;DR – Learn how to run Spring4MVC with Angular on a single server. Follow our step-by-step guide for seamless integration.

No perfect solution till now ? thats fine! I got solution. I will show you How I did!

Note: This trick will work for Angular 2/4/5/6/7/8/9/10/11 versions.

You need 2 contexts as frontend and backend.

  1. Angular 2 project 
  2. Spring MVC

Follow below steps to achieve our main goal, to run SPRINGMVC + Angular 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

Use ng build –prod for production build.
This command will create a “dist” folder, copy all files including all folders.

4. Paste those files and folders into WebContent directory.
5. Last thing, you need to change basehref=”./” in index.html.

It contains details about File structure for Spring4MVC with Angular.

– 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.

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 build and copy-paste dist folder all time and then you can deploy it!

Visit : Run Spring4MVC with Angular 2 on a single server, I have mentioned file structure

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!

Download or clone example : Github, You can fork and star repository.


#Edit-1 (24th Mar, 2017):

In Angular, You can keep basehref=”./” by default. While building project (as dev/prod build) it will keep ./ in index.html as it is. Previously it was not working like that thing.

If you are running your angular app on lets say localhost:4200 then it will refer basehref=”/” you can find basehref attribute in src/app/index.html . Now if you want to run this with springMVC (normal configurations * pattern), you will put all files of dist folder to webapps or WebContent folder. Here you need to say from where spring can start rendering. [(dot-slash)./ means the current directory] [(slash) / is the root directory]. If you have another configurations, you may skip it. In general architecture I have tried and implemented successfully. Even you can try with / and ./


If you find joy and value in what I do, please consider supporting my work with a donation — however much you can afford, it means and helps more than you can imagine.

You can give tips too using Buy me a coffee.

Note: This trick will work for Angular 2/4/5/6/7/8/9/10/11 versions.

You may also like,

adsense


Discover more from 9Mood

Subscribe to get the latest posts sent to your email.


Like it? Share with your friends!

What's Your Reaction?

Lol Lol
0
Lol
WTF WTF
0
WTF
Cute Cute
0
Cute
Love Love
1
Love
Vomit Vomit
0
Vomit
Cry Cry
0
Cry
Wow Wow
1
Wow
Fail Fail
0
Fail
Angry Angry
0
Angry
Rakshit Shah

Legend

Hey Moodies, Kem chho ? - Majama? (Yeah, You guessed Right! I am from Gujarat, India) 25, Computer Engineer, Foodie, Gamer, Coder and may be a Traveller . > If I can’t, who else will? < You can reach out me by “Rakshitshah94” on 9MOodQuoraMediumGithubInstagramsnapchattwitter, Even you can also google it to see me. I am everywhere, But I am not God. Feel free to text me.

0 Comments

Leave a Reply

Choose A Format
Story
Formatted Text with Embeds and Visuals
List
The Classic Internet Listicles
Ranked List
Upvote or downvote to decide the best list item
Open List
Submit your own item and vote up for the best submission
Countdown
The Classic Internet Countdowns
Meme
Upload your own images to make custom memes
Poll
Voting to make decisions or determine opinions
Trivia quiz
Series of questions with right and wrong answers that intends to check knowledge
Personality quiz
Series of questions that intends to reveal something about the personality
is avocado good for breakfast? Sustainability Tips for Living Green Daily Photos Taken At Right Moment