Saturday, 14 May 2016

Up and Running With Spring MVC Controller

MVC or model view controller pattern relies on the front controller pattern, simply said the

DispatcherServlet receives a request for an URl and gives it to a mapped 

Controller to take the request and handle it.




The DispatcherServlet in Spring is generally configured in the XML like below


 <servlet>
        <servlet-name>Login</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    Similarly a Java based approach might look like below
   
    Dynamic dispatcherServlet = servletContext.addServlet("dispatcherServlet", new DispatcherServlet(context));


So its the first file to be loaded.


So once we have our dispatcherservlet loaded we need to make sure the controllers

are up and running.The DispatcherServlet  checks the handler mapping and points the request to the mapped Controller and calls its handleRequest method


In Spring we annotate Spring Controllers with @Controller annotation.

    

@Controller is a class level Annotation and we associate a class as a Controller like below;


import org.springframework.stereotype.Controller;

@Controller
public class HomeController{

}

The method which handles the requests in the Controller is
 ModelAndView handleRequest(HttpServletRequest request, HttpServletResponse response) throws Exception

 Now with the controller up and running we need to direct requests to it, for this we map methods in this controller to specific URls by using an annotation
 @RequestMapping.This annotation can be applied to a method or class level.


 @Controller
 @RequestMapping("/Zero")
 public class HomeController{

 @RequestMapping("/cool/{name}")
 public String isCool(Model model){
 }

 So if a request comes with "/Zero/cool/zerocool", our method isCool will handle it. The model argument is used to send data back with the view and can be shown on the view like JSP page using an JSP Expression.

 If you want to access the name, you need a @PathVariable annotation like below,


 @RequestMapping("/cool/{name})
 public String Test (@PathVariable("name") String name){

 }

 So the extraction and assignment of the variable is done by Spring and even converted to the right type.

 The original way of passing paramaeters in the URL via Get requests which we used to access using the request.getparameter in the servlet can be emulated using the @RequestParam annotation like below,

 So if the name comes in the URL like name=cool, we can access it like below.


 @RequestMapping("/cool)
 public String isCool(@RequestParam("name") String name){
   
 }


 The most advance s way to access the data is by Binding a POJO to the method by using the @ModelAttribute Annotation.


 So if the name is part of a POJO called Name with the getter and setter, we can use thge info like below

  @RequestMapping("/cool)
 public String isCool(@ModelAttribute Name name){
   
 }

 After matching the RequestParams and the POJO field names the data is assigned.