Struts Framework Vs Spring Web MVC Module
Struts Framework Vs Spring Web MVC Module
The main intention of Spring WEB MVC module is to prepare web applications with MVC design
patterns.
)To prepare web applications we have already Struts1 Framework then what is the requirement to use
Spring Web MVC Module?
Ans:
1. Struts is Web Framework, it able to provide very good environment to prepare and execute web
applications.
Spring Framework is an application Framework, it able to provide very good environment to prepare any
type of application like Standalone applications, web applications, Distributed applications,.....
2. Struts framework is mainly MVC based frame work, it able to use only MVC and its corelated design
pattern.
In Spring framework, only Web Module is able to use MVC Design Pattern.
4. Struts framework is able to provide tightly coupled design for its applications.
Spring framework is able to provide loosly coupled design.
5. Struts is not providing clear seperatin between Controller layer, Beans Model and View part.
Spring Framework is able to provide clear seperation between controller layer, Model and
View part.
6. Struts is more API dependent, it is very di cult to perform debugging and Testing. Spring is less API
dependent, it is very simple perform debugging and testing.
7. Struts is not providing very good environment to integrate other technology applications like JDBC,
Hibernate, EJBs,....
Spring is providing very good environment to integrate other technology applications like JDBC,
Hibernate,...
8. Struts is able to use only HTML, JSP ,... basic view related tech to prepare View part.
Spring is able to provide very good environment to use view related tech like HTML, JSP, velocity,
Freemarker,.....
9. Struts is not having AOP implementations to provide loosly coupled design. Spring is supporting AOP
implementations to provide loosly coupled design.
10.Struts is not layered/Modularized Framework. Spring is layered/Modularization Framework.
11.Struts is said to be invasive. In Struts we used to extend Action Classes and ActionForm classes.It
forces the programmer that, the programmer class must extend from the base class provided by Struts
API.
Spring Framework is said to be a non-
programmer to extend or implement their class from any prede ned class or interface
given by Spring API.
ffi
fi
12.Struts framework is able to provide very good Tag library to prepare view part. Spring framework is
not providing good tag library to prepare view part.
13.Spring is providing very good Transaction management and Messaging support for its applications.
Struts is not providing support for Transaction Management and Messaging Support.
To prepare MVC Based Web Applications we have already JSF[Java Server Faces] then what is the
requirement to use Spring Web MVC Framework?
1. JSF is web framework, it able to provide environment to prepare web applications only.
Spring Framework is an application Framework, it able to provide very good environment to prepare any
type of application like Standalone applications, web applications, Distributed applications,.....
2. JSF is Component based Framework, for every view , JSF will create a Component Tree inorder to
manage data.
Spring Framework is not Component based Framework, it will use Beans to manage Form data.
3. JSF is View layered Framework, It will focus on View Layer.
Spring is not View Layered Framework, it will focus on all layers of tghe Enterprise Applications.
4. JSF is having very good Convertors , Validations and Rendering Mechanisms.
Spring is not having good Convertors, validations and Renderring Mechanisms.
7. JSF is not having any integration environment to integrate other technologies applications like JDBC,
EJBs, RMI,....
Spring is providing very good environment to integrate other technologies
2. Spring framework allows Powerful and straightforward con guration of both framework and
application classes as JavaBeans. This con guration capability includes easy navidation across
contexts, such as from web controllers to business objects and validators.
3. Spring Framework is allowing Command and Form objects to reuse Business code instead of
extending Frameworks provided API libraries.
fi
fi
4. Spring Web MVC is providing very good Data Binding Mechanisms, Data Validations ... for web
applications.
5. Spring Web MVC is providing Customizable handler mapping and view resolution.
6. Spring Web MVC module is more Flexible model transfer. Model transfer with a
name/value Map supports easy integration with any view technology.
7. Spring Web MVC provides customizable locale and theme resolution, support for JSPs with
or without Spring tag library, support for JSTL, support for Velocity without the need for
extra bridges, and so on.
8. Spring Web MVC allows all JSP supported Tag Libraries like JSTL tag libraries,...
HandlerMapping is an interface that de nes a mapping between requests and handler objects.
1. View
2. web.xml
3. DispatcherServlet
4. HandlerMapping
fi
5. Controller Component
6. Command Class
7. View Resolver
8. Spring Con guration File
fi
From the above diagram,
1. Submitting request from Client to Servler Side web application, where
DispatcherServlet
will recieve request.
2. DispatcherServlet will interact with HandlerMapping to get Controller name and location.
3. HandlerMapping will return the name and location of the Controller class.
4. DispatcherServlet will recognize the name and location of the Controller class and perform loading
and instantiation of yje Controller class.
5. Depending on the Controller, Form data will be stored in Command Object and that Command object
data will be used in Controller class.
6. After executing Business logic, Controller class will return ModelAndView object, which contains
logical name of the view in View object and Model object is a Map contains Model Objects inorder to
use that data in View part.
7. DispatcherServlet will interact with ViewResolver inorder to get the name and location of the View
page.
8. ViewResolver will return View object with the name and location of the View page.
9. DispatcherServlet will interact witj View part with Model part inorder to prepare response.
10.View part will prepare response and submitting that response to DispatcherServlet.
11.DispatcherServlet will generate the required response to Client.
1.View :
Q)What is the requirement to use View part in Web applications? Ans:
1. It will improve Look And Feel to the web applications.
2. It will provide entry point for the users inorder to interact with applications.
3. It will provide very good environment to take data from users inorder to submit data to
the server side application.
4. It able to provide very good environment to perform Client Side data validations by using
Java Script functions.
5. It allows to specify di erent types of requests like GET, POST, HEAD,... to the web
applications.
There are two types of View parts are existed.
1. Informational View:
• It able to display messages to the users, status of the server side actions.
• It will not include forms, it will not take data from Users, simply it will display data to the
users.
2. Form Based View:
ff
It will include forms to take data from users and to submit that data to the Users.
To prepare View part, we will use View based Tech like AWT, SWING, JAVA FX, html, Jsp, velocity,
freemarker,......
Spring Framework is able to allow all the advanced view tech ,but, at basic level, We will use JSP tech
with Spring provided tag library.
2. web.xml[Deployment Descriptor]
web.xml is deployment descriptor, it will provide metadata about the web application which is required
by the container inorder to identify the server side components and inorder to execute server side
components.
In web applicatins, web.xml le is responsible for
1. welcome les con guration
3. Servlets Conf.
4. Filters Conf.
5. Listeners Conf.
6. Sessiontimeoutconf.
fi
fi
fi
7. Error Pages Conf.
9. Securityconf....
In Spring web MVC applicatins , web.xml le requirement is to con gure Font Controller that is
DispatcherServlet inorder to activate FrontController by Web containers.
In Spring Framework, DispatcherServlet is FrontControiller provided by Spring Framework in the form of
"org.springframework.web.servlet.DispatcherServlet".
Where Initialization Parameters are required to create Framework related objects like Handlermapping,
HandlerAdapter, ViewResolver,.....
Note: The default name and location of Spring con guration le is under WEB-INF folder with the name
"Servlet_Logical_Name-servlet.xml", but, if we want to change this name and location then we must use
"contextCon gLocation" initialization parameter in DispatcherServlet con guration.
EX:
<init-param>
2. <param-name>contextCon gLocation</param-name>
3. <param-value>/WEB-INF/con gd/mycon g.xml</param-value>
4. </init-param>
web.xml
1. <web-app>
2. <servlet>
3. <servlet-name>dispatcherServlet</servlet-name>
4. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
5. <load-on-startup>1</load-on-startup>
6. </servlet>
7. <servlet-mapping>
8. <servlet-name>dispatcherServlet</servlet-name>
9. <url-pattern>*.do</url-pattern> or
10. <url-pattern>/*</url-pattern>
11. </servlet-mapping>
12. </web-app>
3. DispatcherServlet:
The main intention of the Front Controller in web applications is ,
1. Getting request from clients
fi
2. Identifying the respective Model component or Business component which includes
business logic.
• BeanNameUrlHandlerMapping
• SimpleUrlHandlerMapping
• ControllerClassNameHandlerMapping
• CommonsPathMapHandlerMapping
5. Controller Component:
The main intention of Controller in Spring web MVC is to manage application logic or to have controller
logic to manage Service layer provided business methods access.
To prepare Controller classes in Spring web MVC applications Spring framework has provided the
following prede ned Library.
6. Command Class:
fi
The main intention of Command class is to store form data which is submitted by the client when we
use FormCommandControllers. It is like ActionForm component in Struts framework.
EX:
publicclassUser{
2. privateStringuname;
3. privateStringupwd;
4. setXXX()andgetXXX()
5. }
7. View Resolver:
In Spring MVC, view resolvers enable you to render models in a browser without tying you to a speci c
view technology like jsp
There are two interfaces that are important to the way Spring handles views
are ViewResolverand View. The ViewResolver provides a mapping between view names and actual
views. The View interface addresses the preparation of the request and hands the request over to one of
the view technologies.
<beanid class
>
2. <property name value - />
3. <property name value />
4. </bean>
4. PrepareSpringcon gurationFile
Examples:
app1
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
fi
app1
|---index.jsp
|---src
| |-----com.durgasoft.controllers.WishController.java |---WEB-INF
|-----web.xml |-----ds-servlet.xml |-----wish.jsp |-----classes
|----com.durgasoft.controllers.WishController.class
Annotations in Spring WEB MVC
Spring Framework has provided annotations support right from its Spring 2.5 version. By using
Anotations in SPring Web MVC applications we are able to reduce the following con gurations in spring
con guation le.
Spring Web MVC Frame work has provided the following Annotations mainly.
1. @Controller
2. @RequestMapping
3. @RequestParam
4. @SessionAttributes
2. @RequestMapping
@RequestMapping annotation is used for de ning the request urls based on the context root and HTTP
request methods like GET , POST, HEAD,... for the request.
This Annotation is used as either Class level annotation or Method level Annotation.
Syntax:
fi
fi
fi
fi
fi
fi
@RequestMapping(value="/---" , method=---)
Where value member will take Request URL with / pre x.
Where method member will take the constants like GET, POST, HEAD,... from RequestMethod enum.
3.@RequestParam:
@RequestParam annotation will be used in the methods to bind the method parameters to the request
parameters. If the request is submitted by the browser, request parameters are passed through the URL
query parameters. That can be easily mapped to the methods by annotating the method parameters
with @RequestParam.
Syntax:
@RequestParam("paramName")
EX:
@Controller
public class LoginController {
@RequestMapping(value="/login", method=RequestMethod.POST)
public ModelAndView checkLogin(@RequestParameter("uname") String uname,
@RequestParameer("upwd") String upwd){
------
return new ModelAndView("success");
}}
4.@SessionAttributes:
@SessionAttributes is used to de ne a variable name inorder to keep its Key-Value pair in Session
Scope and inorder to use that in multiple pages in the same web application.
fi
fi
Syntax:
@SessionAttributes("Param_Name")
EX:
@Controller
@SessionAttributes("status")
public class LoginController {
@RequestMapping(value="/login", method=RequestMethod.POST)
public ModelAndView checkLogin(@RequestParameter("uname") String uname,
@RequestParameer("upwd") String upwd, ModelMap map){ ------
map.addAttribute("status", "Login SUCCESS");
return new ModelAndView("success");
}}
CONTROLLER
Controller Classes
The main intention of Controller classes is to manage business logic or to include
code inorder access the business logic existed with Business components.
To prepare Controller classes, Spring framework has provided a set of prede ned
classes in "org.springframework.web.servlet.mvc" package an which are
implemented by Controller interface.
AbstractController
This Controller Classe will be used to prepare Controller classes where no form data submission in
request, but, it will dispay dynamic content through web pages
fi
When we click on "view pro le" link in facebook account, generating pro le data
When we click on "Get all Jobs" link JOB Portals
Note: In all the cases, no form submission is going on, but, dynamic content will be included in
response.
MultiAction Controller
3. Command Controllers:
Command Class:
Command Class is a normal Java Bean class, it can be instantiate by Spring WEB MVC framework
inorder to store form data which is submitted by the respective Client along with request.
4. SimpleFormController:
This controller class is able to support the Command classes inorder to store form data in Command
class objects when we submit user forms.
It is best option for Data Validations in Spring WEB MVC Framework, it will render the same page
automatically when validation errors are identi ed and it will forward to successView page when no
Validation messages are identi ed.
Data Validations
The process of checking whether the data is valid or not before using data in application Logic is called
as Data Validations.
There are two types of Data Validations. 1. Client Side Data Validations
2. Server Side Data Validations
1. Client Side Data Validations:
If we check whether data is valid or not at client browser after entering data in user forms and before
submitting form to Server then it is called as Client Side Data Validations.
To perform Client Side data Validations we will use Java Script functions.
2. Server Side Data Validations:
fi
fi
If we check whether the data is valid or not after storing data at server side and before using data in
application logic then it is called as Server side Data validations.
To perform Server side data validation we will use JAVA code at server side.
Spring is providing Server side Data validations in the following two ways. 1. By Using Spring provided
Error class.
2. By Using Java provided validation annotations
• HandlerMapping is a component in Spring Web MVC Framework, it will take the incoming request
URI and it will identify the mapped Controller class through Spring con guration le and return
that Controller class details to DispatcherServlet.
• Spring Web MVC has provided a set of prede ned HandlerMappings in the package like
"org.springframework.web.servlet.handler".
• Spring Web MVC has provided the following prede ned HandlerMapping classes.
• 1. BeanNameUrlHandlerMapping
2. SimpleUrlHandlerMapping
3. ControllerClassNameHandlerMapping
• 4. DefaultAnnotationHandlerMapping
4. DefaultAnnotationHandlerMapping:
org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping is a default
HandlerMapping class in Spring WEB MVC applications if we use Annotations , it is not required to
con gure in Spring con guration File.
It will map incoming request URI with the name provided along with the @RequestMapping annotation which we
provided above of the controller class or Controller class method.
HandlerInterceptor
fi
fi
fi
fi
fi
fi
In general, in web applications, to provide pre-processing and post-processing services for web resources like Servlets
and JSPs we will use "Servlet Filters".
In web applications, we are able to provide single lter for multiple Servlets and JSPs and we are able to provide
multiple lters for Single Servlet or JSP.
fi
fi