spring web 2

Upload: laurentiu-paraschiv

Post on 03-Jun-2018

219 views

Category:

Documents


0 download

TRANSCRIPT

  • 8/12/2019 Spring Web 2

    1/20

  • 8/12/2019 Spring Web 2

    2/20

    Spring Web Flow 2Web Development

    Master Spring's well-designed web frameworks todevelop powerful web applications

    Sven Lppken

    Markus Stuble

    BIRMINGHAM - MUMBAI

    This material is copyright and is licensed for the sole use by Richard Ostheimer on 6th June 2009

    2205 hilda ave., , missoula, , 59801

  • 8/12/2019 Spring Web 2

    3/20

  • 8/12/2019 Spring Web 2

    4/20

    Credits

    Authors

    Sven Lppken

    Markus Stuble

    Reviewers

    Luca Masini

    Xinyu Liu

    Senior Acqui sition Editor

    David Barnes

    Development Editor

    Shilpa Dube

    Technical Editors

    Dhiraj Bellani

    Reshma Sundaresan

    Copy Editor

    Sumathi Sridhar

    Indexer

    Rekha Nair

    Production EditorialManager

    Abhijeet Deobhakta

    Project Team L eader

    Lata Basantani

    Project Coordinator

    Leena Purkait

    Proofreader

    Laura Booth

    Production Coordinator

    Aparna Bhagat

    Cover Work

    Aparna Bhagat

    This material is copyright and is licensed for the sole use by Richard Ostheimer on 6th June 2009

    2205 hilda ave., , missoula, , 59801

  • 8/12/2019 Spring Web 2

    5/20

    About the Authors

    Sven Lppken holds a degree in Computer Science, which he passed withdistinction. He is currently employed as a Java Software Developer at CBC CologneBroadcasting Center GmbH, one of the leading broadcasting and productioncompanies in Germany and a part of Media Group RTL Germany. Sven startedprogramming in C and C++ at the age of sixteen and quickly fell in love with the

    Java programming language during his studies. When he got the chance to writehis diploma thesis about object-relational mapping technologies, he accepted atonce. Since then, he has integrated Hibernate and the JPA in many projects, alwaysin conjunction with the Spring Framework.

    I would like to dedicate my rst book to my ance Frauke. Thankyou for having always been supportive and understanding when Iwas spending my evenings and weekends writing this book . I wouldalso like to thank Markus for giving me the opportunity to writethis book, I'm very grateful to him. Some of my friends provided mewith invaluable feedback, ideas, and criticism: Dr. Thomas Richert,

    Alexandre Morozov, and Oliver Fehrentz. Thanks guys!

    Special thanks to my parents, who have supported and encouragedme my entire life. Thank you so much!

    This material is copyright and is licensed for the sole use by Richard Ostheimer on 6th June 2009

    2205 hilda ave., , missoula, , 59801

  • 8/12/2019 Spring Web 2

    6/20

    Markus Stuble is currently working as a CTO at namics (Deutschland) GmbH.He has a Master's degree in Computer Science. He started programming with Java in

    the year 1999. After that, he has earned much experience in building Java enterprisesystems, especially web applications. He has a deep knowledge of the Java platformand the tools and frameworks around Java.

    There are many people who supported the writing of this book. Butthere is especially one person whom I want to say thank you, mywife Maria Elena. She greatly supported the writing and gave me thepower and energy to nish this book.

    This material is copyright and is licensed for the sole use by Richard Ostheimer on 6th June 2009

    2205 hilda ave., , missoula, , 59801

  • 8/12/2019 Spring Web 2

    7/20

  • 8/12/2019 Spring Web 2

    8/20

    Table of ContentsPreface 1

    Chapter 1: Introduct ion 7Three cornerstones: Spring, Spring MVC, and Spring Web Flow 8Spring Framework 8Spring MVC 9Spring Web Flow 9

    What is Spring Web Flow 9The elements of Spring Web Flow: flow, view, and conversation 10

    Flow 10View 10Conversation 10

    The Spring Web Flow elements: an example 11The new major release: Spring Web Flow 2.0 12

    Spring Web Flow 13Spring Faces 13Spring JavaScript 13Spring Binding 13

    Introduction to a new version 13 Automatic model binding 14Support for a new expression language 14Flash scope is now a real flash scope 14Spring Faces 14Flow managed persistence 15External redirects 15

    Summary 15

    Chapter 2: Setup for Spring Web Flow 2 17Installation of Spring Web Flow 2 17

    Inside the distribution 18

    This material is copyright and is licensed for the sole use by Richard Ostheimer on 6th June 2009

    2205 hilda ave., , missoula, , 59801

  • 8/12/2019 Spring Web 2

    9/20

    Table of Contents

    [ ii ]

    The examples inside the distribution 20Building the examples from the source code 22Installing the examples on your local machine 23

    Support for developers 24Build systems 24

    Ant 24Maven 28

    IDE 30Eclipse and Spring IDE 30NetBeans 35

    A sample for a quick s tar t 38Overview over the example 38The basics 41Building the service and database layer 46The web.xml file 50

    Dependencies 51Summary 52

    Chapter 3: The Basics of Spring Web Flow 2 53Elements of a flow 54

    The entry point to the flow 55Section head 56Section data 56

    The metadata of a flow 57Section input 60Programming in a flow 61The scopes 66The flow instance variables 69

    Assign a value to a scope variable 70 Access the value of a scope 70Inputs 72

    The states 73The start-state 74The action-state and execution of business logic 74The view-state 78The decision-state 83The subflow-state 83The end-state 83

    The exit point 84Section footer 84

    global-transitions: global handling of events 84on-end: execution of actions at the end of the flow 85output: output of the flow 85exception-handler: exceptions between the execution of a flow 85bean-import: declaring beans for a flow 90

    Internals of building a flow 90

    This material is copyright and is licensed for the sole use by Richard Ostheimer on 6th June 2009

    2205 hilda ave., , missoula, , 59801

  • 8/12/2019 Spring Web 2

    10/20

    Table of Contents

    [ iii ]

    Configuration 93FlowRegistry 94

    FlowExecutor 95FlowExecutor Listeners 96Internals of the Webflow Configuration 97Inheritance inside a flow definition 98

    Inheritance for flows 99Inheritance for states 100Merge or no merge 100

    The complete flow for the example 101Summary 102

    Chapter 4: Spring Faces 103Enabling Spring Faces support 104

    Inside the Facelets technology 105

    The ResourceServlet 106Internals of the ResourceServlet 108Configuration of the application context 114

    Using Spring Faces 119Overview of all tags of the Spring Faces tag library 119

    A complete example 121Creating the input page 122Handling of errors 126Reflecting the actions of the buttons into the flow definition file 127Showing the results 127

    Integration with other JavaServer Faces component libraries 129Integration with JBoss RichFaces 129Integration with Apache MyFaces Trinidad 131

    Summary 135Chapter 5: Mastering Spring Web Flow 137

    Subflows 137Spring JavaScript 140

    What is AJAX? 141Installing Spring JavaScript 144

    The first example with Spring JavaScript 145 Apache Tiles integration 153

    Tiles and AJAX 159The Web Flow conf iguration 165

    flow 166

    attribute 166secured 166persistence-context 167var 167

    This material is copyright and is licensed for the sole use by Richard Ostheimer on 6th June 2009

    2205 hilda ave., , missoula, , 59801

  • 8/12/2019 Spring Web 2

    11/20

    Table of Contents

    [ iv ]

    input 168output 168

    actionTypes 169evaluate 170render 170set 170

    on-start 171on-end 171transition 172global-transitions 173exception-handler 173bean-import 174action-state 175view-state 176decision-state 179subflow-state 180end-state 182

    Summary 183Chapter 6: Testing Spring Web Flow Applications 185

    How to test a Spring Web Flow application 185The first example 185

    A look into the source code 186First steps in testing 187Testing Persistent Contexts 190

    A short introduction to EasyMock 191Testing subflows 194More testing with EasyMock 196

    Summary 203Chapter 7: Secur ity 205

    Introducing Spring Security 206Installing Spring Security 206Basic authentication with Spring Security 207

    Setting up your web.xml 208 Advanced Spring Security configuration 209UserDetails 213Using database access to retrieve users 214Securing parts of a web page 216

    Securing method invocations 218Using Spring Security with Spring Web Flow 220Changing the user's password 220

    Summary 224

    This material is copyright and is licensed for the sole use by Richard Ostheimer on 6th June 2009

    2205 hilda ave., , missoula, , 59801

  • 8/12/2019 Spring Web 2

    12/20

    Table of Contents

    [ v ]

    Appendix A: f low.trac:The Model for the Examples 225flow.trac 225

    Item 226User 226Role 227Project 227Issue 228Type 232Priority 232Comment 232

    Attachment 233Summary 233

    Appendix B: Running on the SpringSource dm Server 235Introduction to the SpringSource dm Server 236Installation of the SpringSource dm Server 237Migrating an application 244Summary 246

    Index 247

    This material is copyright and is licensed for the sole use by Richard Ostheimer on 6th June 2009

    2205 hilda ave., , missoula, , 59801

  • 8/12/2019 Spring Web 2

    13/20

    This material is copyright and is licensed for the sole use by Richard Ostheimer on 6th June 2009

    2205 hilda ave., , missoula, , 59801

  • 8/12/2019 Spring Web 2

    14/20

    PrefaceSpring Web Flow is an open-source web development framework and part ofthe Spring product portfolio. Its primary purpose is to de ne the (work) ow of

    a web application. The ow is independent of the implementation and thus theinfrastructure of your application. This enables developers accustomed withSpring Web Flow to write powerful and re-usable web applications that are easyto maintain and enhance. Along with the Spring Web Flow distribution, additionallibraries are shipped. These libraries make it easier for developers to improve theirapplications with compelling AJAX functionality. It also includes Spring Faces,which combines Spring Web Flow with the powerful JavaServer Faces technologyto create feature-rich graphical user interfaces. You will nd explanations about allthis and much more in this book.

    What this book coversChapter 1: Introduction gives an introduction to the world of Spring Web Flow.Additionally, the chapter covers important de nitions that you need to knowto understand the following chapters.

    Chapter 2: Setup for Spring Web Flow 2 shows how to install Spring Web Flow andcreate the rst small application. It also shows the usage of the examples that areprovided in the Spring Web Flow distribution.

    Chapter 3: The Basics of Spring Web Flow 2covers all the basics that are essential tobuild applications with Spring Web Flow. It also explains all the essential thingsabout the ow de nition le.

    Chapter 4: Spring Faces gives an overview and also a detailed explanation on theusage of Spring Faces with Spring Web Flow. For better understanding, it alsoexplains the essential basics around JavaServer Faces.

    This material is copyright and is licensed for the sole use by Richard Ostheimer on 6th June 2009

    2205 hilda ave., , missoula, , 59801

  • 8/12/2019 Spring Web 2

    15/20

    Preface

    [ 2 ]

    Chapter 5: Mastering Spring Web Flow covers advanced topics, for example, the usageof sub ows and the new Spring JavaScript library that ships with Spring Web Flow

    for the rst time. The chapter also covers an in-depth look into the ow de nition le.Chapter 6: Testing Spring Web Flow Applications covers the important topic of testingapplications that are developed with Spring Web Flow. It shows the integratedsupport of JUnit ( ht t p: / / www. j uni t . or g ) and includes step-by-step instructionsshowing how to test your applications.

    Chapter 7: Security shows how to secure applications that are developed with SpringWeb Flow using Spring Security.

    Appendix A: ow.tracThe Model for the Examples describes the classes in the sampleproject, f l ow. t r ac . These classes are used in the examples of this book.

    Appendix B: Running on the SpringSource dm Server explains how to run a Spring WebFlow application on the SpringSource Application Platform (AP).

    What you need for this bookFor the examples in this book, we have used the following software packages:

    Java Development Kit (JDK) 6Spring Web Flow 2.0.xEclipse 3.4.x and NetBeans 6.1Apache Tomcat 6.0.18Apache Ant 1.7.1 / Apache Ivy 2.0.0 RC1Apache Maven 2.0.9Microsoft SQL Server 2008 Express EditionMicrosoft SQL JDBC Database Driver 1.2Hibernate Core 3.3.1 GA, Hibernate Annotations 3.4.0 GA, and HibernateEntityManager 3.4.0 GASpringSource dm Server 1.0.x

    Who this book is for This book is targeted at Java web application developers who work with Spring

    Web Flow. This book is a must-read for those who wish to bridge the gap betweenthe popular web framework and the popular application framework, and also forthose who want to create powerful and re-usable web applications. It requires priorknowledge of Spring.

    This material is copyright and is licensed for the sole use by Richard Ostheimer on 6th June 2009

    2205 hilda ave., , missoula, , 59801

  • 8/12/2019 Spring Web 2

    16/20

    Preface

    [ 3 ]

    ConventionsIn this book, you will nd a number of styles of text that distinguish betweendifferent kinds of information. Here are some examples of these styles, and anexplanation of their meaning.

    Code words in text are shown as follows: "Next, we de ne a custom view resolverwith a special vi ewCl ass property."

    A block of code will be set as follows:

    or g. spr i ngf r amewor k. webf l ow or g. spr i ngf r amewor k. j s 2. 0. 5. RELEASE

    When we wish to draw your attention to a particular part of a code block, therelevant lines or items will be shown in bold:

    Any command-line input or output is written as follows:

    mvn cl ean compi l e war: expl oded

    New terms and important words are shown in bold. Words that you see on thescreen, in menus, or dialog boxes for example, appear in our text like this: "If youclick on the Next button, a request to the server will be sent which renders a new

    web site using Tiles."

    Warnings or important notes appear in a box like this.

    Tips and tricks appear like this.

    This material is copyright and is licensed for the sole use by Richard Ostheimer on 6th June 2009

    2205 hilda ave., , missoula, , 59801

  • 8/12/2019 Spring Web 2

    17/20

    Preface

    [ 4 ]

    Reader feedbackFeedback from our readers is always welcome. Let us know what you think aboutthis bookwhat you liked or may have disliked. Reader feedback is important forus to develop titles that you really get the most out of.

    To send us general feedback, simply drop an email to f eedback@packt pub. com , andmention the book title in the subject of your message.

    If there is a book that you need and would like to see us publish, please sendus a note in the SUGGEST A TITLE form on www. packt pub. com or emailsuggest @packt pub. com .

    If there is a topic that you have expertise in and you are interested in either writingor contributing to a book, see our author guide on www. packt pub. com/ aut hor s .

    Customer supportNow that you are the proud owner of a Packt book, we have a number of things tohelp you to get the most from your purchase.

    Downloading the example code for the bookVisit ht t p: / / www. packt pub. com/ f i l es/ code/ 5425_Code. zi p to directlydownload the example code.

    The downloadable les contain instructions on how to use them.

    ErrataAlthough we have taken every care to ensure the accuracy of our contents, mistakesdo happen. If you nd a mistake in one of our booksmaybe a mistake in text orcodewe would be grateful if you would report this to us. By doing so, you can saveother readers from frustration, and help us to improve subsequent versions of thisbook. If you nd any errata, please report them by visiting ht t p: / / www. packt pub.com/ suppor t , selecting your book, clicking on the let us know link, and enteringthe details of your errata. Once your errata are veri ed, your submission will beaccepted and the errata added to any list of existing errata. Any existing errata canbe viewed by selecting your title from ht t p: / / www. packt pub. com/ suppor t .

    This material is copyright and is licensed for the sole use by Richard Ostheimer on 6th June 2009

    2205 hilda ave., , missoula, , 59801

  • 8/12/2019 Spring Web 2

    18/20

    Preface

    [ 5 ]

    PiracyPiracy of copyright material on the Internet is an ongoing problem across all media.At Packt, we take the protection of our copyright and licenses very seriously. Ifyou come across any illegal copies of our works in any form on the Internet, pleaseprovide us with the location address or web site name immediately so that we canpursue a remedy.

    Please contact us at copyr i ght @packt pub. com with a link to the suspectedpirated material.

    We appreciate your help in protecting our authors, and our ability to bring youvaluable content.

    QuestionsYou can contact us at quest i ons@packt pub. com if you are having a problem withany aspect of the book, and we will do our best to address it.

    This material is copyright and is licensed for the sole use by Richard Ostheimer on 6th June 2009

    2205 hilda ave., , missoula, , 59801

  • 8/12/2019 Spring Web 2

    19/20

    This material is copyright and is licensed for the sole use by Richard Ostheimer on 6th June 2009

    2205 hilda ave., , missoula, , 59801

  • 8/12/2019 Spring Web 2

    20/20

    IntroductionNearly every day you read about a new release of a framework for developing webbased applications. The Spring Framework is no exception. What makes Spring

    Web Flow (SWF ) (second version, unless explicitly mentioned otherwise) special isthat this framework not only solves one part of the tasks that have to be done in thedevelopment of a web application, but also helps you organize the ow (the orderin which pages are called) inside your web application. Additionally, it manages thestorage of data. However, to build a complete web application, you need more thanSpring Web Flow. Therefore, we will also explain how to integrate Spring Web Flowwith other frameworks.

    This book is neither a reference documentation, nor does it replace thereference documentation of the Spring Web Flow Framework. If you areinterested in the reference documentation, we strongly recommend thereference from SpringSource. It is available online at the Uniform Resource

    Locator (URL) ht t p: / / st at i c. spr i ngf r amewor k. org/ spr i ng-webf l ow/ docs/ 2. 0. x/ r ef er ence/ ht ml / i ndex. ht ml . If you needmore information about the Spring Framework, visit the web page of theframework at the URL ht t p: / / www. spr i ngf r amewor k. or g .

    If you need more help with your daily development of frameworks fromthe Spring Portfolio , please visit ht t p: / / www. spr i ngsour ce. or g/ .The site provides more information about all of the different frameworks.If the available reference documentation is insuf cient for your needs,you can search the forums that are offered on that page. The start pagefor the forums is ht t p: / / f or um. spr i ngsour ce. or g/ .