Struts Overview

4,818 views

Published on

Published in: Technology, Education
0 Comments
5 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
4,818
On SlideShare
0
From Embeds
0
Number of Embeds
7
Actions
Shares
0
Downloads
229
Comments
0
Likes
5
Embeds 0
No embeds

No notes for slide

Struts Overview

  1. 1. Struts Overview Shakeel Mahate [email_address]
  2. 2. What is Struts <ul><li>Framework to develop web applications </li></ul><ul><li>Open source Jakarta project </li></ul><ul><li>http://jakarta.apache.org/struts/ </li></ul><ul><li>Current release at 1.1 Beta 3 </li></ul><ul><li>Based on MVC-Model 2 pattern </li></ul>
  3. 3. What is MVC-Model 2 Browser Controller Servlet Model JavaBean View JSP HTML Stylesheets Form submit HTTP Event HTTP Response Get property Forward Create/Set property Business Layer
  4. 4. What is Struts MVC-Model 2 Browser Controller ActionServlet Model JavaBean View JSP Business Layer ActionForm Struts- Config. xml Business Logic Action Resource Bundle create Http Event Http Response forward dispatch create/set get forward
  5. 5. What is ActionServlet <ul><li>Traffic cop </li></ul><ul><li>Part of the Struts Framework </li></ul><ul><li>Consults struts-config.xml to determine Action – ActionForm mapping </li></ul><ul><li>Transforms request parameters into ActionForm </li></ul><ul><ul><li>Type safety, validation </li></ul></ul>
  6. 6. What is Action <ul><li>Extend org.apache.struts.action.Action </li></ul><ul><li>Override execute() </li></ul><ul><li>Talk to your business layer to perform the action </li></ul><ul><li>Populates data in request/session scope </li></ul><ul><li>Tells Struts which page to display </li></ul><ul><ul><li>return mapping.findForward(“displayResults”); </li></ul></ul>
  7. 7. What is struts-config.xml <ul><li>Similar to web.xml which configures logical names for Servlets and specifies the Java class name </li></ul><ul><li>Configures action mappings, form beans, forwards (views), resource bundles, plugins </li></ul>
  8. 8. Resource Bundles <ul><li>Configures message resource bundles </li></ul><ul><li><message-resources parameter=“myApplication”/> </li></ul><ul><li>Utilized to NLS enable application </li></ul><ul><li>All static strings should be specified in myApplication.properties file </li></ul><ul><li>Format: key=value </li></ul><ul><ul><li>Name.label=Enter first name </li></ul></ul>
  9. 9. Global forwards <ul><li>Remember RequestDispatcher.forward() method – hence the name forward!!! </li></ul><ul><li>Logical name for all global filenames </li></ul><ul><li>Level of indirection </li></ul><ul><li>NO FILENAMES in execute() </li></ul><ul><ul><li><forward name=“displayResults” </li></ul></ul><ul><ul><li>path=“/pages/default.jsp”/> </li></ul></ul>
  10. 10. Action Mappings <ul><li>Maps a form name to an Action </li></ul><ul><li>Every form has an ActionForm </li></ul><ul><li>Associates Java class name to an Action </li></ul><ul><li>Associates an input page to an Action </li></ul><ul><ul><li>Page that triggered this action </li></ul></ul><ul><ul><li>Used for handling validation errors </li></ul></ul><ul><li>Associates result pages (forwards) to an Action </li></ul>
  11. 11. Action Mapping <ul><li><action path=“/createUser” </li></ul><ul><li>type=“… Java class name …” </li></ul><ul><li>scope=“request | session | application” </li></ul><ul><li>input=“another action or jsp” </li></ul><ul><li>name=“form bean name”> </li></ul><ul><li><forward name=“…” path=“…”/> </li></ul><ul><li><forward name=“…” path=“…”/> </li></ul><ul><li></action> </li></ul>Utilized by mapping. findForward() /createUser.do
  12. 12. Form Beans <ul><li>Instantiated by ActionServlet before invoking Action.execute() </li></ul><ul><li>How does ActionServlet know which form bean? </li></ul><ul><li>How does it know which Java class? </li></ul><ul><ul><li>Struts-config.xml </li></ul></ul><ul><ul><li><form-bean name=“…” </li></ul></ul><ul><ul><li>type=“… Java class name …”/> </li></ul></ul>
  13. 13. Form Bean Instantiation <ul><li>ActionServlet checks the appropriate scope for the form bean before calling class.newInstance() </li></ul><ul><li>Populates data from the request parameters and does type conversion </li></ul><ul><li>Check out BeanUtils in Jakarta Commons project </li></ul>
  14. 14. How to write Form Beans <ul><li>Zero argument constructor </li></ul><ul><li>Public getters/setters </li></ul><ul><li>DO NOT USE ENTITY BEANS as form beans </li></ul><ul><li>Use Data Transfer Objects (Value Objects) </li></ul><ul><li>Decouple web app from business layer </li></ul>
  15. 15. View <ul><li>JSP or HTML pages </li></ul><ul><li>Use Struts tag library, JSTL tag library </li></ul><ul><li>DO NOT HARD CODE labels </li></ul><ul><ul><li>Utilize ResourceBundle </li></ul></ul><ul><li>Uses beans stored in request or session or application scope </li></ul>
  16. 16. Simple View <ul><li><html:form action=“… form bean name …”> </li></ul><ul><li><bean:message key=“name.label”/> </li></ul><ul><li><html:text property=“name”/> </li></ul><ul><li><html:submit> </li></ul><ul><li>… Name of the button… </li></ul><ul><li></html:submit> </li></ul><ul><li></html:form> </li></ul>
  17. 17. Validation <ul><li>Override validate() in ActionForm </li></ul><ul><ul><li>public ActionErrors validate(ActionMapping map, HttpServletRequest req) { </li></ul></ul><ul><ul><li>ActionErrors errors = new ActionErrors(); </li></ul></ul><ul><ul><li>if (name==null || name.length() < 1) </li></ul></ul><ul><ul><li>errors.add(“nameMissing”, new ActionError(“msg.key”)); </li></ul></ul><ul><ul><li>return errors; </li></ul></ul><ul><ul><li>} </li></ul></ul><ul><li>Add the following to Input page </li></ul><ul><ul><li><html:errors property=“nameMissing”/> </li></ul></ul>
  18. 18. Validator package <ul><li>Automatically generate client side JavaScript code </li></ul><ul><li>Specify WEB-INF/validation.xml </li></ul><ul><ul><li>Define regular expressions for validating data, phone#, social security#, etc. </li></ul></ul><ul><ul><li>For each form specify required fields </li></ul></ul><ul><li>Augment rules in WEB-INF/validator-rules.xml </li></ul>
  19. 19. No need to write ActionForm <ul><li>Dynamically generate ActionForm class for your forms </li></ul><ul><li>Specify form fields in struts-config.xml </li></ul><ul><li>Basic idea is properties of a bean are a HashMap </li></ul><ul><li>Again check Jakarta Commons Project </li></ul>
  20. 20. Modular Struts <ul><li>Typical web app development consists of 4-5 programmers </li></ul><ul><li>Struts-config.xml is the bottleneck </li></ul><ul><li>Create Modules (or folders) with their own struts-config.xml </li></ul>
  21. 21. Tiles <ul><li>Templates for Web pages </li></ul><ul><li>Layout various tiles of a web page </li></ul><ul><ul><li>Header, Navigation, Body, Footer </li></ul></ul><ul><li>Specified in tiles-definition.xml </li></ul><ul><ul><li>Specifies the layout </li></ul></ul><ul><ul><li>Logical names for various tiles </li></ul></ul><ul><ul><li>Associate filenames to the tiles </li></ul></ul>
  22. 22. How to develop Struts apps <ul><li>Use WSAD 5.0 </li></ul>

×