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

Struts Overview

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