SlideShare a Scribd company logo
1 of 24
Download to read offline
Using Struts 2 and Spring
Frameworks Together
Bruce Phillips
University of Kansas
March 2009


 Presentation and code examples available at
   http://www.brucephillips.name/spring
References
 Spring Framework -
 http://www.springsource.org/documentation
 Spring Community Forums -
 http://forum.springsource.org/
 Introduction to the Spring Framework 2.5 -
 http://www.theserverside.com/tt/articles/article.ts
 s?l=IntrotoSpring25
 Spring in Action, 2nd Edition, Manning Publishing,
 August 2007
 Pro Spring 2.5, Apress Publishing, August 2008
 Struts 2 Framework - http://struts.apache.org

                                                   2
References
 Using Struts 2 and Spring Frameworks Together, Bruce
 Phillips Blog -
 http://www.brucephillips.name/blog/index.cfm/2008/10/17/Usi
 ng-Struts-2-and-Spring-Frameworks-Together
 Introduction to the Java Spring Framework,
 http://www.brucephillips.name/blog/index.cfm/2009/1/29/Intr
 oduction-to-the-Java-Spring-Framework
 Struts 2 In Practice, Manning Publishing,
 http://www.manning.com/wannemacher/
 Apache Struts 2 Documentation, Spring Plugin -
 http://struts.apache.org/2.x/docs/spring-plugin.html
 Struts User Forum - http://www.nabble.com/Struts---User-
 f206.html
 Maven - http://maven.apache.org/
 Maven - http://books.sonatype.com/maven-book/index.html

                                                           3
Spring and Struts 2 Frameworks
 Introduction to the Java Spring
 Framework
 ◦ http://www.brucephillips.name/blog/index.cfm/
   2009/1/29/Introduction-to-the-Java-Spring-
   Framework
 Introduction to The Struts 2 Java Web
 Application Framework
 ◦ http://www.brucephillips.name/blog/index.cfm/
   2008/10/7/Introduction-toThe-Struts-2-Java-
   Web-Application-Framework

                                               4
Advantages of Using
Spring and Struts 2 Together
 Leverage the strengths of both
 frameworks
 Spring
 ◦ Dependency management
 ◦ Aspect-Oriented Programming (AOP)
 ◦ Spring JDBC
 Struts 2
 ◦ Simplifies implementing the model, view,
   controller (MVC) pattern in a web application

                                                   5
Example Applications
 Struts2_Spring_Example
 ◦ Simple Struts 2 application that uses Spring to
   manage dependencies
 ContactsWebApp
 ◦ More complicated Struts 2 application that
   uses Spring to manage dependencies,
   implement cross-cutting (AOP) concerns, and
   interact with a database repository
 Code and installation instructions at
 ◦ http://www.brucephillips.name/spring

                                                     6
Classes Depend On Other Classes
          Developers must “manage” these class dependencies (coupling)

                        Struts ActionSupport Classes


 CreatePhone                CreateContact              CreateEmail




                                                               Service Classes


 PhoneService               ContactService             EmailService


           ContactsWebApp –
           sampling of class
           dependencies                                      Persistence Classes


PhoneDataAccess           ContactDataAccess         EmailDataAccess

                                                                                   7
Struts Without Spring
 Struts web applications tend to have quite a few
 “controller” (ActionSupport) classes
 These “controller” classes call upon other classes to
 do the work
 Without Spring must use concrete object
 instantiation
 ◦ ContactService contactService = new
   ContactServiceImpl();
 ◦ The same code will be repeated in all the other Struts
   ActionSupport classes that use a ContactService object
 Each class is responsible for managing its
 dependencies
 ◦ More difficult to configure, maintain, and change


                                                            8
Struts With Spring
 Use Spring to manage dependencies
 ◦ Dependencies are specified and managed in
   one place
 ◦ Much easier to change dependencies
 ◦ Most dependencies can be “auto-wired”
 Use the other benefits Spring provides
 ◦ Aspect Oriented Programming (AOP)
 ◦ Spring JDBC
 ◦ Transaction Management

                                               9
Jars Required
 See Struts2_Spring_Example application
 ◦ jars are under Referenced Libraries
 ◦ spring.jar is needed to use Spring capabilities
 ◦ struts2-spring-plugin-2.1.6.jar is needed so
   that Struts 2 can use Spring




                                                     10
How To Integrate Spring Into Struts
 Changes to web.xml
 ◦ Add ContextLoaderListener and
   RequestContextListener to web.xml
 Add applicationContext.xml
 ◦ Under WEB-INF
 ◦ Spring configuration and bean definitions




                                               11
Use Spring to Manage
Dependencies
 There are two different ways to have
 Spring manage the dependencies in a
 Struts application
 ◦ Configure your actions as Spring beans
    applicationContext.xml
 ◦ Configure your actions in struts.xml
    applicationContext_alternate.xml
 ◦ In both cases use Spring to manage
   dependencies between your ActionSupport
   classes and other classes
                                             12
Struts2_Spring_Example
 Register class is a Struts ActionSupport class
 ◦ Register class depends upon a PersonService
   class
 Option 1 use Spring to create and manage
 the Register objects
    See applicationContext.xml
      Note the id value for the Register bean
    Register class requires a PersonService object and
    Spring will manage this dependency
    In struts.xml create the action node but for the class
    value use the id value specified in
    applicationContext.xml


                                                             13
Struts2_Spring_Example
 Option 2
 ◦ See struts_alternate.xml
    Create action node as usual by specifying the complete
    class for the class attribute
 ◦ See applicatonContext_alternate.xml
    Just create those beans for classes that are NOT Struts
    ActionSupport classes
 ◦ Spring will manage any dependencies for the
   ActionSupport classes
    Register class depends on PersonService class
    Before a Register object is provided to the application,
    Spring will pass a PersonService object to class
    Register’s setPersonService method


                                                               14
Struts 2 and Spring
Configuration Options
 How both option 1 and option 2 manage
 dependencies can be configured to bypass
 the defaults
 ◦ See chapter 3 Struts 2 In Practice
 ◦ See Struts 2 Spring plugin documentation at:
    http://struts.apache.org/2.x/docs/spring-plugin.html




                                                           15
ContactsWebApp
 Very simple contacts manager
 Web application that uses Struts 2 and
 Spring
 ◦ Database is embedded Derby
 Uses Struts to manage ActionSupport
 classes (option 2) and Spring to:
 ◦ Manage dependencies
 ◦ Implement cross-cutting concerns such as logging,
   performance timing
 ◦ Interact with database repository (Spring JDBC)

                                                   16
ContactsWebApp – FindContact and its dependencies

  ActionSupport         FindContact
  Class



    Service         ContactSearchService
    Layer




                     ContactDataAccess
Persistence Layer




PhoneDataAccess       EmailDataAccess      PersonDataAccess




                         DataSource
                                                              17
ContactsWebApp – struts.xml and
applicationContext.xml
 struts.xml
 ◦ Action nodes specify complete class value
 ◦ For example FindContact
    Note class FindContact needs a
    ContactSearchService object and has a public
    setContactSearchService method
 applicationContext.xml
 ◦ Spring configuration
 ◦ Create Spring beans for all non-
   ActionSupport classes that are used by the
   ActionSupport class
                                                   18
Struts 2 – Spring Interaction
applicationContext.xml
 Spring will provide any objects needed by
 the ActionSupport classes automatically
  ◦ Autowire “by name”
  ◦ For example FindContacts property
    contactSearchService will get a value because
    Spring will call the setContactSearchService
    method and pass it a contactSearchService object
 Spring is also managing dependencies for the
 other classes
  ◦ For example see contactSearchService,
    contactDataAccess and personDataAccess beans
  ◦ Be sure to specify scope=“request”

                                                   19
Spring AOP
 Can use Spring AOP just as we did in the
 non-Struts 2 Contact application
 Same configuration in the Struts 2
 project’s applicationContext.xml as we
 had in the Contact (non-web) project’s
 applicationContext_DB_Repository.xml
 See package edu.ku.si.contact.advice



                                            20
AOP Example - Exception Logging
 Need to specify where to apply the
 advice
 ◦ See ExceptionLogging class PointCut
   definition
 ◦ Don’t need to apply advice to the
   ActionSupport classes because they won’t be
   the originator of any Exceptions
 Rename c:/derby/contactsDB to
 c:/derby/tcontactsDB
 Run ContactsWebApp

                                                 21
Spring JDBC
 Can use Spring JDBC as we did in the
 Contact (non-Struts 2) application
 See package edu.ku.si.contact.data
 The data access classes extend Spring
 JDBC’s SimpleJdbcDaoSupport
 ◦ See PersonDataAccess
 ◦ Much simpler to query database and process
   results
 ◦ Note Spring JDBC will manage opening and
   closing connections, handling exceptions, etc.

                                                    22
Data Source Management
 The data access classes need a Data Source
 object
 ◦ See applicationContext.xml
 ◦ Note no scope is specified for the dataSource
   bean
 By extending SimpleJdbcDaoSupport the
 data access classes inherit the setDataSource
 method which Spring uses to inject the data
 source object
 To use connection pooling see notes in
 applicationContext.xml
                                                   23
Why Use Spring With Struts 2?
 Significantly reduces coupling between
 classes
 Central management of dependencies
 Applications are easier to maintain and
 change
 Leverage Spring features that Struts 2
 doesn’t provide
 ◦ Aspect-oriented programming
 ◦ Spring JDBC
                                           24

More Related Content

Similar to Struts 2 And Spring Frameworks Together

Spring from a to Z
Spring from  a to ZSpring from  a to Z
Spring from a to Zsang nguyen
 
Struts2 course chapter 1: Evolution of Web Applications
Struts2 course chapter 1: Evolution of Web ApplicationsStruts2 course chapter 1: Evolution of Web Applications
Struts2 course chapter 1: Evolution of Web ApplicationsJavaEE Trainers
 
Introduction to Struts 1.3
Introduction to Struts 1.3Introduction to Struts 1.3
Introduction to Struts 1.3Ilio Catallo
 
Struts An Open-source Architecture for Web Applications
Struts An Open-source Architecture for Web ApplicationsStruts An Open-source Architecture for Web Applications
Struts An Open-source Architecture for Web Applicationselliando dias
 
Struts & spring framework issues
Struts & spring framework issuesStruts & spring framework issues
Struts & spring framework issuesPrashant Seth
 
Krazykoder struts2 spring_hibernate
Krazykoder struts2 spring_hibernateKrazykoder struts2 spring_hibernate
Krazykoder struts2 spring_hibernateKrazy Koder
 
Spring framework Controllers and Annotations
Spring framework   Controllers and AnnotationsSpring framework   Controllers and Annotations
Spring framework Controllers and AnnotationsAnuj Singh Rajput
 
Android Architecture Components
Android Architecture ComponentsAndroid Architecture Components
Android Architecture ComponentsDarshan Parikh
 
Java J2EE Interview Questions Part 2
Java J2EE Interview Questions Part 2Java J2EE Interview Questions Part 2
Java J2EE Interview Questions Part 2javatrainingonline
 

Similar to Struts 2 And Spring Frameworks Together (20)

141060753008 3715301
141060753008 3715301141060753008 3715301
141060753008 3715301
 
Spring from a to Z
Spring from  a to ZSpring from  a to Z
Spring from a to Z
 
Struts2 course chapter 1: Evolution of Web Applications
Struts2 course chapter 1: Evolution of Web ApplicationsStruts2 course chapter 1: Evolution of Web Applications
Struts2 course chapter 1: Evolution of Web Applications
 
Struts
StrutsStruts
Struts
 
Struts 1
Struts 1Struts 1
Struts 1
 
Skillwise Struts.x
Skillwise Struts.xSkillwise Struts.x
Skillwise Struts.x
 
Introduction to Struts 1.3
Introduction to Struts 1.3Introduction to Struts 1.3
Introduction to Struts 1.3
 
Struts An Open-source Architecture for Web Applications
Struts An Open-source Architecture for Web ApplicationsStruts An Open-source Architecture for Web Applications
Struts An Open-source Architecture for Web Applications
 
Struts & spring framework issues
Struts & spring framework issuesStruts & spring framework issues
Struts & spring framework issues
 
Spring MVC 5 & Hibernate 5 Integration
Spring MVC 5 & Hibernate 5 IntegrationSpring MVC 5 & Hibernate 5 Integration
Spring MVC 5 & Hibernate 5 Integration
 
Krazykoder struts2 spring_hibernate
Krazykoder struts2 spring_hibernateKrazykoder struts2 spring_hibernate
Krazykoder struts2 spring_hibernate
 
Struts Ppt 1
Struts Ppt 1Struts Ppt 1
Struts Ppt 1
 
Hello Android
Hello AndroidHello Android
Hello Android
 
Spring framework Controllers and Annotations
Spring framework   Controllers and AnnotationsSpring framework   Controllers and Annotations
Spring framework Controllers and Annotations
 
Model viewviewmodel2
Model viewviewmodel2Model viewviewmodel2
Model viewviewmodel2
 
Struts ppt 1
Struts ppt 1Struts ppt 1
Struts ppt 1
 
Struts
StrutsStruts
Struts
 
Android Architecture Components
Android Architecture ComponentsAndroid Architecture Components
Android Architecture Components
 
Java J2EE Interview Questions Part 2
Java J2EE Interview Questions Part 2Java J2EE Interview Questions Part 2
Java J2EE Interview Questions Part 2
 
Java J2EE Interview Question Part 2
Java J2EE Interview Question Part 2Java J2EE Interview Question Part 2
Java J2EE Interview Question Part 2
 

More from Syed Shahul

Beginning java and flex migrating java, spring, hibernate, and maven develop...
Beginning java and flex  migrating java, spring, hibernate, and maven develop...Beginning java and flex  migrating java, spring, hibernate, and maven develop...
Beginning java and flex migrating java, spring, hibernate, and maven develop...Syed Shahul
 
Manning Struts 2 In Action
Manning Struts 2 In ActionManning Struts 2 In Action
Manning Struts 2 In ActionSyed Shahul
 
Sahih Bukhari - Tamil
Sahih Bukhari - TamilSahih Bukhari - Tamil
Sahih Bukhari - TamilSyed Shahul
 
Hibernate Interview Questions
Hibernate Interview QuestionsHibernate Interview Questions
Hibernate Interview QuestionsSyed Shahul
 
Step By Step Guide For Buidling Simple Struts App
Step By Step Guide For Buidling Simple Struts AppStep By Step Guide For Buidling Simple Struts App
Step By Step Guide For Buidling Simple Struts AppSyed Shahul
 
Hibernate Reference
Hibernate ReferenceHibernate Reference
Hibernate ReferenceSyed Shahul
 
Hibernate Tutorial
Hibernate TutorialHibernate Tutorial
Hibernate TutorialSyed Shahul
 
Spring Live Sample Chapter
Spring Live Sample ChapterSpring Live Sample Chapter
Spring Live Sample ChapterSyed Shahul
 
Spring Reference
Spring ReferenceSpring Reference
Spring ReferenceSyed Shahul
 

More from Syed Shahul (14)

Beginning java and flex migrating java, spring, hibernate, and maven develop...
Beginning java and flex  migrating java, spring, hibernate, and maven develop...Beginning java and flex  migrating java, spring, hibernate, and maven develop...
Beginning java and flex migrating java, spring, hibernate, and maven develop...
 
Manning Struts 2 In Action
Manning Struts 2 In ActionManning Struts 2 In Action
Manning Struts 2 In Action
 
Sahih Bukhari - Tamil
Sahih Bukhari - TamilSahih Bukhari - Tamil
Sahih Bukhari - Tamil
 
Hibernate Interview Questions
Hibernate Interview QuestionsHibernate Interview Questions
Hibernate Interview Questions
 
Struts Live
Struts LiveStruts Live
Struts Live
 
Struts Intro
Struts IntroStruts Intro
Struts Intro
 
Step By Step Guide For Buidling Simple Struts App
Step By Step Guide For Buidling Simple Struts AppStep By Step Guide For Buidling Simple Struts App
Step By Step Guide For Buidling Simple Struts App
 
Hibernate Reference
Hibernate ReferenceHibernate Reference
Hibernate Reference
 
Hibernate Tutorial
Hibernate TutorialHibernate Tutorial
Hibernate Tutorial
 
Spring Live Sample Chapter
Spring Live Sample ChapterSpring Live Sample Chapter
Spring Live Sample Chapter
 
Spring Reference
Spring ReferenceSpring Reference
Spring Reference
 
Jamon 22
Jamon 22Jamon 22
Jamon 22
 
Do U Know Him
Do U Know HimDo U Know Him
Do U Know Him
 
Hot Chocolate
Hot ChocolateHot Chocolate
Hot Chocolate
 

Recently uploaded

Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Wonjun Hwang
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebUiPathCommunity
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Scott Keck-Warren
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Enterprise Knowledge
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDGMarianaLemus7
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024BookNet Canada
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfngoud9212
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentationphoebematthew05
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024The Digital Insurer
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machinePadma Pradeep
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 

Recently uploaded (20)

Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
Bun (KitWorks Team Study 노별마루 발표 2024.4.22)
 
Dev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio WebDev Dives: Streamline document processing with UiPath Studio Web
Dev Dives: Streamline document processing with UiPath Studio Web
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptxVulnerability_Management_GRC_by Sohang Sengupta.pptx
Vulnerability_Management_GRC_by Sohang Sengupta.pptx
 
Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024Advanced Test Driven-Development @ php[tek] 2024
Advanced Test Driven-Development @ php[tek] 2024
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024Designing IA for AI - Information Architecture Conference 2024
Designing IA for AI - Information Architecture Conference 2024
 
APIForce Zurich 5 April Automation LPDG
APIForce Zurich 5 April  Automation LPDGAPIForce Zurich 5 April  Automation LPDG
APIForce Zurich 5 April Automation LPDG
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
New from BookNet Canada for 2024: BNC BiblioShare - Tech Forum 2024
 
Bluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdfBluetooth Controlled Car with Arduino.pdf
Bluetooth Controlled Car with Arduino.pdf
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
costume and set research powerpoint presentation
costume and set research powerpoint presentationcostume and set research powerpoint presentation
costume and set research powerpoint presentation
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024My INSURER PTE LTD - Insurtech Innovation Award 2024
My INSURER PTE LTD - Insurtech Innovation Award 2024
 
Install Stable Diffusion in windows machine
Install Stable Diffusion in windows machineInstall Stable Diffusion in windows machine
Install Stable Diffusion in windows machine
 
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptxE-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
E-Vehicle_Hacking_by_Parul Sharma_null_owasp.pptx
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 

Struts 2 And Spring Frameworks Together

  • 1. Using Struts 2 and Spring Frameworks Together Bruce Phillips University of Kansas March 2009 Presentation and code examples available at http://www.brucephillips.name/spring
  • 2. References Spring Framework - http://www.springsource.org/documentation Spring Community Forums - http://forum.springsource.org/ Introduction to the Spring Framework 2.5 - http://www.theserverside.com/tt/articles/article.ts s?l=IntrotoSpring25 Spring in Action, 2nd Edition, Manning Publishing, August 2007 Pro Spring 2.5, Apress Publishing, August 2008 Struts 2 Framework - http://struts.apache.org 2
  • 3. References Using Struts 2 and Spring Frameworks Together, Bruce Phillips Blog - http://www.brucephillips.name/blog/index.cfm/2008/10/17/Usi ng-Struts-2-and-Spring-Frameworks-Together Introduction to the Java Spring Framework, http://www.brucephillips.name/blog/index.cfm/2009/1/29/Intr oduction-to-the-Java-Spring-Framework Struts 2 In Practice, Manning Publishing, http://www.manning.com/wannemacher/ Apache Struts 2 Documentation, Spring Plugin - http://struts.apache.org/2.x/docs/spring-plugin.html Struts User Forum - http://www.nabble.com/Struts---User- f206.html Maven - http://maven.apache.org/ Maven - http://books.sonatype.com/maven-book/index.html 3
  • 4. Spring and Struts 2 Frameworks Introduction to the Java Spring Framework ◦ http://www.brucephillips.name/blog/index.cfm/ 2009/1/29/Introduction-to-the-Java-Spring- Framework Introduction to The Struts 2 Java Web Application Framework ◦ http://www.brucephillips.name/blog/index.cfm/ 2008/10/7/Introduction-toThe-Struts-2-Java- Web-Application-Framework 4
  • 5. Advantages of Using Spring and Struts 2 Together Leverage the strengths of both frameworks Spring ◦ Dependency management ◦ Aspect-Oriented Programming (AOP) ◦ Spring JDBC Struts 2 ◦ Simplifies implementing the model, view, controller (MVC) pattern in a web application 5
  • 6. Example Applications Struts2_Spring_Example ◦ Simple Struts 2 application that uses Spring to manage dependencies ContactsWebApp ◦ More complicated Struts 2 application that uses Spring to manage dependencies, implement cross-cutting (AOP) concerns, and interact with a database repository Code and installation instructions at ◦ http://www.brucephillips.name/spring 6
  • 7. Classes Depend On Other Classes Developers must “manage” these class dependencies (coupling) Struts ActionSupport Classes CreatePhone CreateContact CreateEmail Service Classes PhoneService ContactService EmailService ContactsWebApp – sampling of class dependencies Persistence Classes PhoneDataAccess ContactDataAccess EmailDataAccess 7
  • 8. Struts Without Spring Struts web applications tend to have quite a few “controller” (ActionSupport) classes These “controller” classes call upon other classes to do the work Without Spring must use concrete object instantiation ◦ ContactService contactService = new ContactServiceImpl(); ◦ The same code will be repeated in all the other Struts ActionSupport classes that use a ContactService object Each class is responsible for managing its dependencies ◦ More difficult to configure, maintain, and change 8
  • 9. Struts With Spring Use Spring to manage dependencies ◦ Dependencies are specified and managed in one place ◦ Much easier to change dependencies ◦ Most dependencies can be “auto-wired” Use the other benefits Spring provides ◦ Aspect Oriented Programming (AOP) ◦ Spring JDBC ◦ Transaction Management 9
  • 10. Jars Required See Struts2_Spring_Example application ◦ jars are under Referenced Libraries ◦ spring.jar is needed to use Spring capabilities ◦ struts2-spring-plugin-2.1.6.jar is needed so that Struts 2 can use Spring 10
  • 11. How To Integrate Spring Into Struts Changes to web.xml ◦ Add ContextLoaderListener and RequestContextListener to web.xml Add applicationContext.xml ◦ Under WEB-INF ◦ Spring configuration and bean definitions 11
  • 12. Use Spring to Manage Dependencies There are two different ways to have Spring manage the dependencies in a Struts application ◦ Configure your actions as Spring beans applicationContext.xml ◦ Configure your actions in struts.xml applicationContext_alternate.xml ◦ In both cases use Spring to manage dependencies between your ActionSupport classes and other classes 12
  • 13. Struts2_Spring_Example Register class is a Struts ActionSupport class ◦ Register class depends upon a PersonService class Option 1 use Spring to create and manage the Register objects See applicationContext.xml Note the id value for the Register bean Register class requires a PersonService object and Spring will manage this dependency In struts.xml create the action node but for the class value use the id value specified in applicationContext.xml 13
  • 14. Struts2_Spring_Example Option 2 ◦ See struts_alternate.xml Create action node as usual by specifying the complete class for the class attribute ◦ See applicatonContext_alternate.xml Just create those beans for classes that are NOT Struts ActionSupport classes ◦ Spring will manage any dependencies for the ActionSupport classes Register class depends on PersonService class Before a Register object is provided to the application, Spring will pass a PersonService object to class Register’s setPersonService method 14
  • 15. Struts 2 and Spring Configuration Options How both option 1 and option 2 manage dependencies can be configured to bypass the defaults ◦ See chapter 3 Struts 2 In Practice ◦ See Struts 2 Spring plugin documentation at: http://struts.apache.org/2.x/docs/spring-plugin.html 15
  • 16. ContactsWebApp Very simple contacts manager Web application that uses Struts 2 and Spring ◦ Database is embedded Derby Uses Struts to manage ActionSupport classes (option 2) and Spring to: ◦ Manage dependencies ◦ Implement cross-cutting concerns such as logging, performance timing ◦ Interact with database repository (Spring JDBC) 16
  • 17. ContactsWebApp – FindContact and its dependencies ActionSupport FindContact Class Service ContactSearchService Layer ContactDataAccess Persistence Layer PhoneDataAccess EmailDataAccess PersonDataAccess DataSource 17
  • 18. ContactsWebApp – struts.xml and applicationContext.xml struts.xml ◦ Action nodes specify complete class value ◦ For example FindContact Note class FindContact needs a ContactSearchService object and has a public setContactSearchService method applicationContext.xml ◦ Spring configuration ◦ Create Spring beans for all non- ActionSupport classes that are used by the ActionSupport class 18
  • 19. Struts 2 – Spring Interaction applicationContext.xml Spring will provide any objects needed by the ActionSupport classes automatically ◦ Autowire “by name” ◦ For example FindContacts property contactSearchService will get a value because Spring will call the setContactSearchService method and pass it a contactSearchService object Spring is also managing dependencies for the other classes ◦ For example see contactSearchService, contactDataAccess and personDataAccess beans ◦ Be sure to specify scope=“request” 19
  • 20. Spring AOP Can use Spring AOP just as we did in the non-Struts 2 Contact application Same configuration in the Struts 2 project’s applicationContext.xml as we had in the Contact (non-web) project’s applicationContext_DB_Repository.xml See package edu.ku.si.contact.advice 20
  • 21. AOP Example - Exception Logging Need to specify where to apply the advice ◦ See ExceptionLogging class PointCut definition ◦ Don’t need to apply advice to the ActionSupport classes because they won’t be the originator of any Exceptions Rename c:/derby/contactsDB to c:/derby/tcontactsDB Run ContactsWebApp 21
  • 22. Spring JDBC Can use Spring JDBC as we did in the Contact (non-Struts 2) application See package edu.ku.si.contact.data The data access classes extend Spring JDBC’s SimpleJdbcDaoSupport ◦ See PersonDataAccess ◦ Much simpler to query database and process results ◦ Note Spring JDBC will manage opening and closing connections, handling exceptions, etc. 22
  • 23. Data Source Management The data access classes need a Data Source object ◦ See applicationContext.xml ◦ Note no scope is specified for the dataSource bean By extending SimpleJdbcDaoSupport the data access classes inherit the setDataSource method which Spring uses to inject the data source object To use connection pooling see notes in applicationContext.xml 23
  • 24. Why Use Spring With Struts 2? Significantly reduces coupling between classes Central management of dependencies Applications are easier to maintain and change Leverage Spring features that Struts 2 doesn’t provide ◦ Aspect-oriented programming ◦ Spring JDBC 24