In this demo we explore for building a simple web service with MVC
.
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 ofPOJO
. - The
View
(UI logic) is responsible for rendering the model data and in general it generatesHTML
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.
-
After receiving an
HTTP
request,DispatcherServlet
consults theHandlerMapping
to call the appropriateController
. -
The
Controller
takes the request and calls the appropriateservice
methods based on usedGET
orPOST
method. Theservice
method will setmodel
data based on defined business logic and returnsview
name to theDispatcherServlet
. -
The
DispatcherServlet
will take help fromViewResolver
to pickup the defined view for the request. -
Once view is finalized, the
DispatcherServlet
passes themodel
data to theview
which is finally rendered on the browser.
-
pom.xml
contains project dependencies, which arejar
files integrated byMaven
. -
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 inWebapp/WEB-INF
directory ) uses aURL
mapping to specifyservlets
information including names, types and url-patterns. -
dispatch-servlet.xml
has the file name begin with the servlet name specified inweb.xml
. The file contains the online schema for servlet and the package names where theCONTROLLERs
are located. -
@Controller
annotation indicates that a particular class serves the role of a controller. -
@RequestMapping
annotation is used to map aURL
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 inMODEL
. -
Using
ModelAndView
we pass the specific data to the specificview
. -
MathService.java
(MODEL
) supplies the service (computation), according to the principle that the business logic should be separated fromCONTROLLER
and located inMODEL
. -
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 toweb.xml
;DispatchConfig.java
is corresponding todispatch-servlet.xml
;
- Windows 7
- Java 1.8
- Eclipse 4.12.0
- Apache Tomcat v9.0
- Spring-webmvc 4.1.8.RELEASE
- "Spring MVC Tutorial | Full Course" by Telusko
- "Spring - MVC Framework" by tutorialspoint