# Spring Model-View-Controller (MVC) Example

In 
Published 2022-12-03

This tutorial shows you how to use Model-View-Controller (MVC) for creating web applications.

The Model-View-Controller (MVC) is the Spring way to create a JSP application.

The Model represents the application data and in general they will consist of POJO.

The View is responsible for rendering the HTML pages using the Model data.

The Controller is responsible for processing user requests and building an appropriate model and passes it to the view for rendering.

In this tutorial you will see how to create a simple Web application using the Spring MVC framework.

First, you need to create a Web Application and configure it for using Maven repository.

Here is the pom.xml file dependencies:

Now we have to create the following classes:

package com.example.app;
 
import org.springframework.web.servlet.support.AbstractAnnotationConfigDispatcherServletInitializer;
 
import com.example.config.ProjectConfig;
import com.example.config.WebConfig;
 
public class MyAppStarter extends AbstractAnnotationConfigDispatcherServletInitializer{
 
    @Override
    protected Class<!--?-->[] getRootConfigClasses() {
        return new Class[] {ProjectConfig.class};
    }
 
    @Override
    protected Class<!--?-->[] getServletConfigClasses() {
        return new Class[] {WebConfig.class};
    }
 
    @Override
    protected String[] getServletMappings() {
        return new String[] {"/"};
    }
     
}
package com.example.config;
 
public class ProjectConfig {
 
}
package com.example.config;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.ViewResolver;
import org.springframework.web.servlet.config.annotation.DefaultServletHandlerConfigurer;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.servlet.view.InternalResourceViewResolver;
 
@Configuration
@EnableWebMvc
@ComponentScan("com.example.*")
public class WebConfig implements WebMvcConfigurer {
     
    @Override
    public void configureDefaultServletHandling (
            DefaultServletHandlerConfigurer configurer) {
        configurer.enable();
    }
 
    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver =
            new InternalResourceViewResolver();
         
        resolver.setPrefix("/WEB-INF/pages/");
        resolver.setSuffix(".jsp");
        resolver.setExposeContextBeansAsAttributes(true);
        return resolver;
    }
}
package com.example.controller;
 
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
 
@Controller
public class MyController {
 
    @RequestMapping(value="/", method=RequestMethod.GET)
    public String index() {
        return "index";
    }
     
    @RequestMapping(value="/MySpringMvcPage", method=RequestMethod.GET)
    public String mySpringMvcPage(
                   //@PathParam is used when you don't use "?" and the parameters are 
                   //defined in link structure 
                   @RequestParam(value="variable2", required = false) String variable2,          
                   Model m) {
         
        m.addAttribute("variable1","Value1");
        m.addAttribute("variable2", variable2);
         
        return "MySpringMvcPage";
    }
}

You have to create the JSP pages with the following content as well:


     <%@ page language="java" contentType="text/html; charset=ISO-8859-1" pageEncoding="ISO-8859-1"%>
     <meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
     <title>Insert title here</title>

    <br>
    <br>
    <br>
    <br>
    <a href="/SpringMVC/MySpringMvcPage?variable2=Value2">Visit another Spring MVC page</a>
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>

<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>Insert title here</title>

   <h1>  Spring MVC works !   </h1>
   <br> <br>
    
   Variable #1 had the following value: <b> ${variable1} </b>!
   <br>
   Variable #2 had the following value: <b> ${variable2} </b>!
    
   <a href="/SpringMVC/">Index Page</a>
    
   <br> <br>

When the application is completed you will have the following application structure:

When I run the application on a servlet container (Tomcat in my case) you can see the following in the browser:

When you click on the link you will the following page: