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_1.xsd" xsi:schemaLocation=" http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1"> <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, merely 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.
Note: There are multiple approaches to configure a Spring MVC project. Instead of web.xml as described above, we can have a 100% Java-configured project using initializer. For more details, refer to our existing article.
3. The Spring MVC Configuration – Java
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 Spring MVC Configuration – XML
Alternatively to the Java configuration above, we can also use a purely XML config:
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xsi:schemaLocation=" http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd"> <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/" /> <property name="suffix" value=".jsp" /> </bean> <mvc:view-controller path="/sample.html" view-name="sample" /> </beans>
5. 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.
6. Spring MVC with Boot
Spring Boot is an addition to Spring Platform which 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 manage dependencies for us.
The quickest way to get started is by adding the spring-boot-starter-parent pom.xml:
<parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>1.5.6.RELEASE</version> </parent>
This will take care of dependency management.
6.2. Spring Boot Entry Point
Each application built using Spring Boot needs merely to define the main entry point. This is usually a Java class with the main method, annotated with @SpringBootApplication:
@SpringBootApplication public class Application { public static void main(String[] args) { SpringApplication.run(Application.class, args); } }
This annotation adds the following other annotations:
- @Configuration – which marks the class as a source of bean definitions
- @EnableAutoConfiguration – which tells the framework to add beans based on the dependencies on the classpath automatically
- @ComponentScan – which scans for other configurations and beans in the same package as the Application class or below
With Spring Boot, we can set up frontend using Thymeleaf or JSP’s without using ViewResolver as defined in section 4. By adding spring-boot-starter-thymeleaf dependency to our pom.xml, Thymeleaf gets enabled, and no extra configuration is necessary.
The source code for the Boot app is, as always, available over on GitHub.
Finally, if you’re looking to get started with Spring Boot, have a look at our reference intro here.
7. 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: