Introduction to MVC and the Jakarta Struts Framework By Gyanendra Dwivedi Software Consultant.

Slides:



Advertisements
Similar presentations
Struts Portlet Copyright © Liferay, Inc. All Rights Reserved. No material may be reproduced electronically or in print without written permission.
Advertisements

Web Development with Karsten Schulz Terp-Nielsen Master Principal Sales Consultant Oracle Denmark.
Apache Struts Technology
 Copyright Wipro Technologies JSP Ver 1.0 Page 1 Talent Transformation Java Server Pages.
Using JavaServer Pages Harry R. Erwin, PhD CIT304/CSE301.
Web MVC-2: Apache Struts Rimon Mikhaiel
MVC Frameworks Alternatives to coding the MVC pattern.
MC365 Application Servers: Servlets. Today We Will Cover: What a servlet is The HTTPServlet and some of its more important methods How to configure the.
Struts1 Apache Struts Dr Jim Briggs. Struts2 What is Struts? Struts is an open source framework for building Java web applications Supports MVC/Model.
Object-Oriented Enterprise Application Development Tomcat 3.2 Configuration Last Updated: 03/30/2001.
J4www/jea Week 3 Version Slide edits: nas1 Format of lecture: Assignment context: CRUD - “update details” JSP models.
WEB1P servintro1 Introduction to servlets and JSP Dr Jim Briggs.
Struts Basics SSE USTC Qing Ding. Agenda What is and Why Struts? Struts architecture – Controller: Focus of this presentation – Model – View Struts tag.
DT211/3 Internet Application Development
Objectives:1. Investigate the Web application deployment descriptor 2. Install and deploy Tomcat and struts 3. Design and deploy a struts application Struts.
Tomcat Configuration A Very, Very, Very Brief Overview.
Apache Struts Technology A MVC Framework for Java Web Applications.
SE-2840 Dr. Mark L. Hornick1 Java Servlet-based web apps Servlet Architecture.
Introduction to Java web programming Dr Jim Briggs JWP intro1.
Java Enterprise Edition Java Web Development Structure of a web project Introduction to Web Applications The first project Introduction to Java Web Development.
Struts 2.0 an Overview ( )
Struts. Agenda Preface Struts and its components An example The architecture required for Struts Applications.
Michael Rimov Centerline Computers Craig McClanahan Sun Microsystems O’Reilly Open Source Convention July , 2003.
UNIT-V The MVC architecture and Struts Framework.
Introduction to the Jakarta Struts Framework  The Topics: JavaServer Pages (JSP) Overview JSP Tags and Tag Libraries Overview Model – View – Controller.
Java Frameworks Indy Java Users Group January 29, 2003.
Author: DoanNX Version 2.0/Time: 30’. The ways to solve it There are 2 main ways:  Client-side: Using JavaScript (now one very good option is JQuery).
JavaServer Faces Jeff Schmitt October 5, Introduction to JSF Presents a standard framework for building presentation tiers for web applications.
Design Patterns Phil Smith 28 th November Design Patterns There are many ways to produce content via Servlets and JSPs Understanding the good, the.
Introduction to Java Server Pages (JSPs) Robert Thornton.
Jakarta Struts Presented by Object Computing, Inc. (OCI) Written by Greg Elliott
JSP Architecture Outline  Model 1 Architecture  Model 2 Architecture.
8/15/2003System Specialists Corporation How to implement BC4J with the Struts Framework Eugene Sicat Technical Architect/Project Manager System Specialists.
Struts J2EE web application framework “ Model 2 ” Model View Controller Controller Servlet Key features XML metadata Struts taglib Simplified form validation.
Lecturer: Prof. Piero Fraternali, Teaching Assistant: Alessandro Bozzon, Advanced Web Technologies: Struts–
Stanisław Osiński, 2002JSP – A technology for serving dynamic web content Java Server Pages™ A technology for serving dynamic web content Stanisław Osiński,
Fall CIS 764 Database Systems Design L8. Web ….
Java Web Development with NetBeans IDE -- Kai Qian Chapter 5 JavaServer Faces (JSF) Technology.
Struts Framework Anna Paščenko. What is Struts?  An open source framework for building Java web applications.
J2EE Struts 1 3 Struts: Introduction 1 4 Once you’ve coded a lot of JSP applications, you find yourself doing a lot of repetitive things. Also,
Fall 2007cs4201 Advanced Java Programming Umar Kalim Dept. of Communication Systems Engineering
Apache Struts. J2EE Web Application using MVC design pattern Why MVC? Separate components = easier maintenance – Model component holds object data – View.
1 Introduction to Servlets. Topics Web Applications and the Java Server. HTTP protocol. Servlets 2.
JS (Java Servlets). Internet evolution [1] The internet Internet started of as a static content dispersal and delivery mechanism, where files residing.
© FPT SOFTWARE – TRAINING MATERIAL – Internal use 04e-BM/NS/HDCV/FSOFT v2/3 JSP Application Models.
Basic JSP Celsina Bignoli Problems with Servlets Servlets contain –request processing, –business logic –response generation all lumped.
CSI 3125, Preliminaries, page 1 SERVLET. CSI 3125, Preliminaries, page 2 SERVLET A servlet is a server-side software program, written in Java code, that.
COMP9321 Web Application Engineering Semester 2, 2015 Dr. Amin Beheshti Service Oriented Computing Group, CSE, UNSW Australia Week 3 1COMP9321, 15s2, Week.
Preface IIntroduction Objectives I-2 Course Overview I-3 1Oracle Application Development Framework Objectives 1-2 J2EE Platform 1-3 Benefits of the J2EE.
13 Copyright © 2004, Oracle. All rights reserved. Adding Validation and Error Handling.
11 Copyright © 2004, Oracle. All rights reserved. Customizing Actions.
Chapter 3 JSP Overview. The Problem with Servlets processing the request and generating the response are both handled by a single servlet class Java programming.
Struts Framework Day-2 Ashok Chakravarti. DataSource Usage Sample Struts-config.xml …
Implementation Struts Framework for well-architectured web applications Model-View-Controller design pattern.
Chapter 6 Chapter 6 Server Side Programming (JSP) Part 1 1 (IS 203) WebProgramming (IS 203) Web Programming.
APACHE STRUTS ASHISH SINGH TOMAR ast2124. OUTLINE Introduction The Model-View-Controller Design Pattern Struts’ implementation of the MVC Pattern Additional.
Apache Struts Technology A MVC Framework for Java Web Applications.
V Web Tier Architecture MVC and Struts. v First, there were servlets And things were good Faster than CGI programs More scalable with built-in.
Enterprise Java v050228MVC1 Model, View, Controller Web Architecture.
CS520 Web Programming Spring – Web MVC Chengyu Sun California State University, Los Angeles.
Struts 2 Development. Topics  Roles in Struts Development  Control Flow  Actions  Struts 2 Views and Target  Struts 2 Custom Tags  Validation 
An Introduction to and This document is for purely educational purposes.
Introduction to Jakarta Struts Framework Try NCCU Computer Center Mar 20,2002.
CS3220 Web and Internet Programming Introduction to Java Servlets
Java Servlets.
Unit 6-Chapter 2 Struts.
Introduction to Struts
The Model Layer What is Model?
CS3220 Web and Internet Programming Introduction to Java Servlets
Struts BY: Tejashri Udavant..
Presentation transcript:

Introduction to MVC and the Jakarta Struts Framework By Gyanendra Dwivedi Software Consultant

Feb 11, 2003Introduction to Jakarta Struts – Gyanendra Dwivedi 2 What is MVC The Model View Controller design pattern is a technique used to separate Business logic/state (the Model) from User Interface (the View) and program progression/flow (the Control). This pattern is very useful when it comes to modern web development: –The majority of modern, high usage websites are dynamically driven. –People well skilled at presentation (HTML writers) seldom know how to develop back-end solutions and visa versa. –Separating business rules from presentation is good no matter what environment you develop in be it web or desktop.

Feb 11, 2003Introduction to Jakarta Struts - Gyanendra Dwivedi 3 What is MVC (cont…) The View –The view is how data is represented to the user. For instance the view in a web application may be an HTML page, an image or other media The Model –The model represents the data in the system. For instance, a model might represent the properties associated with a user’s profile The Controller –The controller is the glue between the model and the view. It is responsible for controlling the flow of the program as well as processing updates from the model to the view and visa versa

Feb 11, 2003Introduction to Jakarta Struts - Gyanendra Dwivedi 4 What is MVC (cont…) User Controller Model View

Feb 11, 2003Introduction to Jakarta Struts - Gyanendra Dwivedi 5 What is MVC (cont…) User ControllerModel View

Feb 11, 2003Introduction to Jakarta Struts - Gyanendra Dwivedi 6 Benefits of MVC Promotes Modularity Multiple views Abstraction Allows application to be defined in a flow-chart, use-case or activity diagram which is more easily transferred to implementation.

Feb 11, 2003Introduction to Jakarta Struts - Gyanendra Dwivedi 7 Designing an MVC App. Let’s try designing an application from scratch The application will be a message board where users can view messages and post messages The application will require that a user has an account setup with the message board before posting a message, but an account is not required for viewing a message. The basic components we must implement for this message board are: –User profile –Logon/authentication –Message List –Message

Feb 11, 2003Introduction to Jakarta Struts - Gyanendra Dwivedi 8 Activity Diagrams I find the UML activity diagrams to be the most useful construct for describing program flow. Activity diagrams are much like flow charts, except a few new constructs are added such as describing concurrency as well as recursive elements. Each node of the Activity Diagram can be thought of as a call to a “Controller” asking it to take you to the next step in the application

Feb 11, 2003Introduction to Jakarta Struts - Gyanendra Dwivedi 9 Activity Diagram: Create Profile Create ProfileSave ProfileEdit Profile [invalid] [else] == Create Profile()

Feb 11, 2003Introduction to Jakarta Struts - Gyanendra Dwivedi 10 Activity Diagram: Logon == Logon() Login Form [else] Create Profile() [new user]

Feb 11, 2003Introduction to Jakarta Struts - Gyanendra Dwivedi 11 Activity Diagram: View Msg. Show Msg. BoardSelect Msg.Display Msg. == Select Msg()

Feb 11, 2003Introduction to Jakarta Struts - Gyanendra Dwivedi 12 Activity Diagram: Edit Msg. Logon() Edit Msg. Save Msg. [expired session] [else] Select Msg()Create New Msg. [else]

Feb 11, 2003Introduction to Jakarta Struts - Gyanendra Dwivedi 13 Jakarta Struts In order to take advantage of the MVC design pattern in your applications a considerable framework must be in place. Today we are going to learn about such a framework named Struts Struts is an open source MVC framework developed by the Apache Jakarta project group. Struts allows JSP/Servlet writers the ability to fashion their web applications using the MVC design pattern. By designing your web application using Struts you allow: –Architect the ability to design using the MVC pattern –Developer to exploit the Struts framework when building the app. –Web designer to be sheltered from learning how to program

Feb 11, 2003Introduction to Jakarta Struts - Gyanendra Dwivedi 14 Struts (cont…) Struts takes the grunt work out of developing an MVC based web app. The Struts framework provides a plethora of canned objects which can facilitate fundamental aspects of MVC, while allowing you to extend further as need suites Struts allows you to configure a lot of the default framework objects through xml configuration files. Our examples will use the Struts framework using the Apache Tomcat Servlet/JSP container. Tomcat provides a reference implementation of the Servlet specification 2.2 and JSP specification 1.1

Feb 11, 2003Introduction to Jakarta Struts - Gyanendra Dwivedi 15 Struts (cont…) The components of MVC in Struts: –The View is usually defined as a JSP or HTML page. –The Model is usually defined as a Java object (sometimes called a bean). –The Controller is defined by a Java object which extends the org.apache.struts.action.Action class. The Action class is at the heart of the Struts framework.

Feb 11, 2003Introduction to Jakarta Struts - Gyanendra Dwivedi 16 Struts: The Model To define a model in Struts is simple. Just create a java class and provide some functions. Your model classes should be coded independent of the Struts framework to promote maximum code reuse by other applications (i.e. if you have to reference javax.servlet.* class in your model, you are doing something wrong) Struts provides some default Model components, the most important of which is ActionForm. If you create a Model class by extending the Struts ActionForm class, Struts will –Ensure your form bean is created when needed –Ensure form submittal directly updates your form object with the inputted values –Your controller object will be passed the form bean

Feb 11, 2003Introduction to Jakarta Struts - Gyanendra Dwivedi 17 The Model (cont…) Struts will only handle models in an automatic fashion if you extend org.apache.struts.action.ActionForm Most likely you will have already built model object such as Customer, Employee, etc… Because you don’t want to tie your existing model objects to the Struts framework by having to extend ActionForm, Struts provides a org.apache.struts.util.PropertyUtils class that has a static method called copyProperties When your controller receives a form, it can simply call PropertyUtils.copyProperties(myModel, form) to copy all form properties to your original model object.

Feb 11, 2003Introduction to Jakarta Struts - Gyanendra Dwivedi 18 Model: ActionForm import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.struts.action.ActionForm; import org.apache.struts.action.ActionMapping; import org.apache.struts.upload.FormFile; public class LogonForm extends ActionForm { protected String userName; protected String password; public void setUserName(String userName) { this.userName = userName; } public void setPassword(String password) { this.password = password; } //There would also be getters for these properties }

Feb 11, 2003Introduction to Jakarta Struts - Gyanendra Dwivedi 19 Model: Action Form (cont…) When this “LogonForm” is associated with a controller, it will be passed to the controller whenever it’s service is requested by the user. JSP pages acting as the view for this LogonForm are automatically updated by Struts with the current values of the UserName and Password properties. If the user changes the properties via the JSP page, the LogonForm will automatically be updated by Struts But what if the user screws up and enters invalid data? ActionForms provide validation… Before an ActionForm object is passed to a controller for processing, a “validate” method can be implemented on the form which allows the form to belay processing until the user fixes invalid input as we will see on the next slide...

Feb 11, 2003Introduction to Jakarta Struts - Gyanendra Dwivedi 20 Model: ActionForm Validation public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { if (“”.equals(this.userName)) { ActionErrors aes = new ActionErrors(); aes.add(new ActionError(“error.username.invalid”)); return aes; } } Typically what will happen is Struts will see that errors are being returned and will forward the user to a jsp page that has been setup as the “failure” page. Usually, the errors result from bad input on a form, so the failure page will be set to the original form and any tags which are found are replaced with the contents of the ActionErrors returned from the validate method.

Feb 11, 2003Introduction to Jakarta Struts - Gyanendra Dwivedi 21 The Controller The controller is the switch board of MVC. It directs the user to the appropriate views by providing the view with the correct model The task of directing users to appropriate views is called “mapping” in struts. Luckily, the Struts framework provides a base object called org.apache.struts.action.ActionServlet. The ActionServlet class uses a configuration file called struts- config.xml to read mapping data called action mappings The ActionServlet class reads the incoming URI and tries to match the URI against a set of action mappings to see if it can find a controller class which would be willing to handle the request This process is described in a diagram on the following page

Feb 11, 2003Introduction to Jakarta Struts - Gyanendra Dwivedi 22 Controller (cont…) Server configured to pass *.do extensions to org.apache.struts.action.ActionServlet via a web.xml configuration file ActionServlet object inspects the URI and tries to match it against an ActionMapping located in the struts-config.xml file. Instance of appropriate Action class is found and it’s perform() method is called Action object handles the request and returns control to a view based where the user is within the flow of the application

Feb 11, 2003Introduction to Jakarta Struts - Gyanendra Dwivedi 23 Controller: Sample public class LogonAction extends Action { public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) { LogonForm myForm = (LogonForm) form; if (myForm.getUserName().equals(“john”) && myForm.getPassword().equals(“doe”)) { //return a forward to our success page return mapping.findForward(”success”); } else { ActionErrors errors = new ActionErrors(); errors.add("password", new ActionError("error.password.required")); this.saveErrors(errors); //Action implements this method //go back to the page that made the request return (new ActionForward(mapping.getInput())); }

Feb 11, 2003Introduction to Jakarta Struts - Gyanendra Dwivedi 24 Controller: Forwards You might be wondering what mapping.findForward(“success”) means? –The mapping object passed to the Controller’s perform() method is of type ActionMapping. –When you setup your struts-config.xml file you can define forward tags that are available via the ActionMapping.findForward() method. –In the previous example, our ActionMapping object would have been loaded with values from the section defined for the LogonAction controller in struts-config.xml.

Feb 11, 2003Introduction to Jakarta Struts - Gyanendra Dwivedi 25 Controller: ActionMapping <action path="/logon" type="org.apache.struts.example.LogonAction" name="logonForm" scope="request" input="/logon.jsp">

Feb 11, 2003Introduction to Jakarta Struts - Gyanendra Dwivedi 26 The View The view in Struts is represented by JSP pages JSP pages allows developers to write java code, and access server side java objects in a web page Although this is good, we will distract ourselves from the reason we are using Struts and MVC. –We want to let our web page authors create dynamic web pages without knowing how to program JSP solves this by introducing the concept of Tag Libraries –Taglibs allow web designers the convenience of using HTML like tags –It lets developers program the logic behind the tags –The attributes of the tags are used as basic parameters that the developers will interpret, which could change the output generated by the tag * Struts contains a series of taglibs designed to allow developers and web page authors the ability to communication one another to facilitate dynamic web content

Feb 11, 2003Introduction to Jakarta Struts - Gyanendra Dwivedi 27 The View: Example <html:password property="password" size="16" maxlength="16"/> <html:submit property="submit" value="Submit"/>

Feb 11, 2003Introduction to Jakarta Struts - Gyanendra Dwivedi 28 View: Internationalization Commonly referred to as i18n (I n) The ability to maintain a web app in several different languages Based on the language setup on the client side, the web app will auto-magic-ally This is achieve in Struts through Java Resource Bundles ResourceBundles are classes that support, among other things, String data. Instead of having to make a ResourceBundle class for each language supported by your web app, resource bundles can be described as text files If you had a set of strings in the French language you might make a file named MyStrings_fr.properties

Feb 11, 2003Introduction to Jakarta Struts - Gyanendra Dwivedi 29 View: i18n (cont…) When the resource bundle class loader tries to load MyStrings_fr, it will look for a MyStrings_fr.properties file and create the class based on the properties in the file Part of the Struts framework states that when setting up the ActionServlet in the web.xml file, if you add an initialization parameter named “application” this should contain the class name of your resource file: action org.apache.struts.action.ActionServlet application myPackage.MyResources

Feb 11, 2003Introduction to Jakarta Struts - Gyanendra Dwivedi 30 View: i18n (cont…) In the previous example, when the ActionServlet is loaded it will read it’s “application” parameter to figure out that it should load string resources from mypackage.MyResources –If it doesn’t find a class by that name, it tries to find a MyResources.properties file –It then figures out what language the client is using. If it is different from the default, it looks up the 2 character ISO language code and tries to look for a class or.properties file by appending the ISO code to the class: If the client is French it would look up: 1) mypackage.MyResources_fr.class If it can’t find this class it then looks for: 2) mypackage.MyResources_fr.properties

Feb 11, 2003Introduction to Jakarta Struts - Gyanendra Dwivedi 31 View i18n (cont…) MyResources.properties file: helloworld.title=Hello World! menu.file=File menu.file.open=Open menu.file.close=Close menu.file.exit=Exit menu.edit=Edit menu.edit.copy=Copy menu.edit.paste=Paste MyResources_fr.properties file: helloworld.title=Allô monde! menu.file=Dossier menu.file.open=Ouvrir menu.file.close=Fermer menu.file.exit=Sortie menu.edit=Rédiger menu.edit.copy=Copier menu.edit.paste=Pâte

Feb 11, 2003Introduction to Jakarta Struts - Gyanendra Dwivedi 32 View i18n (cont…) In order for web developers to get at the string resources we use a special Struts tag called e.g. –This tag will be replaced with text from the appropriate resource bundle associated with this web app (via the “application” init param for the ActionServlet in web.xml) for the key logon.title

Feb 11, 2003Introduction to Jakarta Struts - Gyanendra Dwivedi 33 Resources Struts homepage – Sun’s Servlet Specification – Sun’s JSP Specification – Blue Stone’s Struts Tutorial – mos/trailMaps/index.jsp My address for questions: