0
STRUTS Part of the Jakarta Project Sponsored by the Apache Software Foundation Developed by: Roger W Barnes of Project Ref...
STRUTS  Objectives <ul><li>Course Overview </li></ul><ul><li>Unit 1 - Model-View-Controller Design Pattern </li></ul><ul><...
STRUTS  Objectives <ul><li>Unit 6 - STRUTS Configuration File </li></ul><ul><li>Unit 7 - Web Application Descriptor File <...
Model-View-Controller Design Pattern Unit 1
STRUTS  MVC Design Pattern <ul><li>Central controller mediates application flow </li></ul><ul><li>Controller delegates to ...
STRUTS  MVC Design Pattern
STRUTS  MVC Design Pattern <ul><li>3 Major Components in STRUTS </li></ul><ul><ul><li>Servlet controller (Controller) </li...
STRUTS  MVC Design Pattern <ul><li>Configuration file contains action mappings (determines navigation) </li></ul><ul><li>C...
Model Components Unit 2
STRUTS  Model Components <ul><li>Model divided into concepts </li></ul><ul><ul><li>Internal state of the system </li></ul>...
STRUTS  Model Components <ul><li>JavaBeans and Scope </li></ul><ul><ul><li>Page  – visible within a single JSP page, for t...
STRUTS  Model Components <ul><li>ActionForm Beans </li></ul><ul><ul><li>Extends the ActionForm class </li></ul></ul><ul><u...
STRUTS  Model Components <ul><li>When coding ActionForm beans consider: </li></ul><ul><ul><li>The ActionForm class itself ...
STRUTS  Model Components <ul><li>Continued </li></ul><ul><ul><li>Define a property (with associated  getXxx()  and  setXxx...
STRUTS  Model Components <ul><li>System State Beans   </li></ul><ul><ul><li>Actual state of a system is normally represent...
STRUTS  Model Components <ul><li>Business Logic Beans   </li></ul><ul><ul><li>Should encapsulate the functional logic of y...
STRUTS  Model Components <ul><li>Business Logic Beans - Continued </li></ul><ul><ul><li>For larger applications, these bea...
STRUTS  Model Components <ul><li>Accessing Relational Databases   </li></ul><ul><ul><li>Struts can define the datasources ...
View Components Unit 3
STRUTS  View Components <ul><li>Internationalized Messages   </li></ul><ul><ul><li>Struts builds upon Java platform to pro...
STRUTS  View Components <ul><li>ApplicationResources.properties </li></ul><ul><ul><li>Contains the messages in the default...
STRUTS  View Components <ul><li>Forms and FormBean interactions </li></ul><ul><ul><li>HTML Forms and their limitations </l...
STRUTS  View Components <ul><li><%@ page language=&quot;java&quot; %>  </li></ul><ul><li><%@ taglib uri=&quot;/WEB-INF/str...
STRUTS  View Components <ul><li>Building Forms with Struts </li></ul><ul><ul><li>The  taglib  directive tells the JSP page...
STRUTS  View Components <ul><li>Building Forms with Struts – continued </li></ul><ul><ul><li>The form tag renders an HTML ...
STRUTS  View Components <ul><li>Input field types supported </li></ul><ul><ul><li>checkboxes </li></ul></ul><ul><ul><li>hi...
STRUTS  View Components <ul><li>Useful Presentation Tags </li></ul><ul><ul><li>[logic]  iterate  repeats its tag body once...
STRUTS  View Components <ul><li>Useful Presentation Tags – continued </li></ul><ul><ul><li>[html]  link  generates a HTML ...
STRUTS  View Components <ul><li>Automatic Form Validation </li></ul><ul><ul><li>Struts offers an additional facility to va...
STRUTS  View Components <ul><li>Page Composition with Includes </li></ul><ul><ul><li>The development of the various segmen...
STRUTS  View Components <ul><li>Page Composition with Includes – continued </li></ul><ul><ul><li>There are three types of ...
Controller Components Unit 4
STRUTS  Controller Components <ul><li>Struts includes a Servlet that implements the primary function of mapping a request ...
STRUTS  Controller Components <ul><li>Your primary responsibilities are:  </li></ul><ul><ul><li>Write an  Action  class (t...
STRUTS  Controller Components <ul><li>Action Classes: </li></ul><ul><ul><li>The  Action  class defines a perform method th...
STRUTS  Controller Components <ul><li>The goal of an  Action  class is to process this request, and then to return an  Act...
STRUTS  Controller Components <ul><li>A typical  Action  class will implement the following logic in its  perform()  metho...
STRUTS  Controller Components <ul><li>Design issues to remember when coding  Action  classes include the following </li></...
STRUTS  Controller Components <ul><li>Design issues to remember when coding  Action  classes include the following – conti...
STRUTS  Controller Components <ul><li>The ActionMapping Implementation </li></ul><ul><ul><ul><li>type  - Fully qualified J...
STRUTS  Controller Components <ul><li>The Actions Mapping Configuration File </li></ul><ul><ul><li>The developer's respons...
STRUTS  Controller Components <ul><ul><ul><li><form-beans> This section contains your form bean definitions. You use a <fo...
STRUTS  Controller Components <ul><ul><li><action-mappings> This section contains your action definitions. You use an <act...
STRUTS  Controller Components <ul><ul><li>One more section of good use is the  <data-sources>  section, which specifies da...
STRUTS  Controller Components <ul><li>The Web Application Deployment Descriptor  </li></ul><ul><ul><li>The final step in s...
Tag Libraries Unit 5
STRUTS  Tag Libraries <ul><li>HTML Tags </li></ul><ul><li>Bean Tags </li></ul><ul><li>Logic Tags </li></ul><ul><li>Templat...
HTML Tags <ul><li>The tags in the Struts HTML library form a bridge between a JSP view and the other components of a Web a...
HTML Tags <ul><li>HTML &quot;form&quot; tags </li></ul><ul><ul><li>button  </li></ul></ul><ul><ul><li>cancel  </li></ul></...
HTML Tags  – Typical HTML Form <HTML> <BODY> <FORM> <TABLE WIDTH=&quot;100%&quot;> <TR><TD>First Name</TD> <TD><INPUT TYPE...
HTML Tags  – Typical Struts Form <HTML:HTML> <BODY> <HTML:FORM Action=&quot;/CustomerForm&quot; focus=“name” > <TABLE WIDT...
Bean Tags <ul><li>The &quot;struts-bean&quot; tag library provides substantial enhancements to the basic capability provid...
Bean Tags   Tag Name Description cookie Define a scripting variable based on the value(s) of the specified request cookie....
Bean Tag Example <table border=&quot;2&quot;> <tr> <th align=&quot;left&quot;><bean:message key=“imagebroker.lob”/></th> <...
Logic Tags <ul><li>The Logic tag library contains tags that are useful in managing conditional generation of output text, ...
Logic Tags <ul><li>For tags that do value comparisons ( equal ,  greaterEqual ,  greaterThan ,  lessEqual ,  lessThan ,  n...
Logic Tags <ul><li>For tags that do substring matching ( match ,  notMatch ), the following rules apply: </li></ul><ul><ul...
Logic Tags   Tag Name Description empty Evaluate the nested body content of this tag if the requested variable is either n...
Logic Tags - Example <html:html> <head> <title><bean:message key=&quot;imagebrokerlink.title&quot;/></title> <META name=&q...
Template Tags <ul><li>The Template tag library contains three tags: put, get, and insert. Put tags put content into reques...
Template Tags <ul><li>Insert  Inserts (includes, actually) a template. Templates are JSP pages that include parameterized ...
Custom Tags  <%@ taglib uri=&quot;WEB-INF/imagebroker.tld&quot; prefix=&quot;broker&quot; %> <table width=750 cellspacing=...
Custom Tags – tld File <tag> <name>doctype</name> <tagclass>com.pri.brokertag.ImageBrokerDoctype</tagclass> <attribute> <n...
Custom Tags – Tag Class public class ImageBrokerDoctype extends TagSupport { private String value = null; public int doSta...
STRUTS Configuration File Unit 6
STRUTS Configuration File <ul><li>The developer's responsibility is to create an XML file named  struts-config.xml , and p...
STRUTS Configuration File <ul><li>Inside of the <struts-config> element, there are two important elements that are used to...
Struts-config.xml <?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot; ?> <!DOCTYPE struts-config PUBLIC &quot;-/...
Web Application Descriptor File Unit 7
Web.xml File <ul><li>The final step in setting up the application is to configure the application deployment descriptor (s...
Web.xml File <?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?> <!DOCTYPE web-app PUBLIC &quot;-//Sun Microsy...
Web.xml File - continued <!-- Action Servlet Mapping --> <servlet-mapping> <servlet-name>action</servlet-name> <url-patter...
Web.xml File - continued <!-- Struts Tag Library Descriptors --> <taglib> <taglib-uri>WEB-INF/struts-bean.tld</taglib-uri>...
Application Resources File Unit 8
Application.properties File error.cryptvalue.required=<li>You must enter some text.</li> error.lob.required=<li>You must e...
Resources Unit 9
Resources <ul><li>Main Struts Web Site </li></ul><ul><ul><li>http://jakarta.apache.org/struts/index.html </li></ul></ul><u...
Upcoming SlideShare
Loading in...5
×

Struts Intro Course(1)

1,206

Published on

Published in: Technology
0 Comments
1 Like
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total Views
1,206
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
41
Comments
0
Likes
1
Embeds 0
No embeds

No notes for slide
  • Transcript of "Struts Intro Course(1)"

    1. 1. STRUTS Part of the Jakarta Project Sponsored by the Apache Software Foundation Developed by: Roger W Barnes of Project Refinery, Inc. Introduction to Struts
    2. 2. STRUTS Objectives <ul><li>Course Overview </li></ul><ul><li>Unit 1 - Model-View-Controller Design Pattern </li></ul><ul><li>Unit 2 - Model Components </li></ul><ul><li>Unit 3 - View Components </li></ul><ul><li>Unit 4 - Controller Components </li></ul><ul><li>Unit 5 - Tag Libraries </li></ul>
    3. 3. STRUTS Objectives <ul><li>Unit 6 - STRUTS Configuration File </li></ul><ul><li>Unit 7 - Web Application Descriptor File </li></ul><ul><li>Unit 8 - Application Resources File </li></ul><ul><li>Unit 9 – Resources </li></ul>
    4. 4. Model-View-Controller Design Pattern Unit 1
    5. 5. STRUTS MVC Design Pattern <ul><li>Central controller mediates application flow </li></ul><ul><li>Controller delegates to appropriate handler </li></ul><ul><li>Handlers are tied to model components </li></ul><ul><li>Model encapsulates business logic </li></ul><ul><li>Control forwarded back through the Controller to the appropriate View </li></ul>
    6. 6. STRUTS MVC Design Pattern
    7. 7. STRUTS MVC Design Pattern <ul><li>3 Major Components in STRUTS </li></ul><ul><ul><li>Servlet controller (Controller) </li></ul></ul><ul><ul><li>Java Server Pages (View) </li></ul></ul><ul><ul><li>Application Business Logic (Model) </li></ul></ul><ul><li>Controller bundles and routes HTTP request to other objects in framework </li></ul><ul><li>Controller parses configuration file </li></ul>
    8. 8. STRUTS MVC Design Pattern <ul><li>Configuration file contains action mappings (determines navigation) </li></ul><ul><li>Controller uses mappings to turn HTTP requests into application actions </li></ul><ul><li>Mapping must specify </li></ul><ul><ul><li>A request path </li></ul></ul><ul><ul><li>Object type to act upon the request </li></ul></ul>
    9. 9. Model Components Unit 2
    10. 10. STRUTS Model Components <ul><li>Model divided into concepts </li></ul><ul><ul><li>Internal state of the system </li></ul></ul><ul><ul><li>Actions that can change that state </li></ul></ul><ul><li>Internal state of system represented by </li></ul><ul><ul><li>JavaBeans </li></ul></ul><ul><ul><li>Enterprise JavaBeans </li></ul></ul>
    11. 11. STRUTS Model Components <ul><li>JavaBeans and Scope </li></ul><ul><ul><li>Page – visible within a single JSP page, for the lifetime of the current request </li></ul></ul><ul><ul><li>Request – visible within a single JSP page, as well as to any page or servlet that is included in this page, or forwarded to by this page </li></ul></ul><ul><ul><li>Session – visible to all JSP pages and servlets that participate in a particular user session, across one or more requests </li></ul></ul><ul><ul><li>Application - visible to all JSP pages and servlets that are part of a web application </li></ul></ul>
    12. 12. STRUTS Model Components <ul><li>ActionForm Beans </li></ul><ul><ul><li>Extends the ActionForm class </li></ul></ul><ul><ul><li>Create one for each input form in the application </li></ul></ul><ul><ul><li>If defined in the ActionMapping configuration file, the Controller Servlet will perform the following: </li></ul></ul><ul><ul><ul><li>Check session for instance of bean of appropriate class </li></ul></ul></ul><ul><ul><ul><li>If no session bean exists, one is created automatically </li></ul></ul></ul><ul><ul><ul><li>For every request parameter whose name corresponds to the name of a property in the bean, the corresponding setter method will be called </li></ul></ul></ul><ul><ul><ul><li>The updated ActionForm bean will be passed to the Action Class perform() method when it is called, making these values immediately available </li></ul></ul></ul>
    13. 13. STRUTS Model Components <ul><li>When coding ActionForm beans consider: </li></ul><ul><ul><li>The ActionForm class itself requires no specific methods to be implemented. It is used to identify the role these particular beans play in the overall architecture. Typically, an ActionForm bean will have only property getter and property setter methods, with no business logic </li></ul></ul><ul><ul><li>The ActionForm object also offers a standard validation mechanism. If you override a &quot;stub&quot; method, and provide error messages in the standard application resource, Struts will automatically validate the input from the form </li></ul></ul>
    14. 14. STRUTS Model Components <ul><li>Continued </li></ul><ul><ul><li>Define a property (with associated getXxx() and setXxx() methods) for each field that is present in the form. The field name and property name must match according to the usual JavaBeans conventions </li></ul></ul><ul><ul><li>Place a bean instance on your form, and use nested property references. For example, you have a &quot;customer&quot; bean on your Action Form, and then refer to the property &quot;customer.name&quot; in your JSP view. This would correspond to the methods customer.getName() and customer.setName(string Name) on your customer bean </li></ul></ul>
    15. 15. STRUTS Model Components <ul><li>System State Beans </li></ul><ul><ul><li>Actual state of a system is normally represented as a set of one or more JavaBeans classes, whose properties define the current state </li></ul></ul><ul><ul><li>A shopping cart system, for example, will include a bean that represents the cart being maintained for each individual shopper, and will (among other things) include the set of items that the shopper has currently selected for purchase </li></ul></ul>
    16. 16. STRUTS Model Components <ul><li>Business Logic Beans </li></ul><ul><ul><li>Should encapsulate the functional logic of your application as method calls on JavaBeans designed for this purpose </li></ul></ul><ul><ul><li>For maximum code re-use, business logic beans should be designed and implemented so that they do not know they are being executed in a web application environment </li></ul></ul><ul><ul><li>For small to medium sized applications, business logic beans might be ordinary JavaBeans that interact with system state beans passed as arguments, or ordinary JavaBeans that access a database using JDBC calls </li></ul></ul>
    17. 17. STRUTS Model Components <ul><li>Business Logic Beans - Continued </li></ul><ul><ul><li>For larger applications, these beans will often be stateful or stateless Enterprise JavaBeans (EJBs) </li></ul></ul>
    18. 18. STRUTS Model Components <ul><li>Accessing Relational Databases </li></ul><ul><ul><li>Struts can define the datasources for an application from within its standard configuration file </li></ul></ul><ul><ul><li>A simple JDBC connection pool is also provided </li></ul></ul>
    19. 19. View Components Unit 3
    20. 20. STRUTS View Components <ul><li>Internationalized Messages </li></ul><ul><ul><li>Struts builds upon Java platform to provide assistance for building internationalized and localized applications </li></ul></ul><ul><ul><ul><li>Locale - fundamental Java class that supports internationalization </li></ul></ul></ul><ul><ul><ul><li>ResourceBundle - supports messages in multiple languages </li></ul></ul></ul><ul><ul><ul><li>PropertyResourceBundle - standard implementation of ResourceBundle that allows you to define resources using the same &quot;name=value&quot; syntax used to initialize properties files </li></ul></ul></ul><ul><ul><ul><li>MessageFormat - allows you to replace portions of a message string with arguments specified at run time </li></ul></ul></ul><ul><ul><ul><li>MessageResources - lets you treat a set of resource bundles like a database, and allows you to request a particular message string for a particular Locale </li></ul></ul></ul>
    21. 21. STRUTS View Components <ul><li>ApplicationResources.properties </li></ul><ul><ul><li>Contains the messages in the default language for your server. If your default language is English, you might have an entry like this: prompt.hello=Hello </li></ul></ul><ul><li>ApplicationResources_xx.properties </li></ul><ul><ul><li>Contains the same messages in the language whose ISO language code is &quot;xx&quot; </li></ul></ul>
    22. 22. STRUTS View Components <ul><li>Forms and FormBean interactions </li></ul><ul><ul><li>HTML Forms and their limitations </li></ul></ul><ul><ul><li>Errors not easily handled </li></ul></ul>
    23. 23. STRUTS View Components <ul><li><%@ page language=&quot;java&quot; %> </li></ul><ul><li><%@ taglib uri=&quot;/WEB-INF/struts-html.tld&quot; prefix=&quot;html&quot; %> </li></ul><ul><li><%@ taglib uri=&quot;/WEB-INF/struts-bean.tld&quot; prefix=&quot;bean&quot; %> </li></ul><ul><li><html:html> </li></ul><ul><li><head> <title> <bean:message key=&quot;logon.title&quot;/> </title> </li></ul><ul><li><body bgcolor=&quot;white&quot;> </li></ul><ul><li><html:errors/> </li></ul><ul><li><html:form action=“/logonpath.do&quot;> </li></ul><ul><li><table border=&quot;0&quot; width=&quot;100%&quot;> </li></ul><ul><li><tr> <th align=&quot;right&quot;> <html:message key=&quot;prompt.username&quot;/> </th> </li></ul><ul><li><td align=&quot;left&quot;> <html:text property=&quot;username&quot; size=&quot;16&quot;/> </td> </tr> </li></ul><ul><li><tr> <td align=&quot;right&quot;> <html:submit> <bean:message key=&quot;button.submit&quot;/> </html:submit> </td> </li></ul>
    24. 24. STRUTS View Components <ul><li>Building Forms with Struts </li></ul><ul><ul><li>The taglib directive tells the JSP page compiler where to find the tag library descriptor for the Struts tag library </li></ul></ul><ul><ul><li>message tag is used to look up internationalized message strings from a MessageResources object containing all the resources for this application </li></ul></ul><ul><ul><li>The errors tag displays any error messages that have been stored by a business logic component, or nothing if no errors have been stored </li></ul></ul>
    25. 25. STRUTS View Components <ul><li>Building Forms with Struts – continued </li></ul><ul><ul><li>The form tag renders an HTML <form> element, based on the specified attributes </li></ul></ul><ul><ul><li>The form tag also associates all of the fields within this form with a request scoped FormBean that is stored under the key FormName </li></ul></ul><ul><ul><li>The form bean can also be specified in the Struts configuration file, in which case the Name and Type can be omitted here </li></ul></ul><ul><ul><li>The text tag renders an HTML <input> element of type &quot;text“ </li></ul></ul><ul><ul><li>The submit and reset tags generate the corresponding buttons at the bottom of the form </li></ul></ul>
    26. 26. STRUTS View Components <ul><li>Input field types supported </li></ul><ul><ul><li>checkboxes </li></ul></ul><ul><ul><li>hidden fields </li></ul></ul><ul><ul><li>password input fields </li></ul></ul><ul><ul><li>radio buttons </li></ul></ul><ul><ul><li>reset buttons </li></ul></ul><ul><ul><li>select lists </li></ul></ul><ul><ul><li>options </li></ul></ul><ul><ul><li>submit buttons </li></ul></ul><ul><ul><li>text input fields </li></ul></ul><ul><ul><li>textareas </li></ul></ul>
    27. 27. STRUTS View Components <ul><li>Useful Presentation Tags </li></ul><ul><ul><li>[logic] iterate repeats its tag body once for each element of a specified collection (which can be an Enumeration, a Hashtable, a Vector, or an array of objects) </li></ul></ul><ul><ul><li>[logic] present depending on which attribute is specified, this tag checks the current request, and evaluates the nested body content of this tag only if the specified value is present </li></ul></ul><ul><ul><li>[logic] notPresent the companion tag to present, notPresent provides the same functionality when the specified attribute is not present </li></ul></ul>
    28. 28. STRUTS View Components <ul><li>Useful Presentation Tags – continued </li></ul><ul><ul><li>[html] link generates a HTML <a> element as an anchor definition or a hyperlink to the specified URL, and automatically applies URL encoding to maintain session state in the absence of cookie support </li></ul></ul><ul><ul><li>[html] img generates a HTML <img> element with the ability to dynamically modify the URLs specified by the &quot;src&quot; and &quot;lowsrc&quot; attributes in the same manner that <html:link> can </li></ul></ul><ul><ul><li>[bean] parameter retrieves the value of the specified request parameter, and defines the result as a page scope attribute of type String or String </li></ul></ul>
    29. 29. STRUTS View Components <ul><li>Automatic Form Validation </li></ul><ul><ul><li>Struts offers an additional facility to validate the input fields it has received </li></ul></ul><ul><ul><li>To utilize this feature, override the validate() method in your ActionForm class </li></ul></ul><ul><ul><li>The validate() method is called by the controller servlet after the bean properties have been populated, but before the corresponding action class's perform() method is invoked </li></ul></ul>
    30. 30. STRUTS View Components <ul><li>Page Composition with Includes </li></ul><ul><ul><li>The development of the various segments of a site is easier if you can divide up the work, and assign different developers to the different segments </li></ul></ul><ul><ul><li>Use the include capability of JavaServer Pages technology to combine the results into a single result page, or use the include tag provided with Struts </li></ul></ul>
    31. 31. STRUTS View Components <ul><li>Page Composition with Includes – continued </li></ul><ul><ul><li>There are three types of include available, depending on when you want the combination of output to occur: </li></ul></ul><ul><ul><ul><li>An <%@ include file=&quot;xxxxx&quot; %> directive can include a file that contains java code or jsp tags </li></ul></ul></ul><ul><ul><ul><li>The include action ( <jsp:include page=&quot;xxxxx&quot; flush=&quot;true&quot; /> ) is processed at request time, and is handled transparently by the server </li></ul></ul></ul><ul><ul><ul><li>The bean:include tag takes either a an argument &quot;forward&quot; representing a logical name mapped to the jsp to include, or the &quot;id&quot; argument, which represents a page context String variable to print out to the jsp page </li></ul></ul></ul>
    32. 32. Controller Components Unit 4
    33. 33. STRUTS Controller Components <ul><li>Struts includes a Servlet that implements the primary function of mapping a request URI to an Action class (ActionServlet) </li></ul>
    34. 34. STRUTS Controller Components <ul><li>Your primary responsibilities are: </li></ul><ul><ul><li>Write an Action class (that is, an extension of the Action class) for each logical request that may be received </li></ul></ul><ul><ul><li>Write the action mapping configuration file (in XML) that is used to configure the controller servlet (struts-config.xml) </li></ul></ul><ul><ul><li>Update the web application deployment descriptor file (in XML) for your application to include the necessary Struts components </li></ul></ul><ul><ul><li>Add the appropriate Struts components to your application </li></ul></ul>
    35. 35. STRUTS Controller Components <ul><li>Action Classes: </li></ul><ul><ul><li>The Action class defines a perform method that you override </li></ul></ul><ul><ul><ul><li>public ActionForward perform(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException; </li></ul></ul></ul>
    36. 36. STRUTS Controller Components <ul><li>The goal of an Action class is to process this request, and then to return an ActionForward object that identifies the JSP page (if any) to which control should be forwarded to generate the corresponding response </li></ul>
    37. 37. STRUTS Controller Components <ul><li>A typical Action class will implement the following logic in its perform() method </li></ul><ul><ul><li>Validate the current state of the user's session </li></ul></ul><ul><ul><li>If validation has not yet occurred, validate the form bean properties as necessary </li></ul></ul><ul><ul><li>Perform the processing required to deal with this request </li></ul></ul><ul><ul><li>Update the server-side objects that will be used to create the next page of the user interface </li></ul></ul><ul><ul><li>Return an appropriate ActionForward object that identifies the JSP page to be used to generate this response, based on the newly updated beans </li></ul></ul>
    38. 38. STRUTS Controller Components <ul><li>Design issues to remember when coding Action classes include the following </li></ul><ul><ul><li>The controller Servlet creates only one instance of your Action class, and uses it for all requests. Thus, you need to code your Action class so that it operates correctly in a multi-threaded environment, just as you must code a Servlet's service() method safely </li></ul></ul><ul><ul><li>The most important principle that aids in thread-safe coding is to use only local variables, not instance variables, in your Action class </li></ul></ul>
    39. 39. STRUTS Controller Components <ul><li>Design issues to remember when coding Action classes include the following – continued </li></ul><ul><ul><li>The beans that represent the Model of your system may throw exceptions due to problems accessing databases or other resources. You should trap all such exceptions in the logic of your perform() method, and log them to the application logfile </li></ul></ul><ul><ul><li>As a general rule, allocating scarce resources and keeping them across requests from the same user (in the user's session) can cause scalability problems </li></ul></ul>
    40. 40. STRUTS Controller Components <ul><li>The ActionMapping Implementation </li></ul><ul><ul><ul><li>type - Fully qualified Java class name of the Action implementation class used by this mapping. </li></ul></ul></ul><ul><ul><ul><li>name - The name of the form bean defined in the config file that this action will use </li></ul></ul></ul><ul><ul><ul><li>path - The request URI path that is matched to select this mapping. See below for examples of how matching works. </li></ul></ul></ul><ul><ul><ul><li>unknown - Set to true if this action should be configured as the default for this application, to handle all requests not handled by another action. Only one action can be defined as a default within a single application. </li></ul></ul></ul><ul><ul><ul><li>validate - Set to true if the validate() method of the action associated with this mapping should be called. </li></ul></ul></ul>
    41. 41. STRUTS Controller Components <ul><li>The Actions Mapping Configuration File </li></ul><ul><ul><li>The developer's responsibility is to create an XML file named struts-config.xml , and place it in the WEB-INF directory of your application </li></ul></ul><ul><ul><li>The outermost XML element must be <struts-config> </li></ul></ul><ul><ul><li>Inside of the <struts-config> element, there two important elements that you use to describe your actions: </li></ul></ul>
    42. 42. STRUTS Controller Components <ul><ul><ul><li><form-beans> This section contains your form bean definitions. You use a <form-bean> element for each form bean, which has the following important attributes: </li></ul></ul></ul><ul><ul><ul><ul><li>name : The name of the request or session level attribute that this form bean will be stored as </li></ul></ul></ul></ul><ul><ul><ul><ul><li>type : The fully-qualified Java classname of your form bean </li></ul></ul></ul></ul>
    43. 43. STRUTS Controller Components <ul><ul><li><action-mappings> This section contains your action definitions. You use an <action> element for each of your actions you would like to define. Each action element has requires the following attributes to be defined: </li></ul></ul><ul><ul><ul><li>path : The application context-relative path to the action </li></ul></ul></ul><ul><ul><ul><li>type : The fully qualified java classname of your Action class </li></ul></ul></ul><ul><ul><ul><li>name : The name of your <form-bean> element to use with this action </li></ul></ul></ul>
    44. 44. STRUTS Controller Components <ul><ul><li>One more section of good use is the <data-sources> section, which specifies data sources that your application can use.This is how you would specify a basic data source for your application inside of struts-config.xml: </li></ul></ul><ul><ul><ul><li><struts-config> </li></ul></ul></ul><ul><ul><ul><li><data-sources> </li></ul></ul></ul><ul><ul><ul><li><data-source autoCommit=&quot;false&quot; </li></ul></ul></ul><ul><ul><ul><li>description=&quot;Example Data Source Description&quot; driverClass=&quot;org.postgresql.Driver&quot; maxCount=&quot;4&quot; minCount=&quot;2&quot; password=&quot;mypassword&quot; url=&quot;jdbc:postgresql://localhost/mydatabase&quot; user=&quot;myusername&quot;/> </li></ul></ul></ul><ul><ul><ul><li></data-sources> </li></ul></ul></ul><ul><ul><ul><li></struts-config> </li></ul></ul></ul>
    45. 45. STRUTS Controller Components <ul><li>The Web Application Deployment Descriptor </li></ul><ul><ul><li>The final step in setting up the application is to configure the application deployment descriptor (stored in file WEB-INF/web.xml ) to include all the Struts components that are required </li></ul></ul>
    46. 46. Tag Libraries Unit 5
    47. 47. STRUTS Tag Libraries <ul><li>HTML Tags </li></ul><ul><li>Bean Tags </li></ul><ul><li>Logic Tags </li></ul><ul><li>Template Tags </li></ul><ul><li>Custom Tags </li></ul>
    48. 48. HTML Tags <ul><li>The tags in the Struts HTML library form a bridge between a JSP view and the other components of a Web application. Since a dynamic Web application often depends on gathering data from a user, input forms play an important role in the Struts framework. Consequently, the majority of the HTML tags involve HTML forms. Other important issues addressed by the Struts-HTML tags are messages, error messages, hyperlinking and internationalization. </li></ul>
    49. 49. HTML Tags <ul><li>HTML &quot;form&quot; tags </li></ul><ul><ul><li>button </li></ul></ul><ul><ul><li>cancel </li></ul></ul><ul><ul><li>checkboxes </li></ul></ul><ul><ul><li>file </li></ul></ul><ul><ul><li>hidden </li></ul></ul><ul><ul><li>image </li></ul></ul><ul><ul><li>multibox </li></ul></ul><ul><ul><li>password input fields </li></ul></ul><ul><ul><li>radio buttons </li></ul></ul><ul><ul><li>reset buttons </li></ul></ul><ul><li>HTML &quot;form&quot; tags </li></ul><ul><ul><li>select lists with embedded </li></ul></ul><ul><ul><li>option </li></ul></ul><ul><ul><li>options </li></ul></ul><ul><ul><li>submit buttons </li></ul></ul><ul><ul><li>text input fields </li></ul></ul><ul><ul><li>textareas </li></ul></ul>
    50. 50. HTML Tags – Typical HTML Form <HTML> <BODY> <FORM> <TABLE WIDTH=&quot;100%&quot;> <TR><TD>First Name</TD> <TD><INPUT TYPE=&quot;TEXT&quot; NAME=&quot;Name&quot; SIZE=&quot;40&quot; MAXLENGTH=&quot;40&quot;></TD></TR> <TR><TD>Street Address</TD> <TD><INPUT TYPE=&quot;TEXT&quot; NAME=&quot;Address&quot; SIZE=&quot;40&quot; MAXLENGTH=&quot;40&quot;></TD></TR> <TR><TD>City</TD> <TD><INPUT TYPE=&quot;TEXT&quot; NAME=&quot;City&quot; SIZE=&quot;20&quot; MAXLENGTH=&quot;20&quot;></TD></TR> <TR><TD>State</TD> <TD><INPUT TYPE=&quot;TEXT&quot; NAME=&quot;State&quot; SIZE=&quot;2&quot; MAXLENGTH=&quot;2&quot;></TD></TR> <TR><TD>Postal Code</TD> <TD><INPUT TYPE=&quot;TEXT&quot; NAME=&quot;ZipCode&quot; SIZE=&quot;9&quot; MAXLENGTH=&quot;9&quot;></TD></TR> <TR><TD ALIGN=CENTER><INPUT TYPE=&quot;SUBMIT&quot; NAME=&quot;Submit&quot; VALUE=&quot;Save&quot;></TD> <TD><INPUT TYPE=&quot;RESET&quot; NAME=&quot;Reset&quot; VALUE=&quot;Cancel&quot;></TD></TR> </TABLE> </FORM> </BODY> </HTML>
    51. 51. HTML Tags – Typical Struts Form <HTML:HTML> <BODY> <HTML:FORM Action=&quot;/CustomerForm&quot; focus=“name” > <TABLE WIDTH=&quot;100%&quot;> <TR><TD><bean:message key=&quot;customer.name&quot;/></TD> <TD><HTML:TEXT property=&quot;name&quot; size=&quot;40&quot; maxlength=&quot;40&quot; /></TD></TR> <TR><TD><bean:message key=&quot;customer.address&quot;/></TD> <TD><HTML:TEXT property=&quot;address&quot; size =&quot;40&quot; maxlength =&quot;40&quot; /></TD></TR> <TR><TD><bean:message key=&quot;customer.city&quot;/></TD> <TD><HTML:TEXT property=&quot;city&quot; size =&quot;20&quot; maxlength =&quot;20&quot; /></TD></TR> <TR><TD><bean:message key=&quot;customer.state&quot;/></TD> <TD><HTML:TEXT property=&quot;state&quot; size =&quot;2&quot; maxlength =&quot;2&quot; /></TD></TR> <TR><TD><bean:message key=&quot;customer.zip&quot;/></TD> <TD><HTML:TEXT property=&quot;zip&quot; size =&quot;9&quot; maxlength =&quot;9&quot; /></TD></TR> <TR><TD ALIGN=CENTER><html:submit property=&quot;action&quot; value =&quot;Save&quot;/></TD> <TD><html:reset property=&quot;action&quot; value =&quot;Reset&quot;/></TD></TR> </TABLE> </HTML:FORM> </BODY> </HTML:HTML>
    52. 52. Bean Tags <ul><li>The &quot;struts-bean&quot; tag library provides substantial enhancements to the basic capability provided by <jsp:useBean> , as discussed in the following sections: </li></ul><ul><ul><li>Bean Properties - Extended syntax to refer to JavaBean properties with simple names (same as the standard JSP tags <jsp:getProperty> and <jsp:setProperty> ), nested names (a property named address.city returns the value retrieved by the Java expression getAddress().getCity() ), and indexed names (a property named address[3] retrieves the fourth address from the indexed &quot;address&quot; property of a bean). </li></ul></ul><ul><ul><li>Bean Creation - New JSP beans, in any scope, can be created from a variety of objects and APIs associated with the current request, or with the servlet container in which this page is running. </li></ul></ul><ul><ul><li>Bean Output - Supports the rendering of textual output from a bean (or bean property), which will be included in the response being created by your JSP page. </li></ul></ul>
    53. 53. Bean Tags   Tag Name Description cookie Define a scripting variable based on the value(s) of the specified request cookie. define Define a scripting variable based on the value(s) of the specified bean property. header Define a scripting variable based on the value(s) of the specified request header. include Load the response from a dynamic application request and make it available as a bean. message Render an internationalized message string to the response. page Expose a specified item from the page context as a bean. parameter Define a scripting variable based on the value(s) of the specified request parameter. resource Load a web application resource and make it available as a bean. size Define a bean containing the number of elements in a Collection or Map. struts Expose a named Struts internal configuration object as a bean. write Render the value of the specified bean property to the current JspWriter.
    54. 54. Bean Tag Example <table border=&quot;2&quot;> <tr> <th align=&quot;left&quot;><bean:message key=“imagebroker.lob”/></th> <th align=&quot;left&quot;><bean:message key=“imagebroker.unitnbr”/></th> <th align=&quot;left&quot;><bean:message key=“imagebroker.onbase_dns”/></th> </tr> <logic:iterate id=&quot;image&quot; property=&quot;collection&quot; name=&quot;ImageLocationListForm&quot;> <tr> <td><a href=&quot;ImageLocationListForm.do?lob=<bean:write name='image' property='lob'/> &unitnbr=<bean:write name='image' property='unitnbr'/> &onbase_dns=<bean:write name='image' property='onbase_dns'/>&quot; > <bean:write name=&quot;image&quot; property=&quot;lob&quot;/></a></td> <td><bean:write name=&quot;image&quot; property=&quot;unitnbr&quot;/></td> <td><bean:write name=&quot;image&quot; property=&quot;onbase_dns&quot;/></td> </tr> </logic:iterate> </table>
    55. 55. Logic Tags <ul><li>The Logic tag library contains tags that are useful in managing conditional generation of output text, looping over object collections for repetitive generation of output text, and application flow management. </li></ul>
    56. 56. Logic Tags <ul><li>For tags that do value comparisons ( equal , greaterEqual , greaterThan , lessEqual , lessThan , notEqual ), the following rules apply: </li></ul><ul><ul><li>The specified value is examined. If it can be converted successfully to a double or a long , it is assumed that the ultimate comparison will be numeric (either floating point or integer). Otherwise, a String comparison will be performed. </li></ul></ul><ul><ul><li>The variable to be compared to is retrieved, based on the selector attribute(s) ( cookie , header , name , parameter , property ) present on this tag. It will be converted to the appropriate type for the comparison, as determined above. </li></ul></ul><ul><ul><li>A request time exception will be thrown if the specified variable cannot be retrieved, or has a null value. </li></ul></ul><ul><ul><li>The specific comparison for this tag will be performed, and the nested body content of this tag will be evaluated if the comparison returns a true result. </li></ul></ul>
    57. 57. Logic Tags <ul><li>For tags that do substring matching ( match , notMatch ), the following rules apply: </li></ul><ul><ul><li>The specified variable is retrieved, based on the selector attribute(s) ( cookie , header , name , parameter , property ) present on this tag. The variable is converted to a String, if necessary. </li></ul></ul><ul><ul><li>A request time exception will be thrown if the specified variable cannot be retrieved, or has a null value. </li></ul></ul><ul><ul><li>The specified value is checked for existence as a substring of the variable, in the position specified by the location attribute, as follows: at the beginning (if location is set to start ), at the end (if location is set to end ), or anywhere (if location is not specified). </li></ul></ul>
    58. 58. Logic Tags   Tag Name Description empty Evaluate the nested body content of this tag if the requested variable is either null or an empty string. equal Evaluate the nested body content of this tag if the requested variable is equal to the specified value. forward Forward control to the page specified by the specified ActionForward entry. greaterEqual Evaluate the nested body content of this tag if requested variable is greater than or equal to specified value. greaterThan Evaluate the nested body content of this tag if the requested variable is greater than the specified value. iterate Repeat the nested body content of this tag over a specified collection. lessEqual Evaluate the nested body content of this tag if requested variable is greater than or equal to specified value. lessThan Evaluate the nested body content of this tag if the requested variable is less than the specified value. match Evaluate the nested body content of this tag if specified value is an appropriate substring of requested variable. messagesNotPresent Generate the nested body content of this tag if the specified message is not present in this request. messagesPresent Generate the nested body content of this tag if the specified message is present in this request. notEmpty Evaluate the nested body content of this tag if the requested variable is neither null nor an empty string. notEqual Evaluate the nested body content of this tag if the requested variable is not equal to the specified value. notMatch Evaluate the nested body content of tag if specified value not an appropriate substring of requested variable. notPresent Generate the nested body content of this tag if the specified value is not present in this request. present Generate the nested body content of this tag if the specified value is present in this request. redirect Render an HTTP Redirect
    59. 59. Logic Tags - Example <html:html> <head> <title><bean:message key=&quot;imagebrokerlink.title&quot;/></title> <META name=&quot;GENERATOR&quot; content=&quot;IBM WebSphere Studio&quot;> </head> <body> <html:form action=&quot;/ImageLocationForm&quot; > <center> <font size=3> <br> <b> <logic:notEqual property=&quot;action&quot; name=&quot;ImageLocationForm&quot; value=&quot;Insert&quot;> <bean:message key=&quot;imagelocationdetail.title&quot;/> </logic:notEqual> <logic:equal property=&quot;action&quot; name=&quot;ImageLocationForm&quot; value=&quot;Insert&quot;> <bean:message key=&quot;imagelocationinsert.title&quot;/> </logic:equal> … …
    60. 60. Template Tags <ul><li>The Template tag library contains three tags: put, get, and insert. Put tags put content into request scope, which is retrieved by a get tag in a different JSP page (the template). That template is included with the insert tag. </li></ul>
    61. 61. Template Tags <ul><li>Insert Inserts (includes, actually) a template. Templates are JSP pages that include parameterized content. That content comes from put tags that are children of insert tags. </li></ul><ul><li>Put Puts content into request scope. </li></ul><ul><li>Get Gets the content from request scope that was put there by a put tag. </li></ul>
    62. 62. Custom Tags <%@ taglib uri=&quot;WEB-INF/imagebroker.tld&quot; prefix=&quot;broker&quot; %> <table width=750 cellspacing=0 cellpadding=2 border=2 > <tr> <td><broker:form lob='<%=test.getLob()%>' unitnbr='<%=test.getUnitnbr()%>' userid='<%=test.getUserid()%>' > <broker:doctype value=&quot;Invoice&quot;/> <broker:keyword name=&quot;CompanyNbr&quot; value=&quot;55555&quot;/> <broker:keyword name=&quot;PONbr&quot; value=&quot;M12345&quot;/> <broker:constraint name=&quot;FromDate&quot; value=&quot;02/02/2002&quot;/> <broker:constraint name=&quot;ToDate&quot; value=&quot;02/28/2002&quot;/> Image Broker Link Test </broker:form> </td> </tr> </table>
    63. 63. Custom Tags – tld File <tag> <name>doctype</name> <tagclass>com.pri.brokertag.ImageBrokerDoctype</tagclass> <attribute> <name>value</name> <required>true</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag>
    64. 64. Custom Tags – Tag Class public class ImageBrokerDoctype extends TagSupport { private String value = null; public int doStartTag() throws JspException { Hashtable ht = null; String keyword_count = null; int iCnt = 0; HttpServletRequest request = (HttpServletRequest) pageContext.getRequest(); ht = (Hashtable) request.getAttribute(&quot;keyword_parms&quot;); keyword_count = (String) request.getAttribute(&quot;queryobject_count&quot;); iCnt ++; ht.put(&quot;QueryObject&quot; + iCnt, value); request.setAttribute(&quot;keyword_parms&quot;, ht); request.setAttribute(&quot;queryobject_count&quot;, new String(new Integer(iCnt).toString())); return EVAL_PAGE; } }
    65. 65. STRUTS Configuration File Unit 6
    66. 66. STRUTS Configuration File <ul><li>The developer's responsibility is to create an XML file named struts-config.xml , and place it in the WEB-INF directory of your application. This format of this document is constrained by it's definition in &quot;struts-config_1_0.dtd&quot;. The outermost XML element must be <struts-config> . </li></ul>
    67. 67. STRUTS Configuration File <ul><li>Inside of the <struts-config> element, there are two important elements that are used to describe your actions: </li></ul><ul><li><form-beans> This section contains your form bean definitions. You use a <form-bean> element for each form bean, which has the following important attributes: </li></ul><ul><ul><li>name : A unique identifier for this bean, which will be used to reference it in corresponding action mappings. Usually, this is also the name of the request or session attribute under which this form bean will be stored. </li></ul></ul><ul><ul><li>type : The fully-qualified Java classname of your form bean. </li></ul></ul><ul><li><action-mappings> This section contains your action definitions. You use an <action> element for each of your actions you would like to define. Each action element requires the following attributes to be defined: </li></ul><ul><ul><li>path : The application context-relative path to the action </li></ul></ul><ul><ul><li>type : The fully qualified java classname of your Action class </li></ul></ul><ul><ul><li>name : The name of your <form-bean> element to use with this action </li></ul></ul>
    68. 68. Struts-config.xml <?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot; ?> <!DOCTYPE struts-config PUBLIC &quot;-//Apache Software Foundation//DTD Struts Configuration 1.0//EN&quot; &quot;http://jakarta.apache.org/struts/dtds/struts-config_1_0.dtd&quot;> <struts-config> <!-- ========== Form Bean Definitions =================================== --> <form-beans> <form-bean name=&quot;CryptForm&quot; type=&quot;com.pri.imagebrokerWeb.CryptForm&quot; /> </form-beans> <!-- ========== Global Forward Definitions ============================== --> <global-forwards> <forward name=&quot;start&quot; path=&quot;/index.html&quot;/> </global-forwards> <!-- ========== Action Mapping Definitions ============================== --> <action-mappings> <action path=&quot;/CryptForm&quot; type=&quot;com.pri.imagebrokerWeb.CryptAction&quot; name=&quot;CryptForm&quot; scope=&quot;request&quot; input=&quot;/pgCrypt.jsp&quot;> <forward name=&quot;encrypt&quot; path=&quot;/pgCryptDisplay.jsp&quot;/> </action> </action-mappings> </struts-config>
    69. 69. Web Application Descriptor File Unit 7
    70. 70. Web.xml File <ul><li>The final step in setting up the application is to configure the application deployment descriptor (stored in file WEB-INF/web.xml ) to include all the Struts components that are required. Using the deployment descriptor for the example application as a guide, we see that the following entries need to be created or modified. </li></ul>
    71. 71. Web.xml File <?xml version=&quot;1.0&quot; encoding=&quot;ISO-8859-1&quot;?> <!DOCTYPE web-app PUBLIC &quot;-//Sun Microsystems, Inc.//DTD Web Application 2.2//EN&quot; &quot;http://java.sun.com/j2ee/dtds/web-app_2_2.dtd&quot;> <web-app id=&quot;WebApp&quot;> <display-name>imagebrokerWeb</display-name> <!-- Action Servlet Configuration --> <servlet id=&quot;Servlet_1&quot;> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>application</param-name><param-value>imagebrokerWeb</param-value> </init-param> <init-param> <param-name>config</param-name><param-value>WEB-INF/struts-config.xml</param-value> </init-param> </servlet>
    72. 72. Web.xml File - continued <!-- Action Servlet Mapping --> <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <!-- The Welcome File List --> <welcome-file-list> <welcome-file>index.html</welcome-file> </welcome-file-list>
    73. 73. Web.xml File - continued <!-- Struts Tag Library Descriptors --> <taglib> <taglib-uri>WEB-INF/struts-bean.tld</taglib-uri> <taglib-location>/WEB-INF/struts-bean.tld</taglib-location> </taglib> <taglib> <taglib-uri>WEB-INF/struts-html.tld</taglib-uri> <taglib-location>/WEB-INF/struts-html.tld</taglib-location> </taglib> <taglib> <taglib-uri>WEB-INF/struts-logic.tld</taglib-uri> <taglib-location>/WEB-INF/struts-logic.tld</taglib-location> </taglib> </web-app>
    74. 74. Application Resources File Unit 8
    75. 75. Application.properties File error.cryptvalue.required=<li>You must enter some text.</li> error.lob.required=<li>You must enter the Line of Business.</li> error.unitnbr.required=<li>You must enter the Unit Number.</li> error.onbase_dns.required=<li>You must enter the OnBase DNS.</li> imagebroker.linkname=Project Refinery, Inc. imagebroker.title=pri Image Broker imagebrokerlink.title=pri Image Broker Link Test imagelocationlist.title=Image Location List imagelocationdetail.title=Image Location Detail imagelocationinsert.title=Image Location Insert errors.header= errors.footer=
    76. 76. Resources Unit 9
    77. 77. Resources <ul><li>Main Struts Web Site </li></ul><ul><ul><li>http://jakarta.apache.org/struts/index.html </li></ul></ul><ul><li>Struts User Guide </li></ul><ul><ul><li>http://jakarta.apache.org/struts/userGuide/index.html </li></ul></ul><ul><li>Various Struts Resources </li></ul><ul><ul><li>http://jakarta.apache.org/struts/resources.html </li></ul></ul><ul><li>Ted Husted Web Site </li></ul><ul><ul><li>http://www.husted.com/struts/ </li></ul></ul>
    1. A particular slide catching your eye?

      Clipping is a handy way to collect important slides you want to go back to later.

    ×