Download presentation
Presentation is loading. Please wait.
Published byValerie Joleen O’Brien’ Modified over 8 years ago
1
An Introduction to and This document is for purely educational purposes.
2
● What is Struts? – Struts is a Java web-application framework that solves the problem of dynamic web content. – Struts uses the MVC (Model-View-Control) design pattern. – A better alternative to Web Applications than CGI/PHP. Model 2 architecture. “Struts: The Complete Reference. Second Edition.” By James Holmes. Retrieved from http://library.books24x7.com.lp.hscl.ufl.edu. October 8 th 2008. http://library.books24x7.com.lp.hscl.ufl.edu
3
The View ● The data presentation layer of the Struts framework. ● Utilizes JSP and HTML (as defined by the Struts framework.) ● Can use other web technologies including: XML, CSS, JavaScript, AJAX. ● There are a variety of JSP Tag Libraries that are used by GatorMail to create the View. – The Bean Library: Conditional and Dynamic Message content (accessing Java Bean properties.) – The HTML Library: Displaying HTML entities. – The Tiles Library: Template Library (similar to CSS.)
4
A look at the View <%@page contentType="text/html" import="org.apache.struts.action.ActionErrors"%> "/> "> ● This is the LoginForm.jsp file from webapp\tiles\LoginForm.jsp ● The file is composed of standard HTML tags as well as the Struts related Tag Libraries. ● and tags are examples of Struts' HTML and Bean libraries. ● The first couple lines deal with defining the document type and the Tag Libraries that will be used in this file. ● The action property of the tag defines what action will be called when the form is submitted! ● But what is missing from this file that we see everyday on our beloved GatorMail login page?
5
The View Continued ● This file contains no information on how the page is supposed to look! ● The loginForm.jsp file defined only the form used for logging in. ● The loginForm.jsp file is loaded by a Tiles container that creates the page look. ● The Tiles container can be found in webapp\login.jsp. ● The Tiles layout configuration can be found in loginLayout.jsp.
6
Why is nothing in the View hard- coded? ● If you hadn't noticed, there aren't any hard- coded messages or images in the View. ● Everything in the View has a template structure. ● Why? – Using message beans allows for dynamic web content. – Specifically, you can change the site language just by creating a properties file for that language. – The current messages.properties file is only in English, but there are many foreign students at UF, so why not take advantage of this feature? (Budget ? Laziness? Ignorance? わからない。)
7
Programming For the View ● If your requirement involves changing the user interface, you will be forced to program for the View. ● Notes: – Copy and paste is your friend. There is plenty of View code already written for you to play with. – Keep to the template structure. If you hard-code something to test the interface, make sure you change it to a template structure before committing it to the SVN server. – If you don't know what you're doing, Google it (or ask one of the people who do, but Google is probably more trust-worthy.)
8
The Control ● The Control is implemented in Java. ● The Control is the middle-man between the View and Model layers. ● The Control Java classes must extend the org.apache.struts.action.Action class. – The execute() method must be overridden. – The execute method performs the action as requested by the View. – The execute method performs complexed validation and calls the business logic. – The execute method returns an ActionForward object (the next page that should be displayed.)
9
The Control Continued ● Control classes are servlets contained in the web application's servlet container. The servlet container re-uses control class servlet instances for multiple View requests (i.e. don't use local variables!) ● The Control classes can be found in the src\java\edu\ufl\osg\webmail\actions directory of the GatorMail project. ● The Control Layer makes calls to Model level classes, but should never access data sources directly. Although the Control Layer could in principle access a database or mail server, this would violate the Model-View-Control paradigm! ● The Control Layer controls the navigation of the View through the use of an ActionForward object. The ActionForward object is returned at the end of the execute() process and contains the URL for the next page to be displayed. It is typical to create a forward mapping in struts-config.xml (“login”, “login.do”) and use ActionMapping.findForward(...) to recall the URL associated with it.
10
The Control Layer Continued ● What happens if the Control Layer encounters an error? – The ActionErrors object contains any errors that occur during the execution of an action. – Each error added has a key + error message. – The key (ActionErrors.GLOBAL_ERROR) will correspond to message beans in the View with the value ActionErrors.GLOBAL_ERROR. – login.authentication.failed refers to a text resource error message contained in src\messages.properties. – login.authentication.failed=Invalid username and/or password. errors.add(ActionErrors.GLOBAL_ERROR, new ActionError("login.authentication.failed", afe.getMessage()));
11
Communication Between the View and Control ● The View form actions are mapped to a Control actions via the struts-config.xml file (\src\webapp\WEB-INF) ● Data is passed between layers automatically using FormBean classes (JavaBean classes.) – FormBeans are data container objects. – For proper use by Struts, every local variable contained in a FormBean must have a getter and setter method with an exact name. – Since FormBeans are JavaBeans, if a FormBean had a “private String[] XXX” variable, this FormBean must also have getXXX() and setXXX() methods with the appropriate types!
12
FormBeans ● FormBeans are data container objects that are automatically populated by the ActionServlet using the getXXX() and setXXX() methods. ● When getting the data from a form, the ActionServlet invokes the setXXX() method of the FormBean. ● When sending data from the Control to the View, the ActionServlet invokes the getXXX() method of the FormBean. ● The FormBean property names must correspond to the field of the Form the FormBean is assigned to. ● For example, if a form has a “Username” entry field, the FormBean that is assigned to that form MUST have a property called Username with methods getUsername() and setUsername(). ● The FormBeans for GatorMail can be found in: src\java\edu\ufl\osg\webmail\forms
13
FormBeans Continued ● Form beans perform one important action other than serving as a Data Storage Class: Simple Validation. ● What is Simple Validation? – Simple validation is any type of input validation that requires no business level logic. – You should not access any model resources (this is Complexed Validation) – Simple validation should populate the ActionsError class with any simple validation errors and returns it. The form automatically re-displays the calling page (with error messages!)
14
FormBeans Continued Servlet Container Contro l Form Beans. “Struts: The Complete Reference. Second Edition.” By James Holmes. Retrieved from http://library.books24x7.com.lp.hscl.ufl.edu. October 8 th 2008. http://library.books24x7.com.lp.hscl.ufl.edu The Servlet instance of the FormBean may already be in the Servlet container from a previous request. This is done by the ActionServlet
15
What is the ActionServlet? ● The heart of the Struts framework. ● Part of the Control layer. ● Performs the actions as defined by the struts-config.xml file. ● Populates Form beans, calls ActionForm.validate() and calls Action.execute() ● There is only one ActionServlet instance in a Struts based web application. ● The action servlet handles all request and response actions.
16
Figure 5-1: The Controller Layer Lifecycle “Struts: The Complete Reference. Second Edition.” By James Holmes. Retrieved from http://library.books24x7.com.lp.hscl.ufl.edu. October 8 th 2008.http://library.books24x7.com.lp.hscl.ufl.edu The Action Servlet
17
The Model ● The Model Layer of the Struts paradigm is not specifically defined. ● Struts leaves the Model layer open to be invoked using any technologies desired: – LDAP, IMAP, SQL, Oracle etc. ● The Model Layer performs business logic and performs reads and writes from any data sources utilized by a project. ● In particular, GatorMail utilizes SQL, IMAP (using javax.mail package) and LDAP (Lightweight Data Access Protocol) technologies. ● Examples of Model level classes can be found in the src\java\edu\ufl\osg\webmail\data directory.
18
struts-config.xml ● Important Struts configuration information are contained in this file. ● Defines the relationship between View and Controller. ● What is defined in the struts-config.xml? – Action Mappings – FormBean Mappings – Global Exceptions – Global Forwards – Plug-ins ● The location of web application resources. ● Strut's configuration data.
19
Linking View and Control with the struts-config.xml ● In the struts-config.xml file, there are two important sections in linking the view and control layers together: – (lines 20 to 36) ● Links the loginForm to the FormBean LoginForm. – (lines 125 to 342) <!-- --> Note: Forwards are mappings for the next page URL. You can retrieve the ActionForward object associated with a forward mapping using the ActionMapping.findForward(“login”) method. Action classes return the ActionForward class to control View navigation.
20
web.xml ● Another important configuration file for Struts. ● Defines the ActionServlet behavior. ● Sets the default extension for actions. – By default, the “.do” extension is used for actions. This can be changed to anything you want. – Many of the GatorMail actions do not include “.do” extension. – i.e. GatorMail just uses “login” as the action name rather than “login.do.”
21
Overview Images from: “Struts: The Complete Reference. Second Edition.” By James Holmes. Retrieved from http://library.books24x7.com.lp.hscl.ufl.edu. October 8 th 2008. http://library.books24x7.com.lp.hscl.ufl.edu
22
Approaching Struts Programming ● A good strategy for modifying a Struts based web-application is to go from bottom up. – Plan out the modifications you will be adding. What other data sources will you need to interact with? What should the user have to do to use your modifications? – Create/Modify any Model level classes that you will need for your requirement. – Create/Modify an Action class to interact with your model modifications. – Create/Modify the Data Storage Object (FormBean) that you will be using. – Create a JSP page with the proper form information (remember to keep to templates!) – Update the struts-config.xml if you need to change any ActionForward- ing or update what actions are called by your form.
23
Where do I go now? ● If you want to learn more about Struts and Struts programming there are plenty of resources on the web. – Start by checking out: http://struts.apache.org/ – For UF Students, I suggest using the George Smather's Library E-Book collection. ● “Struts: The complete reference.” By James Holmes covers everything in-depth and includes example code for you to play with.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.