Java Struts

25
Ian F. Darwin http://www.darwinsys.com/ Struts: A Scalable MVC Framework for Web Applications 1

description

strut

Transcript of Java Struts

Ian F. Darwinhttp://www.darwinsys.com/Struts: A Scalable MVC Framework for Web Applications 1Ian Darwin, http://www.darwinsys.com/Computer Consultant, Mentor, Developer, TrainerOReilly Author: Lint, Java Cookbook, TomcatWorked with UNIX since 1980, Java 1995wrote file(1) command used in BSD, LinuxOpenBSD and Tomcat committerWorked at U of T (Computer Services, Undergrad teaching) and in industry (develop software; mentoring; firewalls; Java web sites; teach UNIX, C & Java for Learning Tree International)Web sites: JabaDot, LHBooks, ToastmastersIan Darwin2Ian Darwin, http://www.darwinsys.com/Overview of MVC and how Struts providesStruts Architecture and OptionsStruts and ScalabilityOutline3Ian Darwin, http://www.darwinsys.com/Before MVC, some web applications were spaghetti code (a 1970s term)The Web version: dynamic pages mixing up presentation, business logic (server-side and JavaScript?), and data representation in one fileHard to read, harder to debug, and almost impossible to maintain.In the beginning...4Ian Darwin, http://www.darwinsys.com/Model-View Controller design pattern, from Xerox PARCDivides interactive application into:Model (data modelling real world)View (display, presentation)Controller - responds to user actionsWhat is MVC?MVC Benefits:maintainability(lack of code inter-dependence)ModelController View5Ian Darwin, http://www.darwinsys.com/Familiar example: slide show program: change text in one View, it updates in all ViewsMVC originally for desktop clients; how do we extend this pattern to the Web?What is MVC? (contd)6Ian Darwin, http://www.darwinsys.com/Servlets are Java components running in a web server, which process web requestsvalidate input, deal with database, generate HTML resultAlternative to CGI scripting, ASP, PHP, etc.faster, strongly type-safe, etc.Part of the J2EE specificationMany implementations, both free and commercialServlets and JSP7Ian Darwin, http://www.darwinsys.com/Java 2 Enterprise Edition: a set of APIs for building large, scaleable distributed applicationsFrom Sun, developed under JCPSuns take since 1980: Agree on specs; compete on implementation (NFS, RPC, etc).J2EE Adopted by everybody (except Microsoft)IBM Websphere (which eBay runs on), BEA WebLogics, Apple WebObjects, Pramati, JBoss, OpenEJB, many moreJ2EE8Ian Darwin, http://www.darwinsys.com/J2EE APIsServlets, JSP Web AppsEJB (Enterprise Java Beans)Distributed DB/Multi-TierCORBA, RMIDistributed programming binary protocolsWeb ServicesDistributed Programming over HTTP (.NET interop)Mail, Activation EmailAlso Transactions, flexible logging, JMS, etc, etc.See http://java.sun.com/j2ee/9Ian Darwin, http://www.darwinsys.com/JavaServer Page (JSP) written as HTML + Java, compiles to a Servlet (easier to write)Comprehensive language and API

JSP Tag LibrariesError handling, etc., etc.JSP10Ian Darwin, http://www.darwinsys.com/ Possible division of labour: Servlet is Controller, deals with Model; JSP is View, displays result (Servlet Dispatcher pattern)Model (data) components are application-specific, not provided by APICustomer names, addressesImage dataServlet and JSP11JabaDot news portal site developed by me in 2000, based on Servlets and JSPsCode published in Java Cookbook, 2001Why did it fail?Tight coupling: business logic and presentation tightly mingledInadequate use of JSP tagsNot enough use of MVC!!R.I.P. JabaDot12Ian Darwin, http://www.darwinsys.com/If theServlet is a Controller and JSP the View (and data objects for the Model): instant MVC?Yes, BUT!Doesnt enforce or structure the division between Controller and ViewNeed a framework for that...Is Servlet + JSP == MVC?13Ian Darwin, http://www.darwinsys.com/A framework is a reusable, semi complete application that can be specialized to produce custom applications (Johnson, 1998)In object-oriented systems, a set of classes that embodies an abstract design for solutions to a number of related problems. (FOLDOC, 1995)Differentiated from a toolkit, which is a set of classes designed for use by an otherwise-complete applicationApplication uses toolkit; framework uses componentsSwing is a Toolkit; Struts is a FrameworkDefinitions14Ian Darwin, http://www.darwinsys.com/Struts is one of three dozen Java-based frameworks that purports to simplify development of MVC web applicationsFostered by Apache JakartaAuthored by Craig McClanahan (second architect of Tomcat web server)Heavily tested: McClanahan wrote entire Tomcat Admin interface while building Struts15Ian Darwin, http://www.darwinsys.com/Struts makes it easier to develop large web appsThe dominant (by far) framework - developers available, advice available, books, etc.Open source, freely usableAll components subclassable or replaceable!Includes powerful JSP Tag librariesSupported by third-party packagesSupported by tools vendors e.g., IBM WebSphere StudioAdvance Developer (WSAD) and Enterprise Developer (WSED) include Struts wizards (EasyStruts for Eclipse, JBuilder), ...Struts Advantages16Ian Darwin, http://www.darwinsys.com/Struts provides a single Controller ServletSite Developer provides:form bean (Java or XML) to hold data for each HTML formsmall Action class for each processing stepApplication Resources (Java properties) config file mapping keys to printable names, allowing for InternationalizationMaster config fileStruts ArchitectureEnglish Frenchstore.name=LH Bookseld.custName=Namestore.name=Livres LHeld.custName=Nom17Ian Darwin, http://www.darwinsys.com/A Struts ApplicationBrowser ClientHTTP RequestActionServletstruts-config.xmlInsertCustomerActionCustomerFormBeanSQL-basedDBMScustomerform.jspCustomerDAOIncluded both in signup.jsp and in myproperties.jspcreate &populatepassbeanpass dataacknowledgecust.jsppassbeanconsult for def'n ofCustomerFormBeanHTTP ResponseUser has filled inHTML form signup.jspJDBCTextJ2EE Web Server(Tomcat?)18Ian Darwin, http://www.darwinsys.com/Struts Validator provides common validationsfield present, valid email, etc.Tiles provides building composite pagespromoting re-use of HTML/JSP fragmentsStruts Options19Ian Darwin, http://www.darwinsys.com/A small research project I undertook atStaffordshire University aimed to determine whether Struts provides enough benefits indevelopment timemaintainability, scaleabilitycode re-useto repay the costs: the time to learn, the discipline to use well, the increase in artifacts, etc.Should be available for publication in 2004May submit to I.J. Web Eng. & Tech.My Research Project20Ian Darwin, http://www.darwinsys.com/All server-side implementations on the Internet must consider Performance!Struts uses the Servlet API, which runs multi-threadedJava Thread support - intrinsicServlet - runs threaded for PerformanceAction classes are instantiated only onceStruts and Scaleability 21Ian Darwin, http://www.darwinsys.com/Actions: consolidate related actions into one Action class (e.g., Add to Cart, Display Cart)HTML forms: always use client-side validationbasically just add tagAvoids spurious round trip on e.g., obvious missing fieldsImplementation Choices22Ian Darwin, http://www.darwinsys.com/See http://jakarta.apache.org/struts/See my web site, http://www.darwinsys.com/Especially /java/javaResources.html for links on Java, J2EE, Struts, ...Also /javawebframeworks/ for the other 38 frameworks, if you really need to know! :-)See http://www.onjava.com/pub/a/onjava/2002/10/30/jakarta.html - Lessons From The TrenchesReferences23Ian Darwin, http://www.darwinsys.com/J2EE provides comprehensive spec for enterprise APIsMany implementations of this specServlet and JSP are J2EE technologies for web appsMVC provides sensible division of labour in interactive applicationStruts extends Servlets and JSP to a full MVC framework.Summary24Ian Darwin, http://www.darwinsys.com/Question&AnswerStruts: A Scalable MVC Framework for Web Applications?25