Spring MVC
Any application consists of three layers those are:
1. Presentation layer : this layer of an application deals with user interaction with the
application.
2. Business logic : this layer of an application deals with data processing and database
connectivity.
3. Database layer : this layer of an application deals with data management and data
storage.
Monolithic application : If all the above layers of an application are present as a single
application then that application is called as monolithic application.
MVC
MVC stands for model, view, controller. It is an architectural design pattern which is used to
develop monolithic application.
1. Model : model represents business logic of an application along with the database
connectivity.
2. View : view represents presentation layer or user interface.
3. Controller : controller acts like an interface between view and model. Controller controls
all the requests and responses.
Detailed spring MVC architecture
steps to create maven project (archetype web app version 1.5)
step 1. press ctrl + N and search for maven project and select the maven project option
step 2. Don’t skip the architype selection and click on next
step 3. in next window apply the filter as org.apache.maven and select the maven-
archetype-webapp (1.5)
step 4. in the next window provide the appropriate group id and artifact id and uncheck the
checkbox saying “run architype generation interactively” and click on finish.
Add required folders
If the project is created with some missing folders then we can follow some following steps
to add those missing folders
step 1. select the project folder and right click. Go to build path and click on configure build
path option.
step 2. Go to order and export section and click on select all option and the click on apply
and close.
Dependencies required for spring MVC project
1. MySQL connector
2. Hibernate core relocation
3. Java servlet API
4. JSP API (Java Server Pages API)
5. Spring context
6. Spring web MVC
7. Project Lombok
steps to update maven project
step 1. Select the project folder and right click
step 2. go to an option called as maven and select the option as update project
step 3. in the next window select the maven project to be updated, check the check box as
force update of Snapshots / Releases and then click on ok.
If still the error is there then right click on the project and click on refresh.
Configuration of web.xml
Location : src/main/webapp/WEB-INF/web.xml
<servlet>
<servlet-name>Dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet class>
</servlet>
<servlet-mapping>
<servlet-name>Dispatcher</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
To get the qualified name of the DispatcherServlet , press ctrl + shift + T, search for
DispatcherServlet, select the appropriate class and open its implementation and copy its
qualified name.
Dispatcher-servlet.xml
Location : src/main/webapp/WEB-INF/Dispatcher-servlet.xml
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<context:component-scan base-package="com.jspiders.springmvc" />
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name="prefix" value="/WEB-INF/views/" />
<property name="suffix" value=".jsp" />
</bean>
</beans>
To get the qualified name of the InternalResourceViewResolver , press ctrl + shift + T, search
for InternalResourceViewResolver, select the appropriate class and open its implementation
and copy its qualified name.
Configure persistence.xml
Location : src/main/resources/META-INF/persistence.xml
Annotations used in spring MVC
@Controller : This is a class level annotation which is used to mark the class as controller
class. Controller class contains Handler methods which are responsible for handling web
requests.
@RequestMapping : This annotation is class level as well as method level annotation. This
annotation is used to map a specific web request to a specific controller or handler method.
@RequestParam : This annotation is field level annotation which is used along with the
formal arguments of handler methods. This annotation binds request parameter to a specific
formal argument of a handler method.
ModelMap
ModelMap object is used to map the model data to a specific view. This object is created by
the DispatcherServlet.
HttpSession
HttpSession object is used to implement the stateful application. This object is used to track
an authenticated user throughout the application. This object is created by the
DispatcherServlet.
“Session is a time span between user login and logout”