Saturday, February 9, 2013

Hello world spring MVC application

Through this post I have tried to provide a step by step guide to build a Spring MVC web application using  eclipse IDE(juno).I haven't explained the theories of  spring or J2EE .‘cause I wanted to manage this post very short. For a detailed description of the theories please refer to following sources.
  1. Spring MVC documentation.
  2. Maven documentation.
Pre-request

In-order to continue building the project you have to have m2e eclipse plugin installed which is available at eclipse market place.

And also you need to have a sever installed which is capable of previewing J2EE web applications, here I am going to use Apache Tomcat 7.

Step by Step guide.

Firstly we need to install spring plugin for eclipse IDE. Go to eclipse market place (Help>Eclipse Market Place) and search for spring tool suit.And choose the appropriate version which will match with your IDE version.


Fig 1 : Spring tool suit from eclipse market place 

Note
Some of spring tool suit features require GEF(Graphical Editing Framework). So make sure that you have enabled eclipse update site in the IDE in case you don't have installed GEF.To add eclipse update site go to  Help > Install new Software. Then add the eclipse update link,in my case the link for the update site is http://download.eclipse.org/releases/juno/ .

My intention is not to use all the features of spring tool suit so I have just installed spring IDE core and spring IDE maven support.In Linux environment you might have to launch eclipse in sudo mode before installing the required plugins.

Fig 2 : Components needed to installed for spring IDE maven support

Before continuing to the next section, make sure you've got installed all the required plugins successfully.

Creating the project

File > New > Other.. From the first window of the wizard choose Spring > Spring Template Project




Since we are going to build a spring MVC application , choose spring MVC Project from the next kwindow.



In the next window you can provide a project name and a top-level package.


After finishing the above steps you could get a project structure seems like below.

initial_project_structure

Now we can run our web application on a selected server.

First we have to build the web application using maven.Right click on the project > Run As > Maven install.After the successful build of maven you can run the web application.Either you can deploy the generated war file directly to Tomcat web container or you can run it from eclipse.


Using Tomcat 7.

To directly run the web application through IDE we have to copy the dependencies to a lib folder.So we have to alter the project structure as below.We have to manually create a lib folder in src>main> webapp > WEB-INF .

project_structure


Now we are ready to launch the application.Right click on the project > Run As > Run on server.If you not have configured a server instance you will directed to create a new server instance.

setting_up_server


In this project I am going to use Apache Tomcat 7.0.Next few steps are pretty straight forward.For the sake of  completion below I have provided the screen shots of the configurations of my server instance.


tomcat_path


Congratulations that’s it.Now we got a spring web application up and running.You might get a tab appeared like below in eclipse.

hello_world

If you are a newbie with J2EE application deployment you might face some errors while the deployment process.Most common errors would be time out errors or port already assigned errors.A solution would be to configure the server parameters. You can change the parameters through the ui provided by eclipse.

Using Jetty plugin

Jetty  is a light weight server capable of previewing J2EE applications.The advantage of using jetty plugin is that we do not want to manually copy the project dependencies every time we install our application.

you just need to add the following plugin to project pom file under the plugins section.

 Then you can run the project by Run>Run As > Maven Build .From the window fill the goals as jetty:run
For more details about the jetty plugin please refer to Maven jetty plugin .

So that's it for the helloworld spring web app.And hope you will be able to get it up and running.I will explain how to build a NLP engine using spring web framework in another post.

Note :  You can download the project zip file through this link.