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

Create and Run Your First Spring MVC Controller in Eclipse/Spring Tool Suite

Creating and running your first Spring MVC controller involves multiple steps including setting up the development environment, creating a Spring project, defining the MVC components, and testing the application.

Let's break this down step by step:

1. Setting Up the Development Environment:

  1. Download and Install Java: Ensure you have the Java Development Kit (JDK) installed. You can download it from the official Oracle website.

  2. Install Eclipse with Spring Tool Suite (STS): Download Spring Tool Suite which is an Eclipse-based development environment that is customized for developing Spring applications.

2. Create a New Spring Project:

  1. Open STS/Eclipse.
  2. Go to File -> New -> Spring Starter Project.
  3. Fill in the details for the project (e.g., Name, Group, Artifact, etc.)
  4. In the dependencies section, select Web to include Spring Web MVC.
  5. Finish and the project will be created for you.

3. Define the MVC Components:

  1. Controller: Create a new package named com.example.controller. Inside this package, create a new class called HelloController.

    package com.example.controller;
    
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    public class HelloController {
    
        @RequestMapping("/hello")
        @ResponseBody
        public String helloWorld() {
            return "Hello, World!";
        }
    }
    
  2. Properties: In src/main/resources, you'll find an application.properties file. Here you can specify properties. For now, just set the server port if you wish:

    server.port=8081
    

4. Testing the Application:

  1. Run the Application: Right-click on the main class (the one annotated with @SpringBootApplication) -> Run As -> Java Application.

  2. Access the Endpoint: Open a web browser and navigate to http://localhost:8081/hello. You should see "Hello, World!" displayed.

Note: If you're creating a web application with Thymeleaf or JSP views, you'd also create a templates or webapp directory to hold your views. The controller would then return the name of the view rather than a direct string, and you wouldn't use @ResponseBody. The above example uses @ResponseBody to directly send a string response for simplicity.

That's it! You have created your first Spring MVC controller using Eclipse with Spring Tool Suite. Remember, this is a very basic introduction. Spring MVC offers many more features for building robust web applications.

  1. Spring MVC Controller example in Spring Tool Suite:

    • Create a simple Spring MVC controller class. Annotate it with @Controller and define methods to handle different URL mappings. For example:
      @Controller
      public class HomeController {
          @RequestMapping("/home")
          public String home() {
              return "home";
          }
      }