Wednesday, October 13, 2010

Spring 3.0 - Where to Start?

Last time I touched spring it was much more cumbersome and I remember following a decent step by step guide ( Spring 2.5 step by Step) to get through starting my first project. Surprised to find that this guide is apparently not updated and is now inconsistent with Spring 3.0 :/.
I searched in vain to find an updated guide but most out there didn't seem right and often felt hackish in some parts as well. I ended up finding a much better way to get started with Spring - the Spring Tool Suite.

If you are new to spring I would highly recommend  Spring Tool Suite . It is an eclipse based IDE with many plugins and includes tc Server (tomcat server with some spring stuff on top). What was mostly helpful was the Spring Template Wizard (New->Project->Spring->Spring Template Project) Which gives you a working Spring app following some sort of template.

I had a Working Spring MVC within seconds and did not have to configure anything the web.xml and various servlet-context xml were configured with a simple view and a Controller giving me a great starting point for an MVC application.In addition with the bundled tomcat no application server configuration needed either it just works!

STS - Spring Template Project creates a working spring program in of the following templates

However it still not all smooth , not going to blame spring here but learning the basics of frameworks can be tough when you need a bit to just get started here's a couple I've come across so far that actually have had obvious answers.


Using Spring and can't access Cascading Style Sheets (CSS) or JavaScript(.js) files from JSP or URL.
I have found this is related to the servlet mapping configured in your web.xml. If your appServlet has a url pattern like "/" or "/*" then it will match all URLs in your application including calls to css files and your public files will not be visible.

As you can see from my folder structure I have a resources folder in my webapp directory which is outside of WEB-INF and should be accessible. but the url mapping prevents the requests going through.

There are different ways to solve this issue but the one I used I found on StackOverflow. Which was basically to add some URL patterns to the default Servlet this way i could make sure images/js/css files properly served as files/resources.



No comments:

Post a Comment