0% found this document useful (0 votes)
16 views

ch5

The document provides an overview of the Spring Framework and Struts, focusing on their architectures, features, and functionalities. It covers key concepts such as Inversion of Control, Aspect-Oriented Programming, and the MVC design pattern, detailing how these frameworks facilitate enterprise Java application development. Additionally, it compares Struts with traditional MVC approaches, highlighting advantages and disadvantages of using Struts in web application development.

Uploaded by

vivekkanet11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

ch5

The document provides an overview of the Spring Framework and Struts, focusing on their architectures, features, and functionalities. It covers key concepts such as Inversion of Control, Aspect-Oriented Programming, and the MVC design pattern, detailing how these frameworks facilitate enterprise Java application development. Additionally, it compares Struts with traditional MVC approaches, highlighting advantages and disadvantages of using Struts in web application development.

Uploaded by

vivekkanet11
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 12

J2EE 5. Spring,Strut B.C.A.

Sem-5

5 Spring,Strut
Topics Covered
1. Introduction to spring framework
2. spring architecture
3. spring and MVC
4. Spring context definition
5. Inversion of control in spring
6. Ascept oriented programming in spring
7. Understanding Struts Framework
8. Comparision with MVC using RequestDispatcher and the EL
9. Struts Flow of Control
10. Processing Requests with Action Objects
11. Handling Request Parameters with FormBeans
12. Prepopulating and Redisplaying Input Forms
13. Using Properties Files

1. Introduction to spring framework

 spring is the most popular application development framework for enterprise


Java.
 Millions of developers around the world use Spring Framework to create high
performing, easily testable, reusable code.
 Spring framework is an open source Java platform and it was initially written by Rod
Johnson and was first released under the Apache 2.0 license in June 2003.
 Spring is lightweight when it comes to size and transparency. The basic version of
spring framework is around 2MB.
 The core features of the Spring Framework can be used in developing any Java
application, but there are extensions for building web applications on top of the Java EE
platform.

Spring framework architecture


 spring could potentially be a one-stop shop for all your enterprise applications,
however, Spring is modular, allowing you to pick and choose which modules are
applicable to you, without having to bring in the rest.
 The Spring Framework provides about 20 modules which can be used based on an
application requirement.

Page 1 of 12
J2EE 5. Spring,Strut B.C.A. Sem-5

Module

 The Core Container consists of the Core, Beans, Context, and Expression Language
modules whose detail is as follows:
 The Core module provides the fundamental parts of the framework, including the IoC
and Dependency Injection features.
 The Bean module provides BeanFactory which is a sophisticated
implementation of the factory pattern.
 The Context module builds on the solid base provided by the Core and Beans modules
and it is a medium to access any objects defined and configured. The
ApplicationContext interface is the focal point of the Context module.
 The Expression Language module provides a powerful expression language for
querying and manipulating an object graph at runtime.

Data Access/Integration

 The Data Access/Integration layer consists of the JDBC, ORM, OXM, JMS and
Transaction modules whose detail is as follows:
 The JDBC module provides a JDBC-abstraction layer that removes the need to do
tedious JDBC related coding.
 The ORM module provides integration layers for popular object-relational mapping
APIs, including JPA, JDO, Hibernate, and iBatis.
 The OXM module provides an abstraction layer that supports Object/XML mapping
implementations for JAXB, Castor, XMLBeans, JiBX and XStream.
 The Java Messaging Service JMS module contains features for producing and
consuming messages.

Page 2 of 12
J2EE 5. Spring,Strut B.C.A. Sem-5

 The Transaction module supports programmatic and declarative transaction


management for classes that implement special interfaces and for all your POJOs.

Web

 The Web layer consists of the Web, Web-Servlet, Web-Struts, and Web-Portlet
modules whose detail is as follows:
 The Web module provides basic web-oriented integration features such as multipart
file-upload functionality and the initialization of the IoC container using servlet
listeners and a web-oriented application context.
 The Web-Servlet module contains Spring's model-view-controller
(MVC)

Miscellaneous

 There are few other important modules like AOP, Aspects, Instrumentation, Web and
Test modules whose detail is as follows:
 The AOP module provides aspect-oriented programming implementation allowing you
to define method-interceptors and pointcuts to cleanly decouple code that implements
functionality that should be separated.
 The Aspects module provides integration with AspectJ which is again a powerful and
mature aspect oriented programming (AOP) framework.
 The Instrumentation module provides class instrumentation support and class loader
implementations to be used in certain application servers.
 The Test module supports the testing of Spring components with JUnit or TestNG
frameworks.

Features of spring framework


Transaction management

 It provides a generic abstraction layer for transaction management.


 It allow the developer to add the pluggable transaction manager and making it easy.

JDBC exception handling

 It offers a meaningful exception hierarchy, which simplifies the error handling strategy.

AOP Framework

 Spring is best AOP framework.

MVC Framework

 It comes with MVC web application framework, built on core spring functionality.
 It is highly configurable via strategy interfaces and accommodates multiple view
technologies like JSP.

Page 3 of 12
J2EE 5. Spring,Strut B.C.A. Sem-5

Spring Architecture
 Spring is well organized architecture consisting of 7 modules.

1)Spring AOP:it is one of the key component of spring.AOP is used in spring.it is used to
provide declarative enterprise services.

2)Spring ORM:the ORM package is related to the database access.It provides integration
layers for popular object-relational mapping API’s.

3)Spring web:it is a part of spring’s web application development stack, which includes
spring MVC.

4)Spring DAO:The DAO(Data Access Object) support in spring is primarily for


standardizing the data access work using technologies like JDBC and hibernate.

5)Spring context:this package build on the beans package to add support for message
sources and for the observer design pattern.

6)Spring web MVC:this is the module which provides the MVC implementation for the
web application.

7)Spring Core:It is the most important component of spring framework.It provides the
Dependancy injection features.It seprates the dependencies like initialization, creation and
access of the objects.

Spring & MVC


Let us see the flow of spring MVC (3.2). I am not going to describe what is M,V,C hope you
already know that mess right ? so lets start with the flow…

Page 4 of 12
J2EE 5. Spring,Strut B.C.A. Sem-5

Spring MVC 3.2 Execution Flow


Step 1: First request will be received by DispatcherServlet
Step 2: DispatcherServlet will take the help of HandlerMapping and get to know the Controller
class name associated with the given request

The Spring Web model-view-controller (MVC) framework is designed around a


DispatcherServlet that handles all the HTTP requests and responses. The request processing
workflow of the Spring Web MVC DispatcherServlet is illustrated in the following diagram:

Following is the sequence of events corresponding to an incoming HTTP request to


DispatcherServlet:

1. After receiving an HTTP request, DispatcherServlet consults the HandlerMapping to


call the appropriate Controller.
2. The Controller takes the request and calls the appropriate service methods based on
used GET or POST method. The service method will set model data based on defined
business logic and returns view name to the DispatcherServlet.

Page 5 of 12
J2EE 5. Spring,Strut B.C.A. Sem-5

3. The DispatcherServlet will take help from ViewResolver to pickup the defined view for
the request.
4. Once view is finalized, The DispatcherServlet passes the model data to the view which
is finally rendered on the browser.

Step 3: So request transfer to the Controller, and then controller will process the request by
executing appropriate methods and returns ModeAndView object (contains Model data and
View name) back to the DispatcherServlet
Step 4: Now DispatcherServlet send the model object to the ViewResolver to get the actual
view page
Step 5: Finally DispatcherServlet will pass the Model object to the View page to display the
result

Spring ApplicationContext Container

 The Application Context is spring's more advanced container.


 Similar to BeanFactory it can load bean definitions, wire beans together and dispense
beans upon request.
 Additionally it adds more enterprise-specific functionality such as the ability to resolve
textual messages from a properties file and the ability to publish application events to
interested event listeners.
 This container is defined by the org.springframework.context.ApplicationContext
interface.
 The ApplicationContext includes all functionality of the BeanFactory, it is generally
recommended over the BeanFactory. BeanFactory can still be used for light weight
applications like mobile devices or applet based applications.
 The most commonly used ApplicationContext implementations are:

FileSystemXmlApplicationContext: This container loads the definitions of the beans


from an XML file. Here you need to provide the full path of the XML bean configuration file
to the constructor.
ClassPathXmlApplicationContext This container loads the definitions of the beans from
an XML file. Here you do not need to provide the full path of the XML file but you need to
set CLASSPATH properly because this container will look bean configuration XML file in
CLASSPATH.
WebXmlApplicationContext: This container loads the XML file with definitions of all
beans from within a web application.

Inversion of control in spring(IOC)


 In spring the IOC principle is implemented using the Dependency Injection(DI) design
pattern.

Dependency Injection

 Martin fowler is the inventor of DI in IOC.


 It is a general concept and it can be expressed in many different ways in the spring
framework.

Page 6 of 12
J2EE 5. Spring,Strut B.C.A. Sem-5

 The application classes in a complex java application should be independent so that the
other java classes can be reused and can be tested independently of other classes while
doing unit testing.
 DI joins these classes together and at the same time keeps them independent.
 DI can be well defined when it is distinguish separately in two parts.
 For example, class A is dependent on class B.The second part, injection states that all
this means is that class B will get injected into class A by the IOC.
 DI is implemented by passing parameters to the constructor.

Ascept oriented programming in spring


 one of the key components of Spring Framework is the Aspect oriented
programming (AOP)framework.
 It redefines the programming model of OOP.
 Aspect Oriented Programming entails breaking down program logic into distinct
parts called so-called concerns. The functions that span multiple points of an
application are called cross-cutting concerns and these cross-cutting concerns are
conceptually separate from the application's business logic.
 There are various common good examples of aspects like logging, auditing, declarative
transactions, security, and caching etc.
 The key unit of modularity in OOP is the class, whereas in AOP the unit of modularity
is the aspect.
 Spring AOP module provides interceptors to intercept an application, for example,
when a method is executed, you can add extra functionality before or after the method
execution.
 The DI helps to decouple the application objects from each other.
 AOP provide declarative enterprise services, especially as a replacement for EJB
declarative services like declarative transaction management.
 It allow users to implement custom aspects, complementing their use of OOP with
AOP.

AOP Terminologies

 Before we start working with AOP, let us become familiar with the AOP concepts and
terminology. These terms are not specific to Spring, rather they are related to AOP.

Terms Description

A module which has a set of APIs providing cross-cutting


Aspect requirements. For example, a logging module would be called
AOP aspect for logging. An application can have any number of
aspects depending on the requirement.

This represents a point in your application where you can plug-in


Join point AOP aspect. You can also say, it is the actual place in the
application where an action will be taken using Spring AOP
framework.
This is the actual action to be taken either before or after the
Advice method execution. This is actual piece of code that is invoked
during program execution by Spring AOP framework.

Page 7 of 12
J2EE 5. Spring,Strut B.C.A. Sem-5

This is a set of one or more joinpoints where an advice should be


Pointcut executed. You can specify pointcuts using expressions or patterns
as we will see in our AOP examples.

Introduction An introduction allows you to add new methods or attributes to


existing classes.

Understanding Struts Framework


 Struts2 is popular and mature web application framework based on the MVC design
pattern. Struts2 is not just the next version of Struts 1, but it is a complete rewrite of the
Struts architecture.
 The strut is introduced in may 2000 which provide a standard MVC framework.
 In simple MVC framework we know that we can create nice separation between the code
that creates the data with the use of java code that represents the data with the use of
HTML and JSP.
 It is a free open source framework, used to create java web application.
 It is differ from because it provides dynamic response.
 Web application based on JSP includes database code, page design, and control flow
together. these type of larger applications sometimes become difficult to maintain.
 We can separate these code by using MVC architecture.
 The strut framework is designed to help developers create web applications that utilize
MVC architecture.
 The WebWork framework started off with Struts framework as the basis and its goal was
to offer an enhanced and improved framework built on Struts to make web development
easier for the developers.
 The framework provides three key component:
1) Tag library
 That helps developer to create interactive form-based application with sesrver
pages.
2) Request handler
 Provided by the application developer that is mapped to a standard URL.
3) Response handler
 Transfers control to another resource which complete the response.

Comparison with MVC using RequestDispatcher and the EL


 The following comparison shows the advantages and disadvantages using strut as
compared to RequestDispatcher and the EL
Advantages
1) Centralized file based configuration
 by this feature we can achieve loose coupling.that means many changes can be
done without modifying or recompiling source code and most of the changes
made by editing single file.
 By this feature web developer focus on their specific task without knowing
about the overall system.
2) Form beans
 in compare to JSP strut extends capabilities of adding java beans component
which servers to really simplify the processing of request parameter.

Page 8 of 12
J2EE 5. Spring,Strut B.C.A. Sem-5

3) Bean tags
 Strut provides a set of custom JSP tags that allows you to easily output the
properties of JavaBean component.
4) HTML tags
 Strut provide a set of custom JSP tag to create HTML forms that are related to
javabean components.
 It allows us to get initial form-field values from java objects and redisplay
forms with some or all previously entered values intact.
5) Form field validation
 It has built in capability for checking that form values are in the required
format which is known as form field validation.
 If values are missing or in improper format, the form can be automatically
reappeared with error message.
 The validation can be performed either on the server or both server and client.
6) Consistent approach
 Strut push you for regular use of MVC throughout your application.

Disadvantages
1) Bigger learning curve
 To use MVC with struts one has to be comfortable with the standard JSP and
servlet API and a large and complicated framework that is almost equal in size
to the core system.
 It is specially significant with smaller projects, near-term deadlines and less
experience developers mush time can be spent learning struts for building an
actual system.
2) Worse documentation
 With compared to the servlet and JSP API, strut has fewer online resources,
and many first time users find the online apache documentation confusing and
poorly organized.
3) Less transparent
 Strut applications are harder to understand.

Struts Flow of Control


1) the user requests a form
 the input form is built with the strut html tags.
 These tags associate a bean with the form so that you can pre populate the
form with values from your application, so that you can redisplay incorrectly
filled out forms.
2) the form is submitted to a url of the form blah.do
 the form contains an ACTION ending in blah.do.
 the strut system receives the request, where an action mapping in struts-
config.xml associates the address with an action object.
3) the execute method of the action object is invoked
 one of the arguments to execute is a form bean that is automatically created
and whose properties are automatically populated with the incoming form

Page 9 of 12
J2EE 5. Spring,Strut B.C.A. Sem-5

data.
 Once it examines the user input data in the bean, the execute method invokes
business logic and data access logic, placing the result in normal beans stored
in the request.
4) strut forward the requests to the appropriate JSP page
 strut normally invokes the result page with RequestDispatcher.
 Forward although request.sendRedirect is occasionally used.In most real
applications, the results page uses bean:write.

Processing Request with Action Objects


 There are six basic steps in processing requests with action objects.
1) Modify struts-config.xml
 Use WEB-INF/struts-config.xml to:
i. Designate action classes to handle requests for xyz.do
ii. Specify the urls that apply in various situation
iii. Declare any form beans that are being used.
 Be sure to restart the server after modifying struts-config.xml; the file is read
only when the web application is first loaded.
2) Define a form bean
 This bean is a class the extends ActionForm and will represent the data
submitted by the user.It is automatically populated when the input form is
submitted.
3) Create results beans
 In the MVC architecture, the business logic and data access code create the
results and the JSP pages present them.
 To transfer the results from one layer to the other, they are stored in beans.
4) Create an Action object to handle requests
 The struts-config.xml file designates the action object that handles requests for
the various URLs.
 The action objects themselves need to do the real work; invoke the appropriate
business and data access logic store the results in the beans.
5) Create form that invokes xyz.do

Page 10 of 12
J2EE 5. Spring,Strut B.C.A. Sem-5


You need to create an input form whose ACTION corresponds to one of the
*.do addresses listed in struts-config.xml.
 At a minimum, the input form should look like this:
<Form action=”…/xyz.do”…>…</form>
6) Display result in JSP
 Strut is built around the MVC architecture, these JSP pages should avoid JSP
scripting elements whenever possible.

Handling Request Parameters with FormBeans


 We call request.getParameter to obtain the form data.
 We used this manual approach because we wanted to concentrate only on the action
mappings, forward entries and structure of the action class.
 In a real application this request.getparameter give the error.
 Strut has a convenient tool for this process is form beans.
 When a user submit a request, the system institate a bean.
Understanding Form beans
Form beans are playing a important role in the strut framework.following are the steps for
define Form Beans in strut-config.xml
1)Declare the form beans
To design form bean, to handle input data, add a form-bean entry to the form beans element
in struts-config.xml.position the form-beans elements before the action-mapping
elements.when you define a bean, you should also identify the fully qualified class name.
<form-beans>
<form-bean name=”exampleFormBean”
Type=”coreservlets.exampleFormBean”/>
</form-beans>

2)Update the action declaration


After using form-bean to give your bean a name, you need to change your action
element.Normally you need to add name and scope attributes to the action element.
<action path=”/register”
Type=”coreservlets.ActionWithBean”
Name=”exampleFormBean”
Scope=”request”>

Writing an Action Form with form beans


1)Extend the ActionForm class
All form beans must extend the ActionForm class.
2)Zero argument constructor
When you instantiating the bean, the system always calls the default constructor.
3)Set bean properties that corresponds to the request parameter names
The bean must have setXXX method corresponding to each incoming request parameter
named xxx.
4)Get bean properties for each property that you want to output in the JSP page.
The bean must have a getXXX method related to each bean property that you want to
display in JSP.after the action process request it routes the request to the output JSP page.

PREPOPULATING AND REDISPLAYING INPUT FORMS:


 In this section, let’s see how to associate form beans with the input page using the
Structs html: tags.
Page 11 of 12
J2EE 5. Spring,Strut B.C.A. Sem-5

 Associating form beans with input forms having three advantages.


1) Request parameter names and the bean property names stay in sync:
 If you write the form by your own selves, you might change the
parameter names but forget to change the bean.
 When using the html:tags, you explicitly refer to the bean property
names when defining text fields or other input elements ,so if you use the
wrong name, you get an immediate error message.
2) Text fields with initial values:
 By associating a bean with the input page, the initial text field values can
from your application.
 So, for example, if the available things change in your database, you
don’t have to separately remember to change hard-coded HTML on the
page.
3) Redisplay values in the text fields:
 If you send the user back to the original form, the originally entered
values are automatically filled in.
 That’s because the form is already associated with a bean, but now the
bean is the one that was just created from the request.

Using Properties Files:


 Properties files are just text files that contain name, value pairs, and key message pairs.
 By using bean: message from the Structs tag library, you can easily retrieve messages
from your properties file and display them on your JSP pages.
Advantage of Properties Files:
1) Centralized updates:
 If the application uses a message in several JSP pages, with properties
files you can easily update a message with a single change.
 This capability is consistent with the Struts of making as many changes
as possible in configuration files, not in Java or JSP code.
 In short if we do the change at one place it reflects to other.
2)Internationalization
 If you use messages in your JSP pages, you can easily internationalize
your application by having multiple properties file.simply create a file
for each language that requires support.For instance to support
English,Japanese etc.

Page 12 of 12

You might also like