Spring MVC 3 Showcase

Since the big Spring 3 release last year, I've been working on a number of application development projects and extracting "showcases" of various framework features. These "showcases" are not reference applications or tutorials, they're more like acceptance tests for specific framework capabilities. After seeing a showcase, you should have a good idea of what the technology can do.
The first showcase I've put together is for Spring MVC 3, our web framework. It includes a sample project, along with a supporting slide presentation and screencast. After digging in, you should have a good understanding of what Spring MVC can do and get a feel for how easy it is to use.
mvc-showcase
In this showcase you'll see the following in action:
- The simplest possible @Controller
- Mapping Requests
- Obtaining Request Data
- Generating Responses
- Message Converters
- Rendering Views
- Type Conversion
- Validation
- Forms
- File Upload
- Exception Handling
I use jQuery to drive feature illustration by applying progressive Ajax techniques. I highly recommend using Firebug or equivalent to gain insight into the client/server interactions. I find it very educational to inspect the request and response details from the client-side, then jump to the code that executed on the server-side.
Get the code by checking out the mvc-showcase project from our spring-samples repository. Simply import the project into STS and drag-n-drop the project onto the "SpringSource tc Server Developer Edition" Server to run.
View the supporting slide show:
See the showcase live in this 8 minute screencast:
The mvc-showcase screencast contains music by the band The Smashing Pumpkins. The mvc-showcase screencast is not affiliated with or endorsed by The Smashing Pumpkins. All audio in the screencast is licensed for non-profit use per the terms and conditions of the live music archive at archive.org.
Summary
I hope you find mvc-showcase useful and discover some "hidden gems" you might not have known about before. Please let me know also if you'd like to contribute your own additions. mvc-showcase covers a lot of ground today, but there's always opportunity to cover more!
Similar Posts
- dm Admin Console Demo Screencast
- Configuration Properties Screencast
- Introduction To Spring Roo Screencast
- Spring Framework: The Origins of a Project and a Name
- SpringSource Seminar Day Linz in Review


David MARTIN says:
Added on July 22nd, 2010 at 2:06 pmHi,
I'm gonna download this source code. It's always nice to have a showcase to illustrate how to implement typical things. Thank you for providing us this. I'm sure it will be helpful.
David
Ben says:
Added on July 22nd, 2010 at 8:04 pmNice sample project!
However, what's with the 'resources' element labeled with "Handles HTTP GET requests for /resources/** by efficiently serving up static resources in the ${webappRoot}/resources directory" found in servlet-context.xml?
Keith Donald (blog author) says:
Added on July 22nd, 2010 at 10:10 pmBen,
Good observation. The <mvc:resources/> tag is a new feature coming in Spring Framework 3.0.4. I used it in this sample so I could achieve clean URLs without having to add a dependency on urlrewrite. Specifically, all requests into the app are routed through the Spring MVC DispatcherServlet (note the "/" mapping in web.xml). This includes requests for static /resources. When a static resource is requested, the ResourcesHandler registered by the <mvc:resources/> tag serves it up.
Keith
Daniel K says:
Added on July 23rd, 2010 at 1:31 amKeith,
I suspect Ben mentioned the resources element because it gets flagged when validated. In Eclipse I get an error, "cvc-complex-type.2.4.c: The matching wildcard is strict, but no declaration can be found for element 'resources'.", at line 15 of src/main/webapp/WEB-INF/spring/appServlet/servlet-context.xml".
Is there a public XSD we can point to that includes this as yet unreleased version of Spring? Or can we just comment it out since it is empty anyway?
Dan
Daniel K says:
Added on July 23rd, 2010 at 2:03 amI should add that once deployed when I hit http://localhost:8080/mvc-showcase/ the page renders without all the JQuery goodness, and logs the following in Tomcat:
WARN : org.springframework.web.servlet.PageNotFound – No mapping found for HTTP request with URI [/mvc-showcase/resources/jqueryui/1.8/jquery.ui.widget.js] in DispatcherServlet with name 'appServlet'
Surely related.
Dan
Jeffery Caldwell says:
Added on July 23rd, 2010 at 4:58 amThe following approach worked for me to view the mvc-showcase. Note I am using sts-2.3.3.M2 but I don't think tat should matter.
1) svn checkout from command line outside of sts.
2) import existing project – see validation error noted above…
3) right-click no the project and do a run-as "Maven Package" (or mvn package from cmd line)
4) go to the project properties dialogue on the pane for "Deployment Assembly".
5) remove all the rows that are there.
6) add a new entry with the Folder Mapping refernce type. Choose the targets war folder, i.e. – /target/mvc-showcase-1.0.0.BUILD-SNAPSHOT.
7) finally I just right-clicked on the project and selected run-as "Run on Server" and it started up with all he goodness on tc dev instance.
Maven handled pulling down the spring 3.0.4.BUILD-SNAPSHOT stuff, and I've got to say this does look nifty. Interesting that urlrewrite dependency is going away, I should have seen that one coming.
Great work BTW you spring guys are great.
Keith Donald (blog author) says:
Added on July 23rd, 2010 at 8:39 amGuys,
Deploying the mvc-showcase project should be as simple as importing the project into STS and dragging it over to the "SpringSource tc Server Developer Edition" Server. This is shown step-by-step in the screencast. If using an IDE is not your thing, you can also deploy from the command line by running "mvn tomcat:run".
Yes, STS does flag the <mvc:resources/> tag with a red marker, but this will not prevent the app from deploying properly. The reason STS flags the tag is because it refers to the version of the mvc namespace in its XML Catalog when validating the XML file. This version is different than the version of spring-mvc-3.0.xsd in the org.springframework.web.servlet.config package of the spring-webmvc-3.0.4.BUILD-SNAPSHOT jar file included in your classpath, which is what actually gets used at runtime. Once the Spring Framework 3.0.4 release is out and STS has updated its catalog, the error marker will go away. But, I promise, the issue is just a tooling thing and will not effect the actual application at runtime (unless you comment the tag out, Dan
)
Hope this helps. Great to see you guys digging into this!
Keith
Daniel K says:
Added on July 23rd, 2010 at 11:05 amKeith – Got things working. I just missed a step. Works nicely. Thanks.
Stephane says:
Added on July 23rd, 2010 at 3:13 pmQuick question, what tool did you use to record the showcase?
Reza says:
Added on July 23rd, 2010 at 4:00 pmThis is awesome. I wish you guys have similar showcase for your other projects like SFW and Spring Security!
Great Job.
Keith Donald (blog author) says:
Added on July 23rd, 2010 at 4:38 pmStephane,
I use ScreenFlow for the Mac. It's a cinch to use and I expect I will be doing more screencasts in the future with this tool.
Reza,
Thanks for the kind words. I'm sure we'll see showcases for other projects in the future. I'd love to see one for Spring Web Flow and Spring Security, too. I encourage you to contact the Spring team whenever you have some code you think will make a good showcase addition.
Keith
Reza says:
Added on July 23rd, 2010 at 4:42 pmOne more question,
What is the status of Spring MVC's form tags with HTML5?
For example, I would like to use 'placeholder' attribute in my text inputs.
Keith Donald (blog author) says:
Added on July 23rd, 2010 at 4:51 pmReza,
Yes, you can use HTML 5 and custom attributes with the form tags in Spring MVC 3.
Keith
Mary says:
Added on July 23rd, 2010 at 7:36 pmGreat to have this showcase app for reference!
Hacking a little at the app, I've come to suspect that Spring MVC 3 is not currently supporting remoting java exception object such that the jquery's ajax() error handler would receive a valid exception object from the server. Is that correct?
If so, I will just use the @ExceptionHandler pattern you provided in the app to package exception info in the response back to the browser.
Sascha T. says:
Added on July 24th, 2010 at 6:12 pmThis showcase is really recommended.
It features many facettes of Spring MVC which have been very difficult to find seperately. All the important pieces very well sorted and seperated in directories.
Thank you Keith and keep on the good work!
Amit Panwar says:
Added on July 24th, 2010 at 10:25 pmHi,
I have a requirement to get the error messages from the database against the form fields server side validation, rather then from the properties files.
Can anyone suggest some approach?
Thanks
Amit
YangL says:
Added on July 25th, 2010 at 7:40 amgood job
jjjjj says:
Added on July 25th, 2010 at 8:32 amquite late for for spring 3 showcase but very very nice! thank you
Raju PV says:
Added on July 25th, 2010 at 1:03 pmThanks Keith. It is really awesome. Pretty clear showcase. I believe you continue the same kind of showcases for your future projects also.Good luck
sunny says:
Added on July 26th, 2010 at 2:37 amBravo.thank you for your sharing!!!
redsonic says:
Added on July 27th, 2010 at 11:38 amThanks a lot, clean work.. awesome. Spring rocks!
Corey Reil says:
Added on July 27th, 2010 at 11:05 pmKeith
With the ability to perform simple request/reply with xml as the input and output (e.g. via the MarshallingHttpMessageConverter), would there be a reason to choose the Spring MVC way vs. exposing the same type of interface via Spring Integration?
Ryan says:
Added on July 28th, 2010 at 12:11 pmI'm getting a 404 for anything under the resources path. I did not comment out the resources tag. I tried both on command line using maven and in STS. For command line, here's the script to repeat:
svn co https://src.springframework.org/svn/spring-samples/mvc-showcase
mvn tomcat:run
wget http://localhost:8080/mvc-showcase/resources/form.css
–2010-07-28 13:08:23– http://localhost:8080/mvc-showcase/resources/form.css
Resolving localhost (localhost)… ::1, fe80::1, 127.0.0.1
Connecting to localhost (localhost)|::1|:8080… connected.
HTTP request sent, awaiting response… 404 Not Found
2010-07-28 13:08:23 ERROR 404: Not Found.
I get this in the logs, which makes it seem it did complete successfully.
DEBUG: org.springframework.web.servlet.DispatcherServlet – DispatcherServlet with name 'appServlet' determining Last-Modified value for [/mvc-showcase/resources/form.css]
DEBUG: org.springframework.web.servlet.handler.SimpleUrlHandlerMapping – Matching patterns for request [/resources/form.css] are [/resources/**]
DEBUG: org.springframework.web.servlet.handler.SimpleUrlHandlerMapping – URI Template variables for request [/resources/form.css] are {}
DEBUG: org.springframework.web.servlet.handler.SimpleUrlHandlerMapping – Mapping [/resources/form.css] to handler 'org.springframework.web.servlet.resources.ResourceHttpRequestHandler@1ef673cc'
DEBUG: org.springframework.web.servlet.DispatcherServlet – Last-Modified value for [/mvc-showcase/resources/form.css] is: -1
DEBUG: org.springframework.web.servlet.DispatcherServlet – DispatcherServlet with name 'appServlet' processing GET request for [/mvc-showcase/resources/form.css]
DEBUG: org.springframework.web.servlet.DispatcherServlet – Null ModelAndView returned to DispatcherServlet with name 'appServlet': assuming HandlerAdapter completed request handling
DEBUG: org.springframework.web.servlet.DispatcherServlet – Successfully completed request
If I just go to http://localhost:8080/, I do get the main screen but without any of the styles or javascript.
Am I missing something?
thanks,
Ryan
Jeremy Grelle (blog author) says:
Added on July 28th, 2010 at 12:29 pmRyan,
The resource handling infrastructure is still a work-in-progress (see http://jira.springframework.org/browse/SPR-7116). It's getting close to being finished, but the recent changes did break the sample. I've updated the sample to reflect the latest changes. Do an svn update and you'll be good to go.
Thanks,
Jeremy
Ryan says:
Added on July 28th, 2010 at 1:19 pm@Jeremy – An svn update fixed the issue. Thanks!
Jiyun Kang says:
Added on July 29th, 2010 at 1:55 pmAwsome Job! Thanks a lot, Spring Team
devdanke says:
Added on August 5th, 2010 at 12:27 ammust i install quicktime to watch this screencast?
why not put it in a more common video format, such as avi or flash?
given apple's anti-developer behavior of late, i won't put any of their software on my computer.
Jiyun Kang says:
Added on August 5th, 2010 at 5:10 pmDoes anyone know what makes this project runnable with just simple drag& drop down to tc server panel in STS?
Ben says:
Added on August 5th, 2010 at 6:25 pmre: JK
Not sure I understand the question. The "drap & drop" is just a short alternative to "right-click project > run on > server" where tc server is registered with STS as a server runtime. Perhaps adding the "Dynamic Web Project" facet to your project is what you're looking for.
Jiyun Kang says:
Added on August 5th, 2010 at 6:53 pmre: Ben
My question was how I can deploy the project to tc server like the demo screencast is doing here.
But your answer gave me hint to solve the issue.
It was version compatibility issue of the J2EE Web module specification.
* The server does not support version 3.0 of the J2EE Web module specification.
Thank you very much
Luis Serrano says:
Added on August 12th, 2010 at 2:51 amHi Keith,
Thanks a lot for these examples, they help understand everything a little bit more. I was wondering whether to find samples of portlet developments, specially how to test them within SpringSource.
I have found a good guide here:
http://www.manning.com/sarin/Sarin_MEAP_Ch1.pdf
And also a Hello World portlet:
http://books.dzone.com/articles/hello-world-portlet-using-Spring-3-portlet-MVC
But an "official", more extended explanation from an official would be greatly useful and appreciated.
BTW I loved the screencast soundtrack.
Cheers!
redsonic says:
Added on August 12th, 2010 at 3:03 amHi,
When using Google Chrome the result page is not displayed, and Chrome is telling me:
"Resource interpreted as document but transferred with MIME type application/xml."
How can I fix the content-type to: text/html (or text/xml for exemple)?
Thanks
redsonic says:
Added on August 18th, 2010 at 3:15 amSolution to my previous question in ResponseController.java
@RequestMapping(value="/response/entity/headers", method=RequestMethod.GET)
public ResponseEntity<String> responseEntityCustomHeaders() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_PLAIN);
return new ResponseEntity<String>("The String ResponseBody with custom header Content-Type=text/plain",
headers, HttpStatus.OK);
}
shriva says:
Added on August 18th, 2010 at 9:38 amnot able to download the project. getting error "could not connect to server" dont know what to do? any pointer would be appreciated.
C:\>svn checkout https://src.springframework.org/svn/spring-samples/mvc-showcase
svn: OPTIONS of 'https://src.springframework.org/svn/spring-samples/mvc-showcase': could not connect to server (https://src.springframework.org)
C:\>svn co https://src.springframework.org/svn/spring-samples/mvc-showcase
svn: OPTIONS of 'https://src.springframework.org/svn/spring-samples/mvc-showcase': could not connect to server (https://src.springframework.org)
Keith says:
Added on August 18th, 2010 at 6:45 pmNice showcase.
Has anyone found a workaround for the hard coded default character encoding in StringHttpMessageConverter?
If I am reading the code correctly, any @ResponseBody returned as a string will be encoded as ISO-8859-1 — which makes it unusable in many real world applications.
Marc Lang says:
Added on August 20th, 2010 at 2:58 amHi,
The project runs fine by dragging onto the tc server (this is my first look at springsource)
However, I am still getting the error in Eclipse to do with the resources tag.
How do I go about updating to resolve this?
I have done a help > check for updates, but this didn't resolve it.
I see that 3.0.4 has been released now.
Keith says:
Added on August 21st, 2010 at 8:14 pmA great enhancement to the showcase would be a @ResponseBody annotated method returning a string containing Japanese (or other high number unicode) characters.
Gerry Kaplan says:
Added on August 25th, 2010 at 11:45 pmI am also still getting the resource error. True, it executes fine, but for those of us who work in Eclipse, this makes a lot of red mess. Any idea when we can expect the resources tag not to be flagged as an error?
Bryan says:
Added on August 27th, 2010 at 8:19 amI am having issues starting the server, I keep getting the timeout after 120 seconds. I tried dragging and dropping the mvc-basic project and it started immediately. Is there something I'm doing wrong? The error I get is below.
INFO: Starting service Catalina
Aug 27, 2010 9:14:27 AM org.apache.catalina.core.StandardEngine start
INFO: Starting Servlet Engine: SpringSource tc Server/6.0.20.C
Aug 27, 2010 9:14:27 AM org.apache.tomcat.util.modeler.Registry registerComponent
SEVERE: Null component Catalina:type=JspMonitor,name=jsp,WebModule=//localhost/mvc-showcase,J2EEApplication=none,J2EEServer=none
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:592)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:290)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:415)
Caused by: java.lang.UnsupportedClassVersionError: Bad version number in .class file (unable to load class org.springframework.samples.mvc.flash.FlashMapFilter)
at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1894)
at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:887)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1351)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1230)
at org.apache.catalina.startup.WebAnnotationSet.loadClassAnnotation(WebAnnotationSet.java:145)
at org.apache.catalina.startup.WebAnnotationSet.loadApplicationFilterAnnotations(WebAnnotationSet.java:84)
at org.apache.catalina.startup.WebAnnotationSet.loadApplicationAnnotations(WebAnnotationSet.java:57)
at org.apache.catalina.startup.ContextConfig.applicationAnnotationsConfig(ContextConfig.java:297)
at org.apache.catalina.startup.ContextConfig.start(ContextConfig.java:1068)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:261)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4339)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:741)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:516)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:587)
… 6 more
javamug says:
Added on August 28th, 2010 at 9:51 amI am getting the same error as Bryan:
SEVERE: Null component Catalina:type=JspMonitor,name=jsp,WebModule=//localhost/mvc-showcase,J2EEApplication=none,J2EEServer=none
java.lang.reflect.InvocationTargetException
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:585)
at org.apache.catalina.startup.Bootstrap.start(Bootstrap.java:290)
at org.apache.catalina.startup.Bootstrap.main(Bootstrap.java:415)
Caused by: java.lang.UnsupportedClassVersionError: Bad version number in .class file (unable to load class org.springframework.samples.mvc.flash.FlashMapFilter)
at org.apache.catalina.loader.WebappClassLoader.findClassInternal(WebappClassLoader.java:1894)
at org.apache.catalina.loader.WebappClassLoader.findClass(WebappClassLoader.java:887)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1351)
at org.apache.catalina.loader.WebappClassLoader.loadClass(WebappClassLoader.java:1230)
at org.apache.catalina.startup.WebAnnotationSet.loadClassAnnotation(WebAnnotationSet.java:145)
at org.apache.catalina.startup.WebAnnotationSet.loadApplicationFilterAnnotations(WebAnnotationSet.java:84)
at org.apache.catalina.startup.WebAnnotationSet.loadApplicationAnnotations(WebAnnotationSet.java:57)
at org.apache.catalina.startup.ContextConfig.applicationAnnotationsConfig(ContextConfig.java:297)
at org.apache.catalina.startup.ContextConfig.start(ContextConfig.java:1068)
at org.apache.catalina.startup.ContextConfig.lifecycleEvent(ContextConfig.java:261)
at org.apache.catalina.util.LifecycleSupport.fireLifecycleEvent(LifecycleSupport.java:119)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4339)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardHost.start(StandardHost.java:741)
at org.apache.catalina.core.ContainerBase.start(ContainerBase.java:1045)
at org.apache.catalina.core.StandardEngine.start(StandardEngine.java:443)
at org.apache.catalina.core.StandardService.start(StandardService.java:516)
at org.apache.catalina.core.StandardServer.start(StandardServer.java:710)
at org.apache.catalina.startup.Catalina.start(Catalina.java:587)
… 6 more
Am I doing something wrong please let me know