Friday, December 27, 2013

Two Spring MVC tutorial projects - Spring 4, Eclipse, Tomcat, multiple models, MultipartFile

Today I present two "hello world"-type Java EE projects using Spring MVC. I've been getting up to speed with Spring for a while now. I've taken advantage of several helpful tutorials, but I found all of them lacking in one way or another.

First, although there are quite a few Spring MVC "hello world" tutorials, I couldn't find one that accommodated all of my requirements, so I created the springmvcshell project. It's based on mkyong's excellent Spring 3 MVC Hello World Example, which I modified to:
  • Use the Maven standard directory structure
  • Work with the Eclipse IDE
  • Work with Tomcat
  • Use Spring 4 rather than an earlier version
  • Use Java 1.7 rather than an earlier version
Second, I wanted to create an MVC view and corresponding controller method that include fields from two models plus a multipart file upload in a single form. Again, I found several relevant tutorials, but none that met all my needs. So I created the springmvcgoodness project, which provides all the features of springmvcshell, plus:
  • Uses a single form to read and write fields from two separate classes
  • Includes a file upload control using Spring's MultipartFile interface
The README files in the springmvcshell and springmvcgoodness repos explain how to install and run the two projects. You can of course see all the source code in those two repos. The remainder of this post discusses a few interesting points about the code and settings.

First of all, the Maven pom.xml files for the springmvcshell and springmvcgoodness projects are of critical importance. Both pom.xml's include the dependencies for Spring 4.0.0, as well as a reference to the com.springsource.repository.maven.snapshot repository that's required for Maven to find these dependencies. The springmvcgoodness pom.xml also adds dependencies for  the Java Server Pages Standard Tag Library (JSTL), the Java Persistence API, and Apache Commons FileUpload.

Next, the project settings are critical to making these projects work with Eclipse and Tomcat. If you clone my springmvcshell and springmvcgoodness repos, you'll automatically have the correct settings, which are stored in the .classpath and .project files. However, it's worth understanding some of the details.
  • I converted these projects to faceted form (right-click the project and select Configure | Convert to Faceted Form) and added the Dynamic Web Module facet version 3.0. This makes the projects usable as servlets.
  • The Java Build Path is key to avoiding compile-time errors due to missing dependencies. I had to add some libraries to the build path (right-click the project and select Build Path | Configure Build Path, then click the Add Library button). I had to add the EAR Libraries. To eliminate some compiler warnings, I also removed the old version 1.5 JRE System Library and replaced it with the JRE System Library [jre7].
  • The Deployment Assembly settings are key to successful deploying on Tomcat and are accessed by right-clicking the project, selecting Properties, and then clicking Deployment Assembly. It's critical that the Maven Dependencies be added here if not already present.
Finally, here are a few points about the springmvcgoodness project:
  • The approach I took to including fields from two models in one form was to create a wrapper class containing an instance of each model class.
  • Once you have such a wrapper class, this view shows how to set the commandName and path attributes to refer to fields of the two model classes.
  • Keys to making the file upload work include:
I hope you'll find these example Spring MVC projects helpful!