Spring Framework Tutorial

Software Setup and Configuration (STS/Eclipse/IntelliJ)

Core Spring

Spring Annotations

Spring Data

Spring JDBC

Spring Security

Spring @Controller Annotation with Example

Spring Core Annotations
  1. Spring @Controller example project:

    • Here's a basic example of a Spring MVC project with a controller annotated with @Controller.
    // MyController.java
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class MyController {
    
        @RequestMapping("/hello")
        public String hello() {
            return "hello";
        }
    }
    
  2. Creating controllers with @Controller annotation in Spring:

    • Create controllers in Spring by annotating a class with @Controller and defining request mapping methods.
    // MyController.java
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class MyController {
    
        @RequestMapping("/welcome")
        public String welcome() {
            return "welcome";
        }
    }
    
  3. Handling requests with @Controller in Spring framework:

    • Use the @Controller annotation to handle HTTP requests in a Spring MVC application.
    // MyController.java
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class MyController {
    
        @RequestMapping("/greet")
        public String greet() {
            return "greet";
        }
    }
    
  4. Mapping URLs to controller methods using @Controller in Spring:

    • Map specific URLs to controller methods using the @RequestMapping annotation.
    // MyController.java
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    public class MyController {
    
        @RequestMapping("/custom-url")
        public String handleCustomURL() {
            return "customPage";
        }
    }
    
  5. Request handling and response in Spring @Controller:

    • Handle requests and customize responses in a Spring @Controller by defining appropriate methods.
    // MyController.java
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.bind.annotation.ResponseBody;
    
    @Controller
    public class MyController {
    
        @RequestMapping("/json-response")
        @ResponseBody
        public String jsonResponse() {
            return "{'message': 'Hello, JSON!'}";
        }
    }
    
  6. Working with ModelAndView in Spring @Controller:

    • Use ModelAndView to work with models and views in a Spring @Controller.
    // MyController.java
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.RequestMapping;
    import org.springframework.web.servlet.ModelAndView;
    
    @Controller
    public class MyController {
    
        @RequestMapping("/model-view")
        public ModelAndView modelView() {
            ModelAndView modelAndView = new ModelAndView("myView");
            modelAndView.addObject("message", "Hello, ModelAndView!");
            return modelAndView;
        }
    }
    
  7. Customizing request mappings with @Controller in Spring:

    • Customize request mappings in a Spring @Controller by specifying URL patterns and handling different HTTP methods.
    // MyController.java
    import org.springframework.stereotype.Controller;
    import org.springframework.web.bind.annotation.GetMapping;
    import org.springframework.web.bind.annotation.PostMapping;
    import org.springframework.web.bind.annotation.RequestMapping;
    
    @Controller
    @RequestMapping("/custom-mappings")
    public class MyController {
    
        @GetMapping("/get-request")
        public String handleGetRequest() {
            return "getMapping";
        }
    
        @PostMapping("/post-request")
        public String handlePostRequest() {
            return "postMapping";
        }
    }