Application Development – Spring MVC – Source


I tried putting up a Spring MVC based application from scratch – means standard tomcat 6, created ant builds and try how easy it is to build a ground up application using SpringMVC.

First. Spring MVC Project:

Add Necessary JARS on the Library Folder (Classpath / Buildpath).

Then, integrate Spring by calling the Spring Context Loader passing the xml files where all Beans are specified. Also call the ResourceResolver Servlet so that we can programmatically manage all URL’s coming in and out of the system.

 

 

The ContextLoaderListener is a Listener Object that is primarily used to pre-load objects needed for the application to run. For this example, we are passing an XML file full of Beans and we will allow the Listener to instantiate them on the application server pool.

The DispatchServlet is what we will use to control our URLs of the application, we need to protect our URLs from attacks and outside injections, this object will allow developers to put some barriers (naming) between the client and the actual page directory.

Second. Integrate it with Template, using Tiles:

Copying all JSP files to create a consistent look and feel is just not efficient, I find tiles and jsf templating very easy to use and learn. I have chosen to use Tiles in this project to showcase its re-usability (you can use tiles on any Web Application Framework – as it only requires you to include a class and call it via filter, servlet or listener).

Add Necessary JARS on the Library Folder (Classpath / Buildpath).

Then, the Actual Call to integrate Tiles Templating (for more info visit: http://tiles.apache.org/)

I find Tiles very flexible and easy to learn. You can define your template profiles and configuration on a single XML file.

Third Step: View – Layer (JSP)

The Resolve declartion above is using the properties file called view.properties:

 

 

A property file where the actual location of the JSP is indicated – and mapped to a name (login).

JSP Page:

 

 

Third Step: Controller – Layer

Spring’s controller framework is very flexible and simple. Developers can us the ResourceBundleResolver to protect URLs and use 3 kinds of Controller Object for transactions.

Controller Class: Calls the “login”

The controller doesn’t know the actual page value, its just knows the name associated to the page.

Fourth Step: Service / Model – Layer

GoF = Program to an Interface, not to an implementation – to hide the actual implementation from the client.

Hiding the actual implementation can be done using Autowiring and Autowiring can be done by putting an @Autowired annotation on your service field or property call:

 

I didn’t do any Database Call here yet, but the same process apply, create a new DAO layer call, put it on the XML file and use Springs Autowiring from the Service layer to access it.

Special Note: Put <context:annotation-config /> on xml that has beans that calls layer calls – Service to Dao Calls or Controller to Service Calls).

After all is setup (View to Controller to Model), Build your application and run it on any J2EE Complaint Container. 🙂

Fifth Step: Build WAR and deploy!

Click here to download the sample project.

Note: The Application is a bit large since I put all the Library and other resource on the WAR. By practice, I believe putting some resources outside the archive is very beneficial to the deployment since it reduces the extraction load from the server and optimize the size of the application archived.

Enjoy!

One thought on “Application Development – Spring MVC – Source

  1. Pingback: JavaPins

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s