Thursday, June 30, 2011

Spring 3 project creation, the easy way

Intention : In this article I will demonstrate easy way to create spring 3 project. This project is very basic project. However it contains commented configuration for database connection, MySQL jdbc driver and all other jars for a basic spring 3 MVC web app. However it does not include library for tiles or sitemesh.
Download : Download spring-minimal archive eclipse project first.


Steps
  •  Choose Import Existing Projects into Workspace to import this project.

  • Choose Select archive file in the Import Projects option.  
  • Build the project
 
  •   Run the project on the server (You can now choose your local installation of apache tomcat to run the project).   

  • You should have following screen at your browser after the project is running.
 
  • Terminate the project. Now it's time to create a new project using this project.   
 

  • Don't forget to remove  the launch (javaw in windows, java in linux)

Rename the project to your desired name using File -> Rename (F2) or from right click menu at project node. Files affected by this rename are .settings/org.eclipse.wst.common.component, .project and web.xml. See them they should reflect new name.
 
  •  If you run the project now, it will run because there has been no change on the class file and the project does not have to compile anything. But if you build and try to run the project you can have 404 (page not found)


  •  Remove the project from tomcat from servers after terminating and removing project launch and delete build files using navigator window  

  • Now you can remove the project from the context of this run. Try delete from project node (Read next step before doing anything silly). 
 
  • Don't check Delete project contents on disk (cannot be undone

  • Import the project again (existing projet). This time select root directory and uncheck copy projects into workspace. 



  •  Try to run the project once again after building the project and run the project. It should now run. 



Troubleshooting : If it does not run this time don't worry there is no problem with the project. Try to close the project from the project node and open again and try to run again. Or unload and load projects some more times. Try to terminate the project and launch. Delete project from tomcat server etc. But it will run eventually.

  • Conclusion : You can make a base spring project template for all your spring projects. Be careful not to choose additional nuisance, less is always more when it comes to having the nuisance of removing may components later. Make it minimal. However if you need to work with mysql (jar is included) make change in jdbc.properties to reflect your database url and un-comment lines from spring-servlet.xml to allow different parts to work together. In that file messageSource points to resources/messages.properties (if the project does not recognize this file, choose to include this directory as source directory). Then we have propertyConfigurer for jdbc.properties file. Then database and session factory. The node transactionManager is used to work on transactions on session layer. Use the blank packages to organize your project. You may rename the package to the name you need. Make sure you change them accordingly in spring-servlet.xml too.
















    No comments:

    Post a Comment