Spring MVC: Bharat Singh 27-May-2019
Spring MVC: Bharat Singh 27-May-2019
Bharat Singh
27-May-2019
Agenda
What is Spring
Introduction to maven
Model - A model contains the data of the application. A data can be a single
object or a collection of objects.
The POM
The pom.xml file is the core of a project's configuration in Maven. It is a
single configuration file that contains the majority of information
required to build a project in just the way you want. The POM is huge
and can be daunting in its complexity, but it is not necessary to
understand all of the intricacies just yet to use it effectively. This
project's POM is:
How maven works
Core Concepts of Maven:
POM Files: Project Object Model(POM) Files are XML file that contains
information related to the project and configuration information such as
dependencies, source directory, plug-in, goals etc. used by Maven to build
the project. When you should execute a maven command you give maven
a POM file to execute the commands. Maven reads pom.xml file to
accomplish its configuration and operations.
Dependencies and Repositories: Dependencies are external Java libraries
required for Project and repositories are directories of packaged JAR files.
The local repository is just a directory on your machine hard drive. If the
dependencies are not found in the local Maven repository, Maven
downloads them from a central Maven repository and puts them in your
local repository.
Build Life Cycles, Phases and Goals: A build life cycle consists of a
sequence of build phases, and each build phase consists of a sequence of
goals. Maven command is the name of a build lifecycle, phase or goal. If a
lifecycle is requested executed by giving maven command, all build phases
in that life cycle are executed also. If a build phase is requested executed,
all build phases before it in the defined sequence are executed too.
Build Profiles: Build profiles a set of configuration values which allows you
to build your project using different configurations. For example, you may
need to build your project for your local computer, for development and
test. To enable different builds you can add different build profiles to your
POM files using its profiles elements and are triggered in the variety of
ways.
Build Plug-ins: Build plug-ins are used to perform specific goal. you can
add a plug-in to the POM file. Maven has some standard plug-ins you can
use, and you can also implement your own in Java.
Elements used for Creating pom.xml file
Project- It is the root element of the pom.xml file.
ModelVersion- means what version of the POM model you are using.
Use version 4.0.0 for maven 2 and maven 3.
A groupId- means the id for the project group. It is unique and Most
often you will use a group ID which is similar to the root Java package
name of the project like we used the groupId com.project.loggerapi.
ArtifactId- used to give name of the project you are building.in our
example name of our project is LoggerApi.
Version- contains the version number of the project. If your project has
been released in different versions then it is useful to give version of
your project.
Other Elements of Pom.xml file
Dependencies - is used to defines a list of dependency of project.
Dependency- defines a dependency and used inside dependencies tag.
Each dependency is described by its groupId, artifactId and version.
Name- element is used to give name to our maven project.
Scope- element used to define scope for this maven project that can be
compile, runtime, test, provided system etc.
Packaging- element is used to packaging our project to output types like
JAR, WAR etc.
Xml based configuration
The /WEB-INF/web.xml file is used to define how to deploy the web module
to a Servlet container like JBoss, Tomcat, Glassfish or any other servlet
container. It is a part of the Java Servlet specification. The file name and
locations cannot be changed.
Basically web.xml tell container all servlets in the web application
with <servlet> element , then tell container when to use which servlet by the
url mapping with <servlet-mapping> element.
In Spring MVC project, for most cases, there is only one
servlet org.springframework.web.servlet.DispatcherServlet.
Let this DispatcherServlet handle all the requests. After container hands over
the request to DispatcherServlet, the income http request formally enters the
Spring world. A controller will be chosen to handle the request according to
@RequestMapping.
What is context configuration xml
In XML style configured Spring MVC project, there will be at least one xml to
configure Spring MVC WebApplicatonContext. As Spring document says, there are
2 type of WebApplicationContext:
Root WebApplicationContext
Servlet WebApplicationContext
All servlets, althrough in most case only one DispatcherServlet, share root
context. Each Servlet has its private context. Spring tries to locate a bean first in
Servlet context, if not found, then search root context. A Spring MVC project can
use both or just any one of them.
The default configuration file for root WebApplicatonContext is /WEB-
INF/applicationContext.xml
The default configuration file for servlet WebApplicatonContext is /WEB-
INF/[servletName]-servlet.xml
The files’ name and location can be changed in web.xml.
The job of the DispatcherServlet is to take an incoming URI and find the
right combination of handlers (generally methods on Controller classes)
and views (generally JSPs) that combine to form the page or resource
that's supposed to be found at that location.
A project might have
a file /WEB-INF/jsp/pages/Home.jsp
and a method on a class
@RequestMapping(value="/pages/Home.html")
private ModelMap buildHome() {
return somestuff;
}
What is Spring DispatcherServlet
DispatcherServlet acts as front controller for Spring based web
applications. It provides a mechanism for request processing where actual
work is performed by configurable, delegate components. It is inherited
from javax.servlet.http.HttpServlet, it is typically configured in
the web.xml file.
A web application can define any number of DispatcherServlet instances.
Each servlet will operate in its own namespace, loading its own application
context with mappings, handlers, etc. Only the root application context as
loaded by ContextLoaderListener, if any, will be shared. In most cases,
applications have only single DispatcherServlet with the context-root
URL(/), that is, all requests coming to that domain will be handled by it.
DispatcherServlet uses Spring configuration classes to discover the
delegate components it needs for request mapping, view resolution,
exception handling etc.
How it uses Web Application Context
how dispatcher servlet works internally? In a Spring-based application, our
application objects live within an object container. This container creates
objects and associations between objects, and manages their complete life
cycle. These container objects are called Spring-managed beans (or simply
beans), and the container is called an application context (via
class ApplicationContext) in the Spring world.
WebApplicationContext is an extension of a plain ApplicationContext. it is
web aware ApplicationContext i.e it has Servlet Context information.
When DispatcherServlet is loaded, it looks for the bean configuration file
of WebApplicationContext and initializes it.
By having access to Servlet context, any spring bean which
implement ServletConextAware interface – can get access
to ServletContext instance and do many things with it. For example, it can
get context init parameters, get context root information and get resources
location inside web application folders.
DispatcherServlet XML based Configuration
how a typical DispatcherServlet declaration and initialization looks like.
web.xml
<web-app>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/dispatcher-servlet-context.xml</param-value>
</context-param>
<servlet>
<servlet-name>dispatcher-servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-
class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value></param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>dispatcher-servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
In above code, dispatcher-servlet-context.xml file will contain all beans definitions and
associations which will be available to DispatcherServlet. These bean definitions will
override the definitions of any beans defined with the same name in the global scope.
e.g. applicationContext.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-2.5.xsd">
<bean id="viewResolver"
class="org.springframework.web.servlet.view.InternalResourceViewResolver" >
<property name="prefix">
<value>/WEB-INF/views/</value>
</property>
<property name="suffix">
<value>.jsp</value>
</property>
</bean>
</beans>
A Spring MVC Web Project
Thank You !!!