Your SlideShare is downloading. ×
0
Struts2
Struts2
Struts2
Struts2
Struts2
Struts2
Struts2
Struts2
Struts2
Struts2
Struts2
Struts2
Struts2
Struts2
Struts2
Struts2
Struts2
Struts2
Struts2
Struts2
Struts2
Struts2
Struts2
Struts2
Struts2
Struts2
Struts2
Struts2
Struts2
Struts2
Struts2
Struts2
Struts2
Struts2
Struts2
Struts2
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Struts2

288

Published on

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
288
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
12
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Struts 2Juan Carlos Figueroa Duran Struts 2Understandingthe contextStruts historySee Struts 2 Juan Carlos Figueroa Duranfrom outsidethe forestSee Struts 2from insidethe forest Otoño -2010Resume:RequestLifecycle inStruts 2 Departamento de Tecnologias de la InformacionDeclaringwill we do y Ciencias de la Computación Universidad del Bío-BíoOne example
  • 2. Outline Struts 2Juan Carlos Figueroa 1 Understanding the context DuranUnderstanding 2 Struts historythe contextStruts history 3 See Struts 2 from outside the forestSee Struts 2from outsidethe forest 4 See Struts 2 from inside the forestSee Struts 2from insidethe forest 5 Resume: Request Lifecycle in Struts 2Resume:RequestLifecycle inStruts 2 6 Declaring will we doDeclaringwill we do 7 One exampleOne example
  • 3. What is a framework? Struts 2Juan Carlos Figueroa DuranUnderstanding A framework is a piece of structural software. We say structuralthe context because structure is perhaps a larger goal of the frameworkStruts history than any specic functional requirement. A framework tries toSee Struts 2from outside make generalizations about the common tasks and workow ofthe forest a specic domain.See Struts 2from insidethe forestResume:RequestLifecycle inStruts 2Declaringwill we doOne example
  • 4. What is a MVC? Struts 2Juan Carlos Figueroa DuranUnderstanding The MVC pattern provides a separation of concerns that appliesthe context well to web applications. Separation of concerns allows us toStruts history manage the complexity of large software systems by dividingSee Struts 2from outside them into high-level components.The MVC design patternthe forest identies three distinct concerns: model, view, and controller.See Struts 2from insidethe forestResume:RequestLifecycle inStruts 2Declaringwill we doOne example
  • 5. Struts history Struts 2Juan Carlos Struts 1.X Figueroa Duran Apache Struts is an open-source framework that is used for developing Java web application. Originally developed by theUnderstandingthe context programmer and author Craig R. McClanahan, this was laterStruts history taken over by the Apache Software Foundation in 2002.See Struts 2from outsidethe forest Struts 2.XSee Struts 2from inside But with the development of new and lightweight MVC basedthe forest frameworks like Spring, Stripes and Tapestry, it becomesResume:Request necessary to modify the Struts 1 framework. So, the team ofLifecycle inStruts 2 Apache Struts and another J2EE framework, WebWork ofDeclaring OpenSymphony joined hand together to develop an advancedwill we do framework with all possible developing features that will make itOne example developer and user friendly.
  • 6. Understanding MVC in Struts 2 Struts 2Juan Carlos Figueroa Duran The high-level design of Struts 2Understandingthe context follows the well-established Web BrowserStruts history Model-View-Controller desing ClientSee Struts 2 pattern. We see what MVC designfrom outside request               the forest pattern identies three distinct                      render pageSee Struts 2 concerns: model, view, and Controller FilterDispatcherfrom insidethe forest controller: In Struts 2, these are invoke action                            Resume: implemented by the action, result,RequestLifecycle in and FilterDispatcher, respectively. Model Action select result View ResultStruts 2Declaringwill we doOne example
  • 7. The controller-FilterDispatcher Struts 2Juan Carlos Figueroa Duran The controllers job is map requests to actions. In a webUnderstandingthe context application, the incoming HTTP Web BrowserStruts history requests can be thought of as ClientSee Struts 2 commands that the user issues tofrom outside request               the forest the application. One of the                      render pageSee Struts 2 fundamental tasks of a web Controller FilterDispatcherfrom insidethe forest application is routing these invoke action                            Resume: requests to the appropriate set ofRequestLifecycle in actions that should be taken Model Action select result View ResultStruts 2Declaring within the application itself.will we doOne example
  • 8. Model-Actions Struts 2Juan Carlos Figueroa Duran In more technical terms, the model is the internal state of theUnderstandingthe context application. This state is Web BrowserStruts history composed of both the data model ClientSee Struts 2 and the business logic. From thefrom outside request               the forest high-level black box view, the data                      render pageSee Struts 2 and the business logic merge Controller FilterDispatcherfrom insidethe forest together into the monolithic state invoke action                            Resume: of the application. This actionsRequestLifecycle in encapsulate the calls to business Model Action select result View ResultStruts 2Declaring logic into a single unit of work.will we doOne example
  • 9. View-Result Struts 2Juan Carlos Figueroa DuranUnderstandingthe context Web BrowserStruts history The view is the presentation the ClientSee Struts 2 page returned to the web browser.from outside request               the forest This page is the user interface                      render pageSee Struts 2 that presents a representation of Controller FilterDispatcherfrom insidethe forest the applications state to the user. invoke action                            Resume:Request Model ViewLifecycle in Action select result ResultStruts 2Declaringwill we doOne example
  • 10. Interceptor Struts 2 We can see that this interceptors Juan Carlos appears like a stack of this in front of Figueroa Duran the action. The invocation of the action must travel through this stack. invoke action finishedUnderstandingthe context The interceptor allow implementStruts history common and cross-cutting task, in aSee Struts 2 clean and reusable component that Interceptorsfrom outsidethe forest you can keep separate from yourSee Struts 2 action code. Logging, data validationfrom insidethe forest and le upload are good examples of Action OGNL                          invoke result ResultResume:Request task should be implemented in a OGNLLifecycle inStruts 2 interceptor. Most every actions will ActionContext (ThreadLocal)Declaring have a stack of interceptor associated ValueStack request session . . .will we do with it. These interceptors areOne example invoked both before and after the action.
  • 11. Interceptor ... continuation Struts 2Juan Carlos Figueroa Duran invoke action finishedUnderstandingthe contextStruts history The using of interceptors make your InterceptorsSee Struts 2from outside application more manageable. Whitthe forest this exibility we gain two primarySee Struts 2from inside benets, reuse and conguration. Action OGNL                          invoke result Resultthe forestResume: OGNLRequestLifecycle in ActionContext (ThreadLocal)Struts 2 ValueStack request session . . .Declaringwill we doOne example
  • 12. Action Struts 2 Struts 2 action serves three roles. Juan Carlos First, an action is an encapsulation of Figueroa Duran the calls to business logic into a single unit of work. Second, the invoke action finishedUnderstandingthe context action serves as a locus of dataStruts history transfer. Third, select the result thatSee Struts 2 should render the result page. Interceptorsfrom outsidethe forestSee Struts 2 Who class can be Actions?from insidethe forest All action what be or pretend be a invoke result Action OGNL                          ResultResume: POJO.Request OGNLLifecycle inStruts 2 Convention ActionContext (ThreadLocal) ValueStack request session . . .Declaringwill we do All action class, must use a namingOne example convention where our class names end with the word Action.
  • 13. Result Struts 2Juan Carlos Figueroa Duran invoke action finishedUnderstandingthe contextStruts historySee Struts 2 Is the component for rendering the Interceptorsfrom outsidethe forest view back to the user. We will seeSee Struts 2 this with more details in the lecturefrom insidethe forest of JSP. Action OGNL                          invoke result ResultResume: OGNLRequestLifecycle in ActionContext (ThreadLocal)Struts 2 ValueStack request session . . .Declaringwill we doOne example
  • 14. ValueStack Struts 2Juan Carlos Figueroa Duran Simpling, the ValueStack is a storage area that holds all the data invoke action finishedUnderstandingthe context associated with the processing of aStruts history request. So keep all data in a InterceptorsSee Struts 2 convenient central location. Thefrom outsidethe forest OGNL is a tool that allows us toSee Struts 2 access the data we put in thefrom inside invoke resultthe forest ValueStack. More especically this Action OGNL                          ResultResume:Request languaje allows reference and OGNLLifecycle inStruts 2 manipulate the data on the ActionContext (ThreadLocal) ValueStack request session . . .Declaring ValueStack.will we doOne example
  • 15. Request Lifecycle Struts 2Juan Carlos Figueroa DuranUnderstandingthe context myAction.actionStruts history Action InterceptorsSee Struts 2from outsidethe forestSee Struts 2from insidethe forest ResultResume:RequestLifecycle in HTMLStruts 2Declaringwill we doOne example
  • 16. Request Lifecycle Struts 2Juan Carlos Figueroa Duran User Sends request:Understandingthe context User sends a request to the server forStruts history some resource. myAction.actionSee Struts 2from outside Action Interceptorsthe forest FilterDispatcher determines theSee Struts 2from inside appropriate action: Resultthe forest The FilterDispatcher looks at the HTMLResume:Request request and then determines theLifecycle inStruts 2 appropriate Action.Declaringwill we doOne example
  • 17. Request Lifecycle Struts 2Juan Carlos Figueroa Interceptors are applied: Duran Interceptors congured for applyingUnderstandingthe context the common functionalities such asStruts history workow, validation, le upload etc.See Struts 2 are automatically applied to the myAction.actionfrom outside Action request. Interceptorsthe forestSee Struts 2from inside Resultthe forest Execution of Action:Resume: HTMLRequest Then the action method is executedLifecycle inStruts 2 to perform the database relatedDeclaring operations like storing or retrievingwill we do data from the database.One example
  • 18. Request Lifecycle Struts 2Juan Carlos Figueroa Duran Return of Request:Understanding Then the request returns through thethe context interceptors in the reverse order. TheStruts history returning request allows us to myAction.actionSee Struts 2from outside perform the clean-up or additional Action Interceptorsthe forest processing.See Struts 2from inside Resultthe forest Display the result to user:Resume: HTMLRequestLifecycle in Finally the control is returned to theStruts 2 servlet container, which sends theDeclaringwill we do output to the user browser.One example
  • 19. Request Lifecycle Struts 2Juan Carlos Figueroa Duran Output rendering:Understanding Then the Result renders the output.the contextStruts historySee Struts 2from outsidethe forest myAction.action ActionSee Struts 2 Interceptorsfrom insidethe forestResume: ResultRequestLifecycle in HTMLStruts 2Declaringwill we doOne example
  • 20. Declarative architecture Struts 2Juan Carlos Figueroa Duran The more important conguration that you can do in Struts 2. With this you can dening the Struts 2 components that yourUnderstandingthe context application will use and linking them together to form yourStruts history required workow paths. We means is it in this step we canSee Struts 2 dene what action is red with a particular URL, and whatfrom outsidethe forest results might be chosen by that action to complete processing.See Struts 2 We have two option to dene the architecture of our webfrom insidethe forest application: XML le or Java Annotation.Resume:RequestLifecycle inStruts 2Declaringwill we doOne example
  • 21. Java Annotation version Struts 2 A relatively new feature of the Java language, annotations allowJuan Carlos Figueroa you to add metadata directly to Java source. Struts 2 uses Java Duran annotations in this way. If you dont want to use XML les, theUnderstandingthe context declarative architecture mechanism can be congured to scanStruts history Java classes for Struts 2related annotations. We can see aSee Struts 2 example:from outsidethe forest @Results ({ @ R e s u l t ( name= i n p u t , v a l u e= / R e g i s t r a t i o n S u c c e s s . j s pSee Struts 2from inside )the forest @ R e s u l t ( v a l u e= / R e g i s t r a t i o n S u c c e s s . j s p )Resume: })Request p u b l i c c l a s s R e g i s t e r implements Action {Lifecycle in public String execute () {Struts 2 // B u s i n e s s logic for loginDeclaring }will we do }One example ...
  • 22. XML le version Struts 2Juan Carlos Struts 2 allows you to use XML les to describe your Figueroa Duran applications desired Struts 2 architectural components. TheUnderstanding ocial XML le where declare actions and results is thethe context struts.xml.Struts history a c t i o n name= L o g i n c l a s s = m a n n i n g . L o g i n See Struts 2 r e s u l t / A c c o u n t P a g e . j s p/ r e s u l t from outsidethe forest r e s u l t name= i n p u t / L o g i n . j s p/ r e s u l t / a c t i o nSee Struts 2 a c t i o n name= R e g i s t r a t i o n from insidethe forest r e s u l t / R e g i s t r a t i o n . j s p/ r e s u l t / a c t i o nResume:Request a c t i o n name= R e g i s t e r c l a s s = m a n n i n g . R e g i s t e r Lifecycle in r e s u l t / R e g i s t r a t i o n S u c c e s s . j s p/ r e s u l t Struts 2 r e s u l t name= i n p u t / R e g i s t r a t i o n . j s p/ r e s u l t Declaring / a c t i o nwill we do ...One example
  • 23. Understanding XML declarative architecture:package name Struts 2Juan Carlos Figueroa Duran Struts 2 packages are similar to Java packages. They providing a mechanism for grouping your actions based on commona lityUnderstandingthe context of function or domain. The name attribute is a merely logicalStruts history name by which we can reference the package.See Struts 2 1 s t r u t sfrom outside 2 p a c k a g e name= a c t i o n s n a m e s p a c e= / a c t i o n s e x t e n d s=the forest struts − d e f a u l t See Struts 2 3 a c t i o n name= M a k e H e l l o c l a s s = a c t i o n s . M a k e H e l l ofrom insidethe forest m e t h o d= e x e c u t e 4 r e s u l t name= s u c c e s s t y p e= d i s p a t c h e r / j s pResume: / S a y H e l l o . j s p/ r e s u l t RequestLifecycle in 5 / a c t i o nStruts 2 6 / p a c k a g eDeclaring 7 / s t r u t swill we doOne example
  • 24. Understanding XML declarative architecture:namespace Struts 2 As weve seen, the name space attribute is used to generate theJuan Carlos Figueroa URL namespace to which the action of these package are Duran mapped.Understandingthe context http://       +       localhost:8080       +      /HelloYou       +       /actions       +      /MakeHello.actionStruts historySee Struts 2 protocol host name: port number servlet context package namespace action name.actionfrom outsidethe forestSee Struts 2 1 s t r u t sfrom insidethe forest 2 p a c k a g e name= a c t i o n s n a m e s p a c e= / a c t i o n s e x t e n d s= struts − d e f a u l t Resume:Request 3 a c t i o n name= M a k e H e l l o c l a s s = a c t i o n s . M a k e H e l l oLifecycle in m e t h o d= e x e c u t e Struts 2 4 r e s u l t name= s u c c e s s t y p e= d i s p a t c h e r / j s pDeclaring / S a y H e l l o . j s p/ r e s u l t will we do 5 / a c t i o nOne example 6 / p a c k a g e 7 / s t r u t s
  • 25. Understanding XML declarative architecture:extends Struts 2Juan Carlos Figueroa Duran This important attribute names another package whoseUnderstanding components should be inherited by the current package. Likethe context struts-default package.Struts history 1 s t r u t sSee Struts 2 2 p a c k a g e name= a c t i o n s n a m e s p a c e= / a c t i o n s e x t e n d s=from outsidethe forest struts − d e f a u l t 3 a c t i o n name= M a k e H e l l o c l a s s = a c t i o n s . M a k e H e l l oSee Struts 2 m e t h o d= e x e c u t e from insidethe forest 4 r e s u l t name= s u c c e s s t y p e= d i s p a t c h e r / j s p / S a y H e l l o . j s p/ r e s u l t Resume:Request 5 / a c t i o nLifecycle in 6 / p a c k a g eStruts 2 7 / s t r u t sDeclaringwill we doOne example
  • 26. Understanding XML declarative architecture:action name Struts 2Juan Carlos Figueroa DuranUnderstanding 1 s t r u t sthe context 2 p a c k a g e name= a c t i o n s n a m e s p a c e= / a c t i o n s e x t e n d s=Struts history struts − d e f a u l t See Struts 2 3 a c t i o n name= M a k e H e l l o c l a s s = a c t i o n s . M a k e H e l l ofrom outside m e t h o d= e x e c u t e the forest 4 r e s u l t name= s u c c e s s t y p e= d i s p a t c h e r / j s pSee Struts 2 / S a y H e l l o . j s p/ r e s u l t from inside 5 / a c t i o nthe forest 6 / p a c k a g eResume: 7 / s t r u t sRequestLifecycle inStruts 2Declaringwill we doOne example
  • 27. Understanding XML declarative architecture:class Struts 2Juan Carlos Figueroa Duran In the class attribute specify a class to provide the actionUnderstanding implementation.the contextStruts history 1 s t r u t s 2 p a c k a g e name= a c t i o n s n a m e s p a c e= / a c t i o n s e x t e n d s=See Struts 2 struts − d e f a u l t from outsidethe forest 3 a c t i o n name= M a k e H e l l o c l a s s = a c t i o n s . M a k e H e l l o m e t h o d= e x e c u t e See Struts 2from inside 4 r e s u l t name= s u c c e s s t y p e= d i s p a t c h e r / j s pthe forest / S a y H e l l o . j s p/ r e s u l t Resume: 5 / a c t i o nRequest 6 / p a c k a g eLifecycle in 7 / s t r u t sStruts 2Declaringwill we doOne example
  • 28. Understanding XML declarative architecture:method Struts 2Juan Carlos Figueroa Duran Now for method attribute, we can mapped a method from action dened in class attribute that contains the business logic.Understandingthe context Here we can specify the method to be invoked for execute thereStruts history business logic.See Struts 2 1 s t r u t sfrom outside 2 p a c k a g e name= a c t i o n s n a m e s p a c e= / a c t i o n s e x t e n d s=the forest struts − d e f a u l t See Struts 2 3 a c t i o n name= M a k e H e l l o c l a s s = a c t i o n s . M a k e H e l l ofrom insidethe forest m e t h o d= e x e c u t e 4 r e s u l t name= s u c c e s s t y p e= d i s p a t c h e r / j s pResume: / S a y H e l l o . j s p/ r e s u l t RequestLifecycle in 5 / a c t i o nStruts 2 6 / p a c k a g eDeclaring 7 / s t r u t swill we doOne example
  • 29. Understanding XML declarative architecture:result name Struts 2Juan Carlos Figueroa Duran The name attribute is a like tag. And this tag should be used toUnderstanding map the result to render.the contextStruts history 1 s t r u t s 2 p a c k a g e name= a c t i o n s n a m e s p a c e= / a c t i o n s e x t e n d s=See Struts 2 struts − d e f a u l t from outsidethe forest 3 a c t i o n name= M a k e H e l l o c l a s s = a c t i o n s . M a k e H e l l o m e t h o d= e x e c u t e See Struts 2from inside 4 r e s u l t name= s u c c e s s t y p e= d i s p a t c h e r / j s pthe forest / S a y H e l l o . j s p/ r e s u l t Resume: 5 / a c t i o nRequest 6 / p a c k a g eLifecycle in 7 / s t r u t sStruts 2Declaringwill we doOne example
  • 30. Understanding XML declarative architecture:type Struts 2Juan Carlos Figueroa Duran Here we must specify to the browser the type of resource toUnderstanding redirect. types: dispatcher, redirect, redirect-action.the contextStruts history 1 s t r u t s 2 p a c k a g e name= a c t i o n s n a m e s p a c e= / a c t i o n s e x t e n d s=See Struts 2 struts − d e f a u l t from outsidethe forest 3 a c t i o n name= M a k e H e l l o c l a s s = a c t i o n s . M a k e H e l l o m e t h o d= e x e c u t e See Struts 2from inside 4 r e s u l t name= s u c c e s s t y p e= d i s p a t c h e r / j s pthe forest / S a y H e l l o . j s p/ r e s u l t Resume: 5 / a c t i o nRequest 6 / p a c k a g eLifecycle in 7 / s t r u t sStruts 2Declaringwill we doOne example
  • 31. The goal Struts 2Juan Carlos Figueroa DuranUnderstandingthe contextStruts historySee Struts 2from outsidethe forestSee Struts 2from insidethe forestResume:RequestLifecycle inStruts 2Declaringwill we doOne example
  • 32. The JSP code: JavaScript Struts 2Juan Carlos Figueroa Duran % User user = ActionContext . getContext () % Understanding form a c t i o n = M a k e H e l l o n a m e s p a c e=/ a c t i o n s method= p o s t the context Struts history t a b l e t rSee Struts 2from outside td a l i g n = r i g h t l a b e l U s e r name :/ l a b e l / tdthe forest td i n p u t type= t e x t name= u s e r N a m e See Struts 2 v a l u e= %=u s e r . g e t U s e r N a m e ( ) % // td from inside / t rthe forest t rResume: tdRequest % i f ( u s e r . getName ( )== ) { % Lifecycle in ...Struts 2Declaringwill we doOne example
  • 33. The JSP code: Tags Struts 2Juan Carlos Figueroa DuranUnderstandingthe context s : a c t i o n e r r o r /Struts history s : form a c t i o n = M a k e H e l l o n a m e s p a c e= / a c t i o n s method=See Struts 2 p o s t from outsidethe forest s : t e x t f i e l d l a b e l = U s e r Name name= u s e r N a m e / s : s u b m i t v a l u e= s a l u d a r ! a l i g n = c e n t e r / See Struts 2 / s : formfrom insidethe forestResume:RequestLifecycle inStruts 2Declaringwill we doOne example
  • 34. Struts conguration: struts.xml Struts 2Juan Carlos Figueroa DuranUnderstanding s t r u t sthe context p a c k a g e name= a c t i o n s n a m e s p a c e= / a c t i o n s e x t e n d s=Struts history struts − d e f a u l t See Struts 2 a c t i o n name= M a k e H e l l o c l a s s = a c t i o n s . M a k e H e l l ofrom outside m e t h o d= e x e c u t e the forest r e s u l t / j s p / S a y H e l l o . j s p/ r e s u l t See Struts 2 / a c t i o nfrom inside / p a c k a g ethe forest / s t r u t sResume:RequestLifecycle inStruts 2Declaringwill we doOne example
  • 35. Action: MakeHello Struts 2Juan Carlos Figueroa public class MakeHello extends ActionSupport { DuranUnderstanding private String userName ;the context p u b l i c S t r i n g e x e c u t e ( ) throws Exception {Struts history r e t u r n SUCCESS ;See Struts 2 }from outsidethe forest p u b l i c S t r i n g getUserName ( ) {See Struts 2from inside r e t u r n userName ;the forest }Resume:Request p u b l i c void setUserName ( S t r i n g userName ) {Lifecycle in t h i s . userName = userName ;Struts 2 }Declaring }will we doOne example
  • 36. JSP: SayHello Struts 2Juan Carlos Figueroa DuranUnderstanding htmlthe context headStruts history meta h t t p − e q u i v= C o n t e n t −Type content= t e x t / h t m l ; c h a r s e t =UTF−8See Struts 2from outside t i t l e H e l l o y o u/ t i t l e the forest / headSee Struts 2 bodyfrom inside h1 H e l l o s : p r o p e r t y v a l u e = u s e r N a m e / ! ! ! / h1the forest / bodyResume: / htmlRequestLifecycle inStruts 2Declaringwill we doOne example

×