Thursday, July 13, 2006

Spring Experiences - 3 (Setup)

Assuming you have used eclipse as your IDE, lets now have a set-up which would help us launch our application and test it without any hassle.
Lets say, our project root folder is: SpringTraining and we aim to build MVC based webapp.
Then using eclipse->New->Folder, we may have our application structure as:
  1. SpringTraining/src [All source code and packages].
  2. SpringTraining/src/edu.springtraining.services [All services pakcage i.e. service interfaces (model) would go in here]
  3. SpringTraining/src/edu.springtraining.db.orm [All hibernate package - reverse engineering code generated *.hbm.xml + *.java]
  4. SpringTraining/src/edu.springtraining.db.dao [All DAO, which would contain access code and business logic would go in here].
  5. SpringTraining/src/edu.springtraining.mvc.controller [All controller code package]
  6. SpringTraining/webapps [Actual deployment directory].
  7. SpringTraining/webapps/training/ [deployment root]
  8. SpringTraining/webapps/training/WEB-INF [deployment contents container]
  9. SpringTraining/webapps/training/WEB-INF/web.xml [web.xml for our webapp container i.e. Jetty in this case].
  10. SpringTraining/webapps/training/WEB-INF/training-servlet.xml [Spring MVC wiring xml file].
  11. SpringTraining/webapps/training/WEB-INF/training-services.xml [Spring services xml, i.e. actual application xml]
  12. SpringTraining/webapps/training/WEB-INF/training-data.xml [Spring data access layer wiring].
  13. SpringTraining/webapps/training/WEB-INF/classes [the compiled byte-code]
  14. SpringTraining/webapps/training/WEB-INF/lib [all needed jars]
  15. SpringTraining/webapps/training/WEB-INF/jsp [all jsp files would go in here]
Note, for all WEB-INF/*.xml, you may also make them go under a directory like WEB-INF/conf/*.xml

To have jetty deploy or launch your application you need to do simply change in jetty's folder:
etc/jetty.xml
root



/projects/SpringTraining/webapps/
/etc/webdefault.xml
true
true


Note: The "projects" is a new folder under jetty-root folder and SpringTraining is a symlink (*nix) or short-cut folder (windows) to our actual SpringTraining folder.

With above structure in place, we can see, how MVC based development can start. You might make further changes to the packages, as you wish like, edu.springtraining.services might as well go under edu.springtraining.mvc.model.services ........ but as long as you get the big-picture and the whole idea, it suffices, rest is personal choice or maybe application requirements based.

Now you have spring-framework webapp MVC set-up ready!