Title: 6202009
1Dynamic Struts Forms
- By Sharad Acharya
- Cabelas Inc
2Prerequisite
- Basic understanding of Struts architecture is
required. - Understanding of following J2EE elements is
expected but not required - Java Server Pages
- Servlet
3Goals
- Discuss and differentiate
- Action form
- Dyna action form
- Map/List backed action form
- Demonstrate working example of Map backed action
forms
4Struts
- Web development framework
- Popular trioM-V-C (architecture pattern)
- Model Action Forms
- View Java Server Pages
- Controller Action Servlet
5Struts benefits
- Framework
- Free
- Active developer community
- Vendor support
6Struts liabilities
- Learning curve
- Complex
- Stiff
7Struts MVC
- Action Form Model
- (hold application data)
- Java Server Page View
- (process request and generate response)
- Action Servlet Controller
- (orchestrate execution)
8Action Form
- Attached to a Struts action
- Encapsulates application data
- Exposes properties with getters/setters
- JSP field ? Action Form property
- Matching HTML form properties are automatically
populated from request
9Action Form in action
type"AddressForm" / n-mappings typeAddressAction" name" addressForm "
ard
Name propertyname"/
valueSubmit / /
public class AddressForm extends ActionForm
String name . public String getName ()
return name public void seName
(String name) this.name name
10Action Form
- Implementation of Model element in MVC ()
- One to one matching of HTML form element
- Can be manipulated dynamically (/-)
- Numerous attributes(-)
11Dyna Action Form
- Way of instructing framework to use configuration
element for properties() - Avoids change in form class to add or remove
fields () - Static definitions(/-)
12Dyna Action Form in action
type"DynaAddressForm" name"name" type"java.lang.String" /
ngs name"dynaAdrForm" path"thankyou.jsp"
Fist
Name
valueReset /
public class DynaAddressForm extends
DynaActionForm //overridden methods not
shown
13Dyna Action Form
- Form properties are externalized()
- Form class is lean and clean()
- Adding/removing fields involves change only in
configuration element () - Huge configuration file (-)
- Error prone(-)
- Fields can not be included/excluded dynamically(-)
14Dynamic Form
- List or Map backed
- Can be manipulated before rendering()
- A top level list or map holds rendering
information - Suitable to present extremely dynamic contents()
15Dynamic Form
- Dynamically modifiable form fields()
- Cleaner Form class and struts configuration
file() - Form properties depend on type of collection(-)
16A Mini Application
17Starting Up
name"indexAction" type"form.IndexForm /
n path"/index" type"action.IndexAction"
name"indexAction" path"/address.jsp" /
Click next
to enter address
18Setting the Map
public class IndexAction extends Action public
ActionForward execute( ActionMapping
mapping, ActionForm form, HttpServletRequest
request, HttpServletResponse response) throws
Exception request.getSession().setAttribute( "ma
p.address",mockBusinessCall()) return
mapping.findForward("success") private Map
mockBusinessCall() Map m new
LinkedHashMap() m.put("Name","John
Doe") m.put("Street","1 Wake Forest
Road") m.put("City","Raleigh") m.put("State","NC
") m.put("Zip","27604") return m
Click next
to enter address
type"action.IndexAction" name"indexAction" orward name"success" path"/address.jsp"
/
19Displaying the Map Contents
Please enter your address action"address" id"myAddressMap" name"addressAction"
property"fields" id"addressMapKey" name"myAddressMap"
property"key / / "
public class DynamicAddressForm extends
ActionForm Map fields new LinkedHashMap() pub
lic void reset(ActionMapping arg0,
HttpServletRequest arg1) Map addressMap
(Map)arg1.getSession().getAttribute("map.address")
if(fields.isEmpty())fields.putAll(addressMap)
public Map getFields() return fields public
void setFields(Map map) fields map
type"form.DynamicAddressForm/
type"action.DynamicAddressAction" name"success" path"/thankyou.jsp" /
20Editing Form Fields
public class DynamicAddressAction extends Action
public ActionForward execute( ActionMapping
mapping, ActionForm form, HttpServletRequest
request, HttpServletResponse response) throws
Exception DynamicAddressForm f
(DynamicAddressForm )form Map addressmap
(Map)f.getFields() return mapping.findForward("s
uccess")
type"action.DynamicAddressAction" name"success" path"/thankyou.jsp" /
21Populating
Here are your information id"myAddressMap" name"addressAction"
property"fields" name"myAddressMap" property"key"/ name"myAddressMap" property"value"/
giciterate
Thank you.
22Environments
- WebSphere Application Developer (WSAD)
Integration Edition version 5.1.1 - WSAD 5.1.1 test environment
- J2EE version 1.3
- Struts 1.1
23Known Use (1/2)
- Harleysville Insurance (www.harleysvillegroup.com)
- ProblemCommercial Auto application
-
-
- Extremely dynamic content based on
- TransactionQuote, Endorse, Inquiry
- User roleAgents, In-houses
- Line of BusinessAuto, Business owners
24Known Use (2/2)
- Harleysville Insurance (www.harleysvillegroup.com)
- SolutionMap and List backed forms
- Presentation layer calls business layer methods
- Applying rules, business layer returns data
- Forms populated using returned data
- Presentation layer renders using such forms
25References
- Official Struts page
- http//struts.apache.org
- JavaPro online article
- Part 1 http//www.fawcette.com/javapro/2003_11/on
line/dynamic_sacharya_11_11_03/ - Part 2 http//www.fawcette.com/javapro/2003_11/on
line/dynamic_sacharya_11_17_03/
26Feedback
- QA on this presentation
- Future communications
- s.acharya_at_computer.org
27Thank You!
- Thank you for your participation.
- Hope to see you in future!