Spring MVC Tutorial

Core Spring MVC

Spring MVC - Annotation

Spring MVC - Form Handling

Spring MVC with JSTL

Spring MVC with REST API

Spring MVC with Database

Spring MVC using Java-based configuration

Java-based configuration in Spring MVC provides an alternative to XML-based configuration. This approach allows you to configure your Spring MVC application using Java classes rather than XML files. Below is a step-by-step guide on how to set up a basic Spring MVC application using Java-based configuration:

1. Maven Dependencies

In your pom.xml file, add the required dependencies:

<!-- Spring Web MVC -->
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-webmvc</artifactId>
    <version>5.x.x.RELEASE</version>
</dependency>

<!-- Servlet API for compile scope only, it's provided by the servlet container at runtime -->
<dependency>
    <groupId>javax.servlet</groupId>
    <artifactId>javax.servlet-api</artifactId>
    <version>3.1.0</version>
    <scope>provided</scope>
</dependency>

2. Java Configuration Class

Create a configuration class annotated with @Configuration and @EnableWebMvc:

@Configuration
@EnableWebMvc
@ComponentScan(basePackages = "com.example.demo")
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("index");
    }

    @Bean
    public ViewResolver viewResolver() {
        InternalResourceViewResolver resolver = new InternalResourceViewResolver();
        resolver.setPrefix("/WEB-INF/views/");
        resolver.setSuffix(".jsp");
        return resolver;
    }
}

3. Web Application Initializer

Implement WebApplicationInitializer to programmatically configure the Servlet context:

public class MyWebAppInitializer implements WebApplicationInitializer {

    @Override
    public void onStartup(ServletContext container) throws ServletException {
        AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
        context.register(WebConfig.class);

        ServletRegistration.Dynamic registration = container.addServlet("dispatcher", new DispatcherServlet(context));
        registration.setLoadOnStartup(1);
        registration.addMapping("/");
    }
}

This initializer will replace the traditional web.xml file.

4. Controller

Create a simple controller to handle web requests:

@Controller
public class HomeController {

    @RequestMapping("/")
    public String home() {
        return "index";
    }
}

5. JSP View

Create a JSP file under /WEB-INF/views/ named index.jsp:

<!DOCTYPE html>
<html>
<head>
    <title>Welcome to Spring MVC</title>
</head>
<body>
    <h1>Hello, Spring MVC with Java Configuration!</h1>
</body>
</html>

6. Run the Application

You can now run your application on a servlet container (like Tomcat) to see it in action. When accessing the root URL of your application (/), you should see the greeting message from the index.jsp view.

By following these steps, you'll have set up a basic Spring MVC application using Java-based configuration without any XML files.

  1. Spring MVC Java-based Configuration Example:

    • Description: This is a basic example showcasing the configuration of Spring MVC without XML using Java-based configuration.

    • Code Snippet: (Java Configuration)

      @Configuration
      @EnableWebMvc
      public class MvcConfig extends WebMvcConfigurerAdapter {
      
          @Override
          public void configureViewResolvers(ViewResolverRegistry registry) {
              registry.jsp("/WEB-INF/views/", ".jsp");
          }
      
          @Override
          public void addResourceHandlers(ResourceHandlerRegistry registry) {
              registry.addResourceHandler("/static/**").addResourceLocations("/static/");
          }
      }
      
  2. Java-based Spring MVC Controller Configuration:

    • Description: This example focuses on configuring Spring MVC controllers using Java-based configuration.

    • Code Snippet: (Java Configuration with controller setup)

      @Configuration
      @EnableWebMvc
      @ComponentScan(basePackages = "com.example.controllers")
      public class MvcConfig extends WebMvcConfigurerAdapter {
      
          // Other configurations...
      
      }