Spring MVC, a Java Model-View-Contraller (MVC) web framework, which builds on top of the Spring Inversion of control(IoC) framework.

Rewrite and Spring 4 (12/Jun/2015)
I’m rewriting the outdated articles and upgrade it to Spring 4, give me some time 🙂

1. Spring MVC Hello World

Some hello world examples to quick start Spring MVC framework.

  • Gradle – Spring 4 MVC Hello World Example (XML Configuration)
  • Gradle – Spring 4 MVC Hello World Example (@JavaConfig + Servlet 3)
  • Maven – Spring 3 MVC Hello World Example (XML Configuration)
  • Maven – Spring 3 MVC Hello World Example (@JavaConfig + Servlet 3)
  • Spring 3 REST Example (Working)
  • Spring 4 REST Example (Working)
  • @Deprecated – Maven + Spring 2.5.6 MVC hello world example
  • @Deprecated – Maven + Spring 2.5.6 MVC hello world annotation example

New & Updated…

  • Spring 4 MVC Ajax Example
  • Spring 4 Form Handling Example
  • Spring 4 + Logback Example

2. Handler Mapping

Define how web request (URL) maps to the Controller handlers.

  • BeanNameUrlHandlerMapping example
    Maps the requested URL to the name of the cocntroller.
  • ControllerClassNameHandlerMapping example
    Uses convention to map the requested URL to Controller.
  • SimpleUrlHandlerMapping example
    Allow developer to specify the mapping of URL patterns and handler mappings explicitly.
  • Configure the handler mapping priority
    If multiple view handler mappings are applied, you have to declare priority to avoid conflict issue.

3. Controller

Controller class to handle the web request.

  • MultiActionController example
    Group related actions into a single controller class.
  • MultiActionController annotation example
    Multi-actions controller by using annotation, @RequestMapping, he best and easy solution.
  • PropertiesMethodNameResolver example
    A flexible method name resolver for the MultiActionController controller class, it allow to define the mapping between requested URL and method name explicitly.
  • ParameterMethodNameResolver example
    Another method name resolver for the MultiActionController controller class, it allow to map URL to method name via requested parameter name.
  • ParameterizableViewController example
    With ParameterizableViewController, you don’t need to hard code the view name in the controller class anymore, instead, you specify the view name through the ParameterizableViewController’s “viewName” property.

4. View Resolver

Resolve “view name” that returned from the controller class to a physical view page or JSP page.

  • InternalResourceViewResolver example
    Adding a predefined prefix and suffix to the view name (prefix + view name + suffix), and generate the final view page URL.
  • XmlViewResolver example
    Puts view beans in XML file.
  • ResourceBundleViewResolver example
    Puts view beans in “.properties” file.
  • Configure multiple view resolvers priority
    If multiple view resolver strategies are applied, you have to declare priority to avoid conflict issue.

5. Form Handling

Form handling in Spring MVC.

  • Form handling example
    Form handling in Spring MVC, XML based version.
  • Form handling annotation example
    Form handling in Spring MVC, annotation version.
  • Handling duplicate form submission
    “Post/Redirect/Get” Design Pattern is the common solution for this duplicated form submission problem.
  • RedirectView example
    A view which redirect to another absolute, context relative, or current request relative URL.
  • Handling multipage / wizard forms
    How to handle multipage forms or wizard form.

6. Spring’s Form Tag Library

Render HTML form’s components via Spring’s form tag.

  • Textbox example
    <form:input /> tag, render a HTML textbox field.
  • Password example
    <form:password /> tag, render a HTML password field.
  • Textarea example
    <form:textarea /> tag, render a HTML textarea field.
  • Checkbox and Checkboxes example
    <form:checkbox />, <form:checkboxes /> tags, render a single or multiple HTML check boxes.
  • Radiobutton and Radiobuttons example
    <form:radiobutton />, <form:radiobuttons /> tags, render a single or multiple HTML radio buttons.
  • Dropdown and list box example
    <form:select />, <form:option /> and <form:options /> tags, render a HTML dropdown box, multiple select box and listbox.
  • Hidden value example
    <form:hidden /> tags, render a HTML hidden value field.
  • Form errors tag example
    <form:errors /> tag , render those form components error messages.
  • File upload example
    File upload handling via Spring MultipartResolver.

7. Integrating Spring MVC with other frameworks

Integrate Spring MVC with others.

  • Spring 3 MVC and JSR303 @Valid example
    Using Hibernate validator (JSR303 implementation) to validate bean in Spring MVC.
  • Spring 3 MVC and RSS feed example
    Using ROME to generate RSS feed from Spring MVC.
  • Spring 3 MVC and XML example
    Using JAXB to generate XML output from Spring MVC.
  • Spring 3 MVC and JSON example
    Using Jackson to generate JSON output from Spring MVC.
  • Spring MVC and Excel file via AbstractExcelView
    Using AbstractExcelView to export data to Excel file via Apache POI library.
  • Spring MVC and Excel file via AbstractJExcelView
    Using AbstractJExcelView to export data to Excel file via JExcelAPI library.
  • Spring MVC and PDF file via AbstractPdfView
    Using AbstractPdfView to export data to Pdf file via Bruno Lowagie’s iText library.
  • Spring MVC and Log4j integration example
    Example to integrate Log4j into the Spring MVC application.

8. FAQs

Some common use cases.

  • Spring 3 MVC ContentNegotiatingViewResolver example
    This view resolver let you determine which view resolver will be returned, base on reuqested file extension.
  • Exception handling example
    Exception handling in Spring MVC.
  • Handler interceptors example
    Intercept web request through handler interceptors.
  • Internationalization example
    Multiple languages supported in Spring MVC.

10. Common Errors

Spring MVC common errors and solution.

  • ModelAndView’s model value is not displayed in JSP via EL
  • 404 error code is not working in Spring MVC
  • Cannot change HTTP accept header – use a different locale resolution strategy
  • Spring MVC failed to convert property value in file upload form
  • Neither BindingResult nor plain target object for bean name ‘xxx’ available as request attribute
  • ClassNotFoundException : com.sun.syndication.feed.WireFeed
  • ClassNotFoundException : com.thoughtworks.xstream.io.HierarchicalStreamReader

Spring Web MVC References

  1. Spring 4.2.x MVC Documentation
  2. Spring 3.2.x MVC Documentation
  3. Spring 2.5.6 MVC Documentation

Leave a Reply

Your email address will not be published. Required fields are marked *