A Fix to Our Spring-MVC Course

Just a quick blog post to alert that there is an error in one of the config files in the Spring-MVC course. I’m uploading a fixed version now, so if you’re a user of the course, please download the new “Practicals and Code” folder, or follow these instructions:

If you start your project from the “Starting Code” folder, our web.xml contains the following:

{!{code}!}czo1MzA6XCIgICAmbHQ7IS0tICB3ZVwndmUgYWxyZWFkeSBjb25maWd1cmVkIFNwcmluZy1NVkMgZm9yIHlvdSAtIG5vdGhpbmcgdG8ge1smKiZdfWRvIGhlcmUhIC0tJmd0OyAgICAgICAgIAogICAgJmx0O3NlcnZsZXQmZ3Q7CiAgICAgICAmbHQ7c2VydmxldC1uYW1lJmd0O0Zyb257WyYqJl19dENvbnRyb2xsZXImbHQ7L3NlcnZsZXQtbmFtZSZndDsKICAgICAgICZsdDtzZXJ2bGV0LWNsYXNzJmd0O29yZy5zcHJpbmdmcmFtZXtbJiomXX13b3JrLndlYi5zZXJ2bGV0LkRpc3BhdGNoZXJTZXJ2bGV0Jmx0Oy9zZXJ2bGV0LWNsYXNzJmd0OwogICAgICAgJmx0O2xvYWQtb24te1smKiZdfXN0YXJ0dXAmZ3Q7MSZsdDsvbG9hZC1vbi1zdGFydHVwJmd0OwogICAgJmx0Oy9zZXJ2bGV0Jmd0OwogIAogICAgJmx0O3NlcnZsZXR7WyYqJl19LW1hcHBpbmcmZ3Q7CiAgICAgICAmbHQ7c2VydmxldC1uYW1lJmd0O0Zyb250Q29udHJvbGxlciZsdDsvc2VydmxldC1uYW1lJmd0O3tbJiomXX0KICAgICAgICZsdDt1cmwtcGF0dGVybiZndDsqLmRvJmx0Oy91cmwtcGF0dGVybiZndDsKICAgICZsdDsvc2VydmxldC1tYXBwaW5ne1smKiZdfSZndDsKXCI7e1smKiZdfQ=={!{/code}!}

However, our Spring XML config file is called “Dispatcher-servlet.xml”.

By default, Spring finds it’s config file in a web app by taking the servlet name and appending “-servlet.xml” to the end – it then looks for a matching file in /WEB-INF.

If you downloaded the course before the date of this post, please rename the servlet name to Dispatcher (it appears twice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Q7CiAgCiAgICAmbHQ7c2VydmxldC1tYXBwaW5nJmd0e1smKiZdfTsKICAgICAgICZsdDtzZXJ2bGV0LW5hbWUmZ3Q7RGlzcGF0Y2hlciZsdDsvc2VydmxldC1uYW1lJmd0OwogICAgICAgJmx0O3VybC17WyYqJl19cGF0dGVybiZndDsqLmRvJmx0Oy91cmwtcGF0dGVybiZndDsKICAgICZsdDsvc2VydmxldC1tYXBwaW5nJmd0OwoKJmx0Oy93ZWItYXtbJiomXX1wcCZndDsKXCI7e1smKiZdfQ=={!{/code}!}

Many apologies for the inconvenience this has caused – it was entirely my fault and happened because I changed my naming convention between chapters.

Thanks to David Wubshet and Benjamin Quarless amongst others for flagging up this problem.

Integrating Spring MVC and a “Real” Database

On the Spring MVC and WebFlow video training course, we build a system from scratch based around a book store. To keep things focussed on the web tier, we use a mock implementation of a service class.

Therefore, we can run the system up without worrying about setting up a database, getting the tables set up etc.

However, I’ve had a few requests from people who are stuck on “upgrading” their Spring MVC project that uses the Mock. They want to talk to a back end database but for some reason it isn’t working.

The best way to work is to start from your Spring-MVC project, and make sure that is working. As on the course, you’ll probably have a Dispatcher-servlet.xml file with the web-tier beans, and a separate application.xml containing the “middle-tier” beans.

Now write a production version of your service class, in the same style as on the fundamentals course (for example, service class calls a dao which uses a Spring template to read/write to the db).

As all of the above is covered in both courses, it’s just a case of integrating your knowledge.

There are a couple of steps you might need to follow however:

1: Add in missing Jar files

Add in any JAR files that you need for your data access strategy. I used Hibernate 3 (the old XML mapping style, not annotations), and I added:

antlr-2.7.6.jar
dom4j-1.6.1.jar
hibernate3.jar
hsqldb.jar
javassist.jar
jta.jar

If you’re using Hibernate with Annotations, or MyBatis or whatever, your dependencies will be different. I simply borrowed these JARs from the project I created on the Spring Fundamentals course.

Step 2: Ensure your mapping files are copied across (Hibernate XML only)

Now for an annoying complication: I’m using the old fashioned but still usable XML mapping approach to Hibernate. I have a file called Book.hbm.xml in my package “com.virtualpairprogrammers.domain”.

However, this file is not being picked up by the build script we used on the Spring MVC course. When Eclipse compiles, it copies any non-Java files from the /src folder structure, across to the corresponding folder under /bin.

The ant compile task does NOT do this, however. So I need to manually add this step into my build.xml.

<!-- ensure hibernate XML files are also moved in the compilation -->
<copy todir="${bin}">
   <fileset dir="${src}">
      <include name="**/*.hbm.xml"/>
   </fileset>
</copy>
Note: this is added inside the compile target.

I’ve uploaded a full sample project to http://vpp-misc-files.s3.amazonaws.com/MVC%20Project.zip. These sample files will only be available for a short period, so grab them while you can.