Spring MVC with Apache Maven


After a long long time. While you are learning some other stuffs, I am going to catch your eye with Spring framework. As I said in my first post, this is the time to move with Spring which is the one of most popular Java related frameworks. I don't use my time to give you any overview about Spring framework. This can be got using following rich links (Just google). In this post I'm going to create your first Maven based project using Spring Tool Suite. First of all you need to install Maven in your machine.

I'm not going to give you theory parts, because you can google it and find so many resources about these theories. Here I'm going to give you hands-on experience when you are developing. Once you open STS (Spring Tool Suite, I'm using 3.6.3.SR1 version), following interface will be appeared. You can download Spring Tool Suite from http://spring.io/tools/sts/all or you can change your existing Eclipse IDE into Spring tool suite using Eclipse Market Place. 







Before create a Maven project...


First of all you need to  rebuild Maven indexes. Maven consist of local and global repositories. So your first step should be rebuilding these two repositories. How to do it ?

Follow this path

Window -> Show view -> Other -> "type Maven" -> "select Maven Repositories"





Now you can see Maven Repository view.


Before rebuild indexes...



  • Delete existing .m2 folder, this folder can be found in C:\Users\{User name}





  • If you are using a proxy server, please check whether that your proxy allows you to download. If it is not downloading, you have to use other connection to download .
  • Right click on the Global Repository and select Full index enabled. 



Now you are ready to rebuild indexes.



  • Right click on the local repositories and click rebuild indexes.
  • Then right click on the global repositories and click rebuild indexes.
This gets a lot of time to rebuild indexes. Please wait until if finished. If it doesn't give an error, you successfully rebuilt Maven indexes.


You are ready to create your first Maven project


  • Right click on Package Explorer 
  • New -> Other -> "Type maven in text filed" -> Select Maven Project -> Next

Skip archetype selection




Enter a Group Id, Artifact Id and select "war" from Packaging. (You need to select war, because you are going to create a web project)



Your project will appear like this.





STOP !


You need to configure few things first.


  • Change Java version of the project
  • Change Dynamic Web Module version of the project, How to do this ?



Configure the project



  • Right click on the project (Or Alt + Enter) to go to project properties.
  • Select Project Facets 
  • Do the changes (I use Java version 1.8 and Dynamic web module version 3.0)




Then right click on the project -> Maven -> Update project or (Alt + F5)

Now you are ready.


web.xml file


web.xml file is a Deployment Descriptor for the project (Google for more info). In STS you can generate it simply.

Right click on the project -> Java EE Tools -> Generate Deployment Descriptor Stub




Then your web.xml file will appear like this.




 Please check the version of web.xml file. Otherwise your project won't work.



If the version is still 2.5, then delete web.xml file and update the project (F5) and update Maven (Alt + F5). Then again generate web.xml file


Create first JSP page


  • Right click on the project -> New ->Other
  • search for jsp
  • Select jsp and give the file name as "index.jsp"
  • Click finish



You can see there is red marked things looks like errors. Forgot it for a moment.

Just type something in index.jsp file to identify the sample project is working or not. Then right click on the project,

Run As -> Run on server and select your server. (I'm using Apache tomcat 7.0 or 8.0 server) I assume that you know how to configure a server in a IDE like Eclipse. 



When it running, you will able to see your message on your browser that you have put on index.jsp file. If you can see this, you are successfully created simple Maven project. In my next post lets add Spring dependencies to our project.





Spring MVC with Apache Maven Spring MVC with Apache Maven Reviewed by Ravi Yasas on 11:32 PM Rating: 5

No comments:

Powered by Blogger.