Spring MVC Tutorial

1. Overview and Maven

This is a simple Spring MVC tutorial showing how to set up a Spring MVC project, both with Java based Configuration as well as with XML Configuration.

The Maven artifacts for Spring MVC project are described in the in detail in the Spring MVC dependencies article.

2. The web.xml

This is a simple configuration of the web.xml for a Spring MVC project:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xmlns="http://java.sun.com/xml/ns/javaee" 
   xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
   xsi:schemaLocation="

http://java.sun.com/xml/ns/javaee

      http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
   id="WebApp_ID" version="3.0">

   <display-name>Spring MVC Java Config App</display-name>

   <servlet>
      <servlet-name>mvc</servlet-name>
      <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
      <load-on-startup>1</load-on-startup>
   </servlet>
   <servlet-mapping>
      <servlet-name>mvc</servlet-name>
      <url-pattern>/</url-pattern>
   </servlet-mapping>

   <context-param>
      <param-name>contextClass</param-name>
      <param-value>
         org.springframework.web.context.support.AnnotationConfigWebApplicationContext
      </param-value>
   </context-param>
   <context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>org.baeldung.spring.web.config</param-value>
   </context-param>
   <listener>
      <listener-class>
         org.springframework.web.context.ContextLoaderListener
      </listener-class>
   </listener>

</web-app>

We are using Java based Configuration, so we’re using AnnotationConfigWebApplicationContext as the main context class – this accepts @Configuration annotated classes as input. As such, we only need to specify the package where these configuration classes are located, via contextConfigLocation.

To keep this mechanism flexible, multiple packages are also configurable here, simply space delimited:

<context-param>
      <param-name>contextConfigLocation</param-name>
      <param-value>org.baeldung.spring.web.config org.baeldung.spring.persistence.config</param-value>
   </context-param>

This allows more complex projects with multiple modules to manage their own Spring Configuration classes and contribute them to the overall Spring context at runtime.

Finally, the Servlet is mapped to / – meaning it becomes the default Servlet of the application and it will pick up every pattern that doesn’t have another exact match defined by another Servlet.

3. The View Configuration

The Spring MVC Java configuration is simple – it uses the MVC configuration support introduced in Spring 3.1:

@EnableWebMvc
@Configuration
public class ClientWebConfig extends WebMvcConfigurerAdapter {

   @Override
   public void addViewControllers(ViewControllerRegistry registry) {
      super.addViewControllers(registry);

      registry.addViewController("/sample.html");
   }

   @Bean
   public ViewResolver viewResolver() {
      InternalResourceViewResolver bean = new InternalResourceViewResolver();

      bean.setViewClass(JstlView.class);
      bean.setPrefix("/WEB-INF/view/");
      bean.setSuffix(".jsp");

      return bean;
   }
}

Very important here is that we can register view controllers that create a direct mapping between the URL and the view name – no need for any Controller between the two now that we’re using Java configuration.

4. The JSP Views

We defined above a basic view controller – sample.html – the corresponding jsp resource is:

<html>
   <head></head>

   <body>
      <h1>This is the body of the sample view</h1>	
   </body>
</html>

The JSP based view files are located under the /WEB-INF folder of the project, so they’re only accessible to the Spring infrastructure and not by direct URL access.

5. Conclusion

In this example we configured a simple and functional Spring MVC project, using Java configuration. The implementation of this simple Spring MVC tutorial can be found in the github project – this is an Eclipse based project, so it should be easy to import and run as it is.

When the project runs locally, the sample.html can be accessed at:

 

Reference: Spring MVC Tutorial from our JCG partner Eugen Paraschiv at the baeldung blog.
Related Whitepaper:

Introduction to Web Applications Development

Kick start your web apps development with this introductory ebook!

This 376 page eBook 'Introduction to Web Applications Development', starts with an introduction to the internet, including a brief history of the TCT/IP protocol and World Wide Web. It defines the basic concepts for web servers and studies the case of Apache, the most used webserver, while other free software webservers are not forgotten. It continues with webpage design focusing on HTML and JavaScript. XML Schemas, their validation and transformation are covered as well as dynamic webpages built with CGI, PHP or JSP and database access.

Get it Now!  

Leave a Reply


6 + = twelve



Java Code Geeks and all content copyright © 2010-2014, Exelixis Media Ltd | Terms of Use | Privacy Policy
All trademarks and registered trademarks appearing on Java Code Geeks are the property of their respective owners.
Java is a trademark or registered trademark of Oracle Corporation in the United States and other countries.
Java Code Geeks is not connected to Oracle Corporation and is not sponsored by Oracle Corporation.

Sign up for our Newsletter

20,709 insiders are already enjoying weekly updates and complimentary whitepapers! Join them now to gain exclusive access to the latest news in the Java world, as well as insights about Android, Scala, Groovy and other related technologies.

As an extra bonus, by joining you will get our brand new e-books, published by Java Code Geeks and their JCG partners for your reading pleasure! Enter your info and stay on top of things,

  • Fresh trends
  • Cases and examples
  • Research and insights
  • Two complimentary e-books