TO Spring MVC: A Report On Industrial Training at
TO Spring MVC: A Report On Industrial Training at
TO Spring MVC: A Report On Industrial Training at
TO
SPRING MVC
by
RISHABH SINGH,
1509010166
Submitted To:
AUGUST,2018
CHAPTER 1: INTRODUCTION
The Spring Web MVC framework provides Model-View-Controller (MVC) 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 these elements.
The Model encapsulates the application data and in general they will consist of POJO.
The View is responsible for rendering the model data and in general it generates
HTML output that the client's browser can interpret.
The Controller is responsible for processing user requests and building an
appropriate model and passes it to the view for rendering.
In Spring Web MVC you can use any object as a command or form-backing object; you do not
need to implement a framework-specific interface or base class. Spring's data binding is highly
flexible: for example, it treats type mismatches as validation errors that can be evaluated by the
application, not as system errors. Thus you need not duplicate your business objects' properties
as simple, untyped strings in your form objects simply to handle invalid submissions, or to
convert the
Strings properly. Instead, it is often preferable to bind directly to your business objects.
Spring's view resolution is extremely flexible. A Controller implementation can even write
directly to the response stream. Typically, a ModelAndView instance consists of a view name
and a model Map, which contains bean names and corresponding objects such as a command
or form, which contain reference data. View name resolution is highly configurable, through
bean names, a properties file, or your own ViewResolver implementation. The model (the M
in MVC) is based on the Map interface, which allows for the complete abstraction of the
view technology. You can integrate directly JSP, Velocity, or any other rendering
technology. The model Map is simply transformed into an appropriate format, such as JSP
request attributes or a Velocity template model.
CHAPTER 2: ADVANTAGES OF SPRING
FRAMEOWRK:
The Spring framework addresses most of the infrastructure functionalities of the Enterprise
applications. Following are the few major advantages of Spring Framework.
1. Spring enables the developers to develop enterprise applications using POJOs (Plain
Old Java Object). The benefit of developing the applications using POJO is, that we
do not need to have an enterprise container such as an application server but we have
the option of using a robust servlet container.
2. Spring provides an abstraction layer on existing technologies like servlets, jsps, jdbc,
jndi, rmi, jms and Java mail etc., to simplify the develpment process.
3. Spring comes with some of the existing technologies like ORM framework, logging
framework, J2EE and JDK Timers etc, Hence we don’t need to integrate explicitly
those technologies.
4. Spring WEB framework has a well-designed web MVC framework, which provides a
great alternate to lagacy web framework.
5. Spring can eliminate the creation of the singleton and factory classes.
6. Spring provides a consistent transaction management interface that can scale down to
a local transaction and scale up to global transactions (using JTA).
7. Spring framework includes support for managing business objects and exposing their
services to the presentation tier components, so that the web and desktop applications
can access the same objects.
8. Spring framework has taken the best practice that have been proven over the years in
several applications and formalized as design patterns.
9. Spring application can be used for the development of different kind of applications,
like standalone applications, standalone GUI applications, Web applications and
applets as well.
10. Spring supports both xml and anotation configurations.
11. Spring Framework allows to develop standalone, desktop, 2 tire – n-tire architecture
and distributed applications.
12. Spring gives built in middleware services like Connection pooling, Transaction
management and etc.,
13. Spring provides a light weight container which can be activaed without using
webserver or application server.
CHAPTER 3: OVERVIEW OF SPRING MVC
PROCESSING SEQUENCE
The processing flow o f Spring MVC from receiving the request till the response is
returned is shown in the following diagram.
You need to map requests that you want the DispatcherServlet to handle, by using a URL
mapping in the web.xml file. The following is an example to show declaration and mapping
for HelloWeb DispatcherServlet example –
xmlns = "http://java.sun.com/xml/ns/j2ee"
xmlns:xsi = "http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation = "http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<servlet>
<servlet-name>HelloWeb</servlet-name>
<servlet-class>
org.springframework.web.servlet.DispatcherServlet
</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>HelloWeb</servlet-name>
<url-pattern>*.jsp</url-pattern>
</servlet-mapping>
</web-app>
10
The web.xml file will be kept in the WebContent/WEB-INF directory of your web
application. Upon initialization of HelloWeb DispatcherServlet, the framework will try to
load the application context from a file named [servlet-name]-servlet.xml located in the
application's WebContent/WEB-INFdirectory. In this case, our file will be
HelloWebservlet.xml.
<web-app...>
....
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/HelloWeb-servlet.xml</param-value>
</context-param>
<listener>
<listener-class>
org.springframework.web.context.ContextLoaderListener
</listener-class>
</listener>
</web-app>
Now, let us check the required configuration for HelloWeb-servlet.xml file, placed in your
web application's WebContent/WEB-INF directory –
<beans xmlns = "http://www.springframework.org/schema/beans"
= "http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation =
"http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd">
=
<bean class
"org.springframework.web.servlet.view.InternalResourceViewResolver">
<property name = "prefix" value = "/WEB-INF/jsp/" /> <property name =
"suffix" value = ".jsp" /> </bean>
</beans>
The DispatcherServlet delegates the request to the controllers to execute the functionality
specific to it. The @Controllerannotation indicates that a particular class serves the role of a
controller. The @RequestMappingannotation is used to map a URL to either an entire class
or a particular handler method.
@Controller
@RequestMapping("/hello")
@RequestMapping(method = RequestMethod.GET)
The @Controller annotation defines the class as a Spring MVC controller. Here, the first
usage of @RequestMapping indicates that all handling methods on this controller are
relative to the /hello path. Next annotation@RequestMapping(method =
RequestMethod.GET) is used to declare theprintHello() method as the controller's default
service method to handle HTTP GET request. You can define another method to handle any
POST request at the same URL.
You can write the above controller in another form where you can add additional attributes
in @RequestMapping as follows –
@Controller
return "hello";
The value attribute indicates the URL to which the handler method is mapped and the
method attribute defines the service method to handle HTTP GET request. The following
important points are to be noted about the controller defined above −
You will define required business logic inside a service method. You can call another
method inside this method as per requirement.
Based on the business logic defined, you will create a model within this method. You
can use setter different model attributes and these attributes will be accessed by the
view to present the final result. This example creates a model with its attribute
"message".
A defined service method can return a String, which contains the name of the view to
be used to render the model. This example returns "hello" as logical view name.
CHAPTER 7: CREATING JSP VIEWS:
Spring MVC supports many types of views for different presentation technologies. These
include - JSPs, HTML, PDF, Excel worksheets, XML, Velocity templates, XSLT, JSON,
Atom and RSS feeds, JasperReports, etc. But most commonly we use JSP templates written
with JSTL.
<html>
<head>
</head>
<body>
<h2>${message}</h2>
</body>
</html>
Here ${message} is the attribute which we have set up inside the Controller. You can have
multiple attributes to be displayed inside your view.