yuhang2685 / Demo-SpringMVC

In this demo, we explore the features of Spring MVC, with explanation for the fundamental concepts.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Demo-SpringMVC

Author: Dr. YUHANG ZHAO

Introduction

In this demo we explore for building a simple web service with MVC.

Overview:

Model–View–Controller (well known as MVC) is a software architectural pattern commonly used for developing user interfaces. It divides the related program logic into three interconnected elements. This is done to separate internal representations of information from the ways information is presented to and accepted from the user. Following the MVC architectural pattern decouples these major components allowing for code reuse and parallel development, while providing a loose coupling between these components.

  • The Model (business logic) encapsulates the application data and in general they will consist of POJO.
  • The View (UI logic) is responsible for rendering the model data and in general it generates HTML output that the client's browser can interpret.
  • The Controller (input logic) is responsible for handling user requests and building an appropriate model and passes it to the view for rendering.

DispatcherServlet

DispatcherServlet

  • After receiving an HTTP request, DispatcherServlet consults the HandlerMapping to call the appropriate Controller.

  • The Controller takes the request and calls the appropriate service methods based on used GET or POST method. The service method will set model data based on defined business logic and 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.

Working Mechanism

  • pom.xml contains project dependencies, which are jar files integrated by Maven.

  • index.jsp is the start page of the web site where it has triggers to requests for web services.

  • Through the annotation @RequestParam we obtain the user input parameters.

  • web.xml (must reside in Webapp/WEB-INF directory ) uses a URL mapping to specify servlets information including names, types and url-patterns.

  • dispatch-servlet.xml has the file name begin with the servlet name specified in web.xml. The file contains the online schema for servlet and the package names where the CONTROLLERs are located.

  • @Controller annotation indicates that a particular class serves the role of a controller.

  • @RequestMapping annotation is used to map a URL to either an entire class or a particular handler method.

  • MathController.java (CONTROLLER) contains the methods annotated by @RequestMapping to handle the particular requests by calling the corresponding services in MODEL.

  • Using ModelAndView we pass the specific data to the specific view.

  • MathService.java (MODEL) supplies the service (computation), according to the principle that the business logic should be separated from CONTROLLER and located in MODEL.

  • displayResult.jsp (VIEW) displays the result of request using "Expression Language(EL)".

An alternative approach is called "Annotation Configuration". It replaces the configuration in XML (web.xml and dispatch-servlet.xml) by java classes (MyWebInitializer.java and DispatchConfig.java):

  • MyWebInitializer.java is corresponding to web.xml;
  • DispatchConfig.java is corresponding to dispatch-servlet.xml;

Related Projects

Demo-SpringBoot-WebApp

System Configuration:

  • Windows 7
  • Java 1.8
  • Eclipse 4.12.0
  • Apache Tomcat v9.0
  • Spring-webmvc 4.1.8.RELEASE

Reference:

About

In this demo, we explore the features of Spring MVC, with explanation for the fundamental concepts.


Languages

Language:Java 100.0%