Featured Post

spring context


What is Spring MVC framework?

The Spring web MVC framework provides model-view-controller architecture and ready components that can be used to develop flexible and loosely coupled web applications. The MVC pattern results in separating the different aspects of the application (input logic, business logic, and UI logic), while providing a loose coupling between model, view and controller parts of application. Spring framework provides lots of advantages over other MVC frameworks e.g.
  1. Clear separation of roles ? controller, validator, command object, form object, model object, DispatcherServlet, handler mapping, view resolver, etc. Each role can be fulfilled by a specialized object.
  2. Powerful and straightforward configuration of both framework and application classes as JavaBeans.
  3. Reusable business code ? no need for duplication. You can use existing business objects as command or form objects instead of mirroring them in order to extend a particular framework base class.
  4. Customizable binding and validation
  5. Customizable handler mapping and view resolution
  6. Customizable locale and theme resolution
  7. A JSP form tag library, introduced in Spring 2.0, that makes writing forms in JSP pages much easier. etc.

What is DispatcherServlet and ContextLoaderListener?

Spring?s web MVC framework is, like many other web MVC frameworks, request-driven, designed around a central Servlet that handles all the HTTP requests and responses. Spring?s DispatcherServlet however, does more than just that. It is completely integrated with the Spring IoC container so it allows you to use every feature that Spring has.
After receiving an HTTP request, DispatcherServlet consults the HandlerMapping (configuration files) to call the appropriate Controller. The Controller takes the request and calls the appropriate service methods and set model data and then returns view name to the DispatcherServlet. The DispatcherServlet will take help from ViewResolver to pickup the defined view for the request. Once view is finalized, The DispatcherServlet passes the model data to the view which is finally rendered on the browser.

  Archetype Created Web Application
  
  
  spring
   
    org.springframework.web.servlet.DispatcherServlet
   
  1
 

 
  spring
  /
 
 

By default, DispatcherServlet loads its configuration file using -servlet.xml. E.g. with above web.xml file, DispatcherServlet will try to find spring-servlet.xml file in classpath.
ContextLoaderListener reads the spring configuration file (with value given against ?contextConfigLocation? in web.xml), parse it and loads the beans defined in that config file. e.g.

 spring
 
  org.springframework.web.servlet.DispatcherServlet
 
 
 
  contextConfigLocation
  /WEB-INF/applicationContext.xml
 
 
 1


What is the front controller class of Spring MVC?

A front controller is defined as ?a controller which handles all requests for a Web Application.? DispatcherServlet (actually a servlet) is the front controller in Spring MVC that intercepts every request and then dispatches/forwards requests to an appropriate controller. 
When a web request is sent to a Spring MVC application, dispatcher servlet first receives the request. Then it organizes the different components configured in Spring?s web application context (e.g. actual request handler controller and view resolvers) or annotations present in the controller itself, all needed to handle the request.

How to use Java based configuration?

To configure java based MVC application, first add required dependencies.

         

 org.springframework
 spring-webmvc
 4.1.4.RELEASE



 org.springframework
 spring-web
 4.1.4.RELEASE

 

 

 javax.servlet
 jstl
 1.2
 runtime

 

 taglibs
 standard
 1.1.2
 runtime

Now add DispatcherServlet entry in web.xml file so that all incoming requests come though DispatcherServlet only.

 spring
  
   org.springframework.web.servlet.DispatcherServlet
  
 1



 spring
 /

Now add below entries in spring configuration file.

 
    
 
    
    
 
    
    
        
        
    
 

Add controller code.
@Controller
@RequestMapping("/employee-module")
public class EmployeeController
{
    @Autowired
    EmployeeManager manager;
 
    @RequestMapping(value = "/getAllEmployees", method = RequestMethod.GET)
    public String getAllEmployees(Model model)
    {
        model.addAttribute("employees", manager.getAllEmployees());
        return "employeesListDisplay";
    }
}
Additionally you should add manager and dao layer classes as well. Finally you add the jsp file to display the view.
I will suggest to read above linked tutorial for complete understanding.

Comments

Popular posts from this blog

[Inside AdSense] Understanding your eCPM (effective cost per thousand impress...