Introduction to Struts 1.3

12,223 views

Published on

An Introduction to the Struts 1.3 Framework

Published in: Technology, Education
4 Comments
14 Likes
Statistics
Notes
No Downloads
Views
Total views
12,223
On SlideShare
0
From Embeds
0
Number of Embeds
5
Actions
Shares
0
Downloads
0
Comments
4
Likes
14
Embeds 0
No embeds

No notes for slide

Introduction to Struts 1.3

  1. 1. Introduction to Jakarta Struts 1.3 Ilio Catallo – info@iliocatallo.it
  2. 2. Outline ¤ Model-View-Controller vs. Web applications ¤ From MVC to MVC2 ¤ What is Struts? ¤ Struts Architecture ¤ Building web applications with Struts ¤ Setting up the Controller ¤ Writing Views ¤ References 2
  3. 3. Model-View-Controller vs. Web applications 3
  4. 4. Model-View-Controller design pattern ¤ In the late seventies, software architects saw applications as having three major parts: ¤ The part that manages the data (Model) ¤ The part that creates screens and reports (View) ¤ The part that handles interactions between the user and the other subsystems (Controller) ¤ MVC turned out to be a good way to design applications ¤ Cocoa (Apple) ¤ Swing (Java) ¤ .NET (Microsoft) 4
  5. 5. Model-View-Controller design pattern 5 View Controller Model State query Change notification Event Method invocation
  6. 6. Model-View-Controller vs. Web applications ¤ What is the reason not to use the same MVC pattern also for web applications? ¤ Java developers already have utilities for: ¤ building presentation pages, e.g., JavaServer Pages (View) ¤ handling databases, e.g., JDBC and EJB (Model) ¤ But… ¤ the HTTP protocol imposes limitations on the applicability of the MVC design pattern ¤ we don’t have any component to act as the Controller 6
  7. 7. HTTP limitations ¤ The MVC design pattern requires a push protocol for the views to be notified by the model ¤ HTTP is a pull protocol: no request implies no response ¤ The MVC design pattern requires a stateful protocol to keep track of the state of the application ¤ HTTP is stateless 7
  8. 8. HTTP limitations: Struts solutions ¤ HTTP is stateless: we can implement the MVC design pattern on top of the Java Servlet Platform ¤ the platform provides a session context to help track users in the application ¤ HTTP is a pull protocol: we can increase the Controller responsibility. It will be responsible for: ¤ state changes ¤ state queries ¤ change notifications 8
  9. 9. Model-View-Controller 2 design pattern ¤ The resulting design pattern is sometimes called MVC2 or Web MVC ¤ Any state query or change notification must pass through the Controller ¤ The View renders data passed by the Controller rather than data returned directly from the Model 9 View Controller Model
  10. 10. What is Jakarta Struts? ¤ Jakarta Struts is an open source framework ¤ It provides a MVC2-style Controller that helps turn raw materials like web pages and databases into a coherent application ¤ The framework is based on a set of enabling technologies common to every Java web application: ¤ Java Servlets for implementing the Controller ¤ JavaServer Pages for implementing the View ¤ EJB or JDBC for implementing the Model 10
  11. 11. Struts Architecture 11
  12. 12. Struts Main Components: ActionForward, ActionForm, Action ¤ Each web application is made of three main components: ¤ Hyperlinks lead to pages that display data and other elements, such as text and images ¤ HTML forms are used to submit data to the application ¤ Server-side actions which performs some kind of business logic on the data 12
  13. 13. Struts Main Components: ActionForward, ActionForm, Action ¤ Struts provides components that programmers can use to define hyperlinks, forms and custom actions: ¤ Hyperlinks are represented as ActionForward objects ¤ Forms are represented as ActionForm objects ¤ Custom actions are represented as Action objects 13
  14. 14. Struts Main Components: ActionMapping ¤ Struts bundles these details together into an ActionMapping object ¤ Each ActionMappinghas its own URI ¤ When a specific resource is requested by URI, the Controller retrieves the corresponding ActionMapping object ¤ The mapping tells the Controller which Action, ActionForm and ActionForwards to use 14
  15. 15. Struts Main Components: ActionServlet ¤ The backbone component of the Struts framework is ActionServlet (i.e., the Struts Controller) ¤ For every request, the ActionServlet: ¤ uses the URI to understand which ActionMapping to use ¤ bundles all the user input into an ActionForm ¤ call the Action in charge of handling the request ¤ reads the ActionForwardcoming from the Action and forward the request to the JSP page what will render the result 15
  16. 16. Struts Control Flow 16 Controller (ActionServlet) Action (ActionMapping, ActionForm) JSP page (with HTML form) JSP page (result page) HTTP request HTTP response ① ② ③ ④ Model (e.g., EJB) ActionForward ⑤ Controller Model
  17. 17. Struts main component responsibilities Class Description ActionForward A user’s gesture or view selection ActionForm The data for a state change ActionMapping The state change event ActionServlet The part of the Controller that receives user gestures and stare changes and issues view selections Action classes The part of the Controller that interacts with the model to execute a state change or query and advises ActionServlet of the next view to select 17
  18. 18. Building Web applications with Struts Setting up the Controller 18
  19. 19. Setting up the Controller: The big picture 19 struts- config.xml web.xml ActionMapping 1 ActionMapping N
  20. 20. Setting up the Controller: Servlet Container (1/3) ¤ The web.xml deployment descriptor file describes how to deploy a web application in a servlet container (e.g., Tomcat) ¤ The container reads the deployment descriptor web.xml, which specifies: ¤ which servlets to load ¤ which requests are sent to which servlet 20
  21. 21. Setting up the Controller: Servlet Container (2/3) ¤ Struts implements the Controller as a servlet ¤ Like all servlets it lives in the servlet container ¤ Conventionally, the container is configured to sent to ActionServlet any request that matches the pattern *.do ¤ Remember: Any valid extension or prefix can be used, .do is simply a popular choice 21
  22. 22. Setting up the Controller: Servlet Container (3/3) web.xml (snippet) <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> 22 ¤ Forward any request that matches the pattern *.do to the servlet named action (i.e., the Struts controller)
  23. 23. Struts Controller: struts-config.xml ¤ The framework uses the struts-config.xml file as a deployment descriptor ¤ It contains all the ActionMappings definedfor the web application ¤ At boot time, Struts reads it to create a database of objects ¤ At runtime, Struts refers to the object created with the configuration file, not the file itself 23
  24. 24. Struts Controller: ActionForm (1/4) ¤ A JavaBean is a reusable software component which conform to a set of design patterns ¤ The access to the bean’s internal state is provided through two kinds of methods: accessors and mutators ¤ JavaBeans are used to encapsulate many objects into a single object ¤ They can be passed around as a single bean object instead of as multiple individual objects 24
  25. 25. Struts Controller: ActionForm (2/4) ¤ Struts model ActionForms as JavaBeans ¤ The ActionForm has a corresponding property for each field on the HTML form ¤ The Controller matches the parameters in the HTTP request with the properties of the ActionForm ¤ When they correspond, the Controller calls the setter methods and passes the value from the HTTP request 25
  26. 26. Struts Controller: ActionForm (3/4) LoginForm.java pubic class LoginForm extends org.apache.struts.action.ActionForm { private String username; private String password; public String getUsername() {return this.username;} public String getPassword() {return this.password;} public void setUsername(String username) {this.username = username;} public void setPassword(String password) {this.password = password;} } 26 ¤ An ActionForm is a JavaBean that extends org.apache.struts.action.ActionForm
  27. 27. Struts Controller: ActionForm (4/4) Specifying a new ActionForm in struts-config.xml <form-beans> <form-bean name=”loginForm" type=”app.LoginForm"/> </form-beans> 27 ¤ Define a mapping between the actual ActionForm and its logical name
  28. 28. Struts Controller: ActionForwards Specifying new ActionForwardsin struts-config.xml <forward name="success" path="/success.html"/> <forward name=”failure" path="/success.html"/> <forward name=”logon" path=”/Logon.do"/> 28 ¤ Define a mapping between the resource link and its logical name ¤ Once defined, throughout the web application it is possible to reference the resource via its logical name
  29. 29. Struts Controller: Action ¤ Actions are Java classes that extend org.apache.struts.Action ¤ The Controller populates the ActionForm and then passes it to the Action ¤ the entry method is perform (Struts 1.0) or execute (Struts 1.1+) ¤ The Action is generally responsible for: ¤ validating input ¤ accessing business information ¤ determining which ActionForward to return to the Controller 29
  30. 30. Struts Controller: Action LoginAction.java import javax.servet.http.*; public class LoginAction extends org.apache.struts.action.Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest req, HttpServletResponse res) { // Extract data from the form LoginForm lf = (LoginForm) form; String username = lf.getUsername(); String password = lf.getPassword(); // Apply business logic UserDirectory ud = UserDirectory.getInstance(); if (ud.isValidPassword(username, password)) return mapping.findForward("success"); return mapping.findForward("failure"); } } 30
  31. 31. Struts Controller: struts-config.xml struts-config.xml (snippet) <form-beans> <form-bean name="loginForm" type="app.LoginForm"/> </form-beans> <action-mappings> <action path="/login" type="app.LoginAction" name="loginForm"> <forward name="success" path="/success.jsp"/> <forward name="failure" path="/failure.jsp"/> </action> </action-mappings> 31 Struts trims automatically the .do extension
  32. 32. Building web applications with Struts Writing Views 32
  33. 33. Writing Views: JavaServer Pages (JSP) ¤ JavaServer Pages is a technology that helps Java developers create dynamically generated web pages ¤ A JSP page is a mix of plain old HTML tags and JSP scripting elements ¤ JSP pages are translated into servlets at runtime by the JSP container 33 JSP Scripting Element <b> This page was accessed at <%= new Date() %></b>
  34. 34. Writing Views: JSP tags ¤ JSP scripting elements require that developers mix Java code with HTML. This situation leads to: ¤ non-maintainable applications ¤ no opportunity for code reuse ¤ An alternative to scripting elements is to use JSP tags ¤ JSP tags can be used as if they were ordinary HTML tags ¤ Each JSP tag is associated with a Java class ¤ It’s sufficient to insert the same tag on another page to reuse the same code ¤ If the code changes, all the pages are automatically updated 34
  35. 35. Writing Views: JSP Tag Libraries ¤ A number of prebuilt tag libraries are available for developers ¤ Example: JSP Standard Tag Library (JSTL) ¤ Each JSP tag library is associated with a Tag Library Descriptor (TLD) ¤ The TLD file is an XML-style document that defines a tag library and its individual tags ¤ For each tag, it defines the tag name, its attributes, and the name of the class that handles tag semantics 35
  36. 36. Writing Views: JSP Tag Libraries ¤ JSP pages are an integral part of the Struts Framework ¤ Struts provides its own set of custom tag libraries 36 Tag library descriptor Purpose struts-html.tld JSP tag extension for HTML forms struts-bean.tld JSP tag extension for handling JavaBeans struts-logic.tld JSP tag extension for testing the values of properties
  37. 37. Writing Views login.jsp <%@ taglib uri=”http://struts.apache.org/tags-html" prefix="html" %> <html> <head> <title>Sign in, Please!</title> </head> <body> <html:form action="/login" focus="username"> Username: <html:text property="username"/> <br/> Password: <html:password property="password"/><br/> <html:submit/> <html:reset/> </html:form> </body> </html> 37 the taglib directive makes accessible the tag library to the JSP page JSP tag from the struts-html tag library
  38. 38. References ¤ Struts 1 In Action, T. N. Husted, C. Dumoulin, G. Franciscus, D. Winterfeldt, , Manning Publications Co. ¤ JavaBeans, In Wikipedia, The Free Encyclopedia,http://en.wikipedia.org/w/index.php?title= JavaBeans&oldid=530069922 ¤ JavaServer Pages, In Wikipedia, The Free Encyclopediahttp://en.wikipedia.org/w/index.php?title= JavaServer_Pages&oldid=528080552 38

×