1. Overview
This is a simpleSpring MVC tutorial showing how to set up a Spring MVC project, both with a Java-based configuration as well as with XML configuration.
The Maven dependencies for a Spring MVC project are described in detail in theSpring MVC dependencies article.
2. What Is Spring MVC?
As the name suggests,it’s a module of the Spring framework dealing with the Model-View-Controller or MVC pattern. It combines all the advantages of the MVC pattern with the convenience of Spring.
Spring implements MVC with thefront controller pattern using itsDispatcherServlet.
In a nutshell, theDispatcherServlet acts as the main controller to route requests to their intended destination. Model is nothing but the data of our application, and the view is represented by any of thevarious template engines.
We’ll look at JSPs in our example in a bit.
3. Spring MVC Using Java Configuration
To enable Spring MVC support through a Java configuration class, we justadd the@EnableWebMvc annotation:
@EnableWebMvc@Configurationpublic class WebConfig { /// ...}
This will set up the basic support we need for an MVC project, such as registering controllers and mappings, type converters, validation support, message converters and exception handling.
If we want to customize this configuration, we need to implement theWebMvcConfigurer interface:
@EnableWebMvc@Configurationpublic class WebConfig implements WebMvcConfigurer { @Override public void addViewControllers(ViewControllerRegistry registry) { registry.addViewController("/").setViewName("index"); } @Bean public ViewResolver viewResolver() { InternalResourceViewResolver bean = new InternalResourceViewResolver(); bean.setViewClass(JstlView.class); bean.setPrefix("/WEB-INF/view/"); bean.setSuffix(".jsp"); return bean; }}
In this example, we’ve registered aViewResolver bean that will return.jsp views from the/WEB-INF/view directory.
Very important here is that we can register view controllers that create a direct mapping between the URL and the view name using theViewControllerRegistry. This way, there’s no need for any Controller between the two.
If we want to also define and scan controller classes, we can add the@ComponentScan annotation with the package that contains the controllers:
@EnableWebMvc@Configuration@ComponentScan(basePackages = { "com.baeldung.web.controller" })public class WebConfig implements WebMvcConfigurer { // ...}
To bootstrap an application that loads this configuration, we also need an initializer class:
public class MainWebAppInitializer implements WebApplicationInitializer { @Override public void onStartup(final ServletContext sc) throws ServletException { AnnotationConfigWebApplicationContext root = new AnnotationConfigWebApplicationContext(); root.scan("com.baeldung"); sc.addListener(new ContextLoaderListener(root)); ServletRegistration.Dynamic appServlet = sc.addServlet("mvc", new DispatcherServlet(new GenericWebApplicationContext())); appServlet.setLoadOnStartup(1); appServlet.addMapping("/"); }}
Note that for versions earlier than Spring 5, we have to use theWebMvcConfigurerAdapter class instead of the interface.
4.Spring MVC Using XML Configuration
Instead of the Java configuration above, we can also use a purely XML config:
<context:component-scan base-package="com.baeldung.web.controller" /><mvc:annotation-driven /> <bean > <property name="prefix" value="/WEB-INF/view/" /> <property name="suffix" value=".jsp" /> </bean> <mvc:view-controller path="/" view-name="index" /></beans>
If we want to use a purely XML configuration, we’ll also need to add aweb.xml file to bootstrap the application. For more detail on this approach, check outour previous article.
5. Controller and Views
Let’s have a look at an example of a basic controller:
@Controllerpublic class SampleController { @GetMapping("/sample") public String showForm() { return "sample"; }}
And the corresponding JSP resource is thesample.jsp file:
<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.
6. Spring MVC With Boot
Spring Boot is an addition to the Spring platform that makes it very easy to get started and create stand-alone, production-grade applications.Boot is not intended to replace Spring but to make working with it faster and easier.
6.1. Spring Boot Starters
The new framework provides convenient starter dependencies, which are dependency descriptors that can bring in all the necessary technology for a certain functionality.
These have the advantage that we no longer need to specify a version for each dependency but instead allow the starter to manage dependencies for us.
The quickest way to get started is by adding thespring-boot-starter-parentpom.xml:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId></parent>
This will take care of dependency management.
6.2. Spring Boot Entry Point
Each application built usingSpring Boot needs merely to define the main entry point.
This is usually a Java class with themain method, annotated with@SpringBootApplication:
@SpringBootApplicationpublic class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); }}
This annotation adds the following other annotations:
- @Configurationmarks the class as a source of bean definitions.
- @EnableAutoConfiguration tells the framework to add beans based on the dependencies on the classpath automatically.
- @ComponentScan scans for other configurations and beans in the same package as theApplication class or below.
With Spring Boot, we can set up front end using Thymeleaf or JSP’s without using ViewResolver as defined in Section 3. By addingspring-boot-starter-thymeleaf dependency to our pom.xml, Thymeleaf gets enabled, and no extra configuration is necessary.
Finally, if you’re looking to get started with Spring Boot, have a look at ourreference intro here.
7. Conclusion
In this article, we configured a simple and functional Spring MVC project, using Java configuration.
While accessible at http://localhost:8080/spring-mvc-basics-3/sample , sample.jsp and other JSP views reside in the secure WEB-INF folder, and are intended for internal Spring use and not direct URL access.