Dynamic Struts Forms By Sharad Acharya Cabela’s Inc Aug, 10 2005
Prerequisite <ul><li>Basic understanding of Struts architecture is required. </li></ul><ul><li>Understanding of following ...
Goals <ul><li>Discuss and differentiate  </li></ul><ul><ul><li>Action form  </li></ul></ul><ul><ul><li>Dyna action form  <...
Struts <ul><li>Web development framework </li></ul><ul><li>Popular trio—M-V-C (architecture pattern) </li></ul><ul><li>Mod...
Struts benefits  <ul><li>Framework </li></ul><ul><li>Free </li></ul><ul><li>Active developer community </li></ul><ul><li>V...
Struts liabilities <ul><li>Learning curve </li></ul><ul><li>Complex </li></ul><ul><li>Stiff </li></ul>
Struts = MVC <ul><li>Action Form = Model </li></ul><ul><ul><li>(hold application data)   </li></ul></ul><ul><li>Java Serve...
Action Form <ul><li>Attached to a Struts action  </li></ul><ul><li>Encapsulates application data </li></ul><ul><li>Exposes...
Action Form in action <html:form action=&quot;/ address &quot;> <br>Name <html:text property=“ name &quot;/> … … … … …  <b...
Action Form <ul><li>Implementation of Model element in MVC (+) </li></ul><ul><li>One to one matching of HTML form element ...
Dyna Action Form <ul><li>Way of instructing framework to use configuration element for properties(+) </li></ul><ul><li>Avo...
Dyna Action Form in action <html:form action=&quot;/ address &quot;> <br>Fist Name <html:text property=“ name &quot;/> … …...
Dyna Action Form <ul><li>Form properties are externalized(+) </li></ul><ul><li>Form class is lean and clean(+) </li></ul><...
Dynamic Form <ul><li>List or Map backed </li></ul><ul><li>Can be manipulated before rendering(+) </li></ul><ul><li>A top l...
Dynamic Form <ul><li>Dynamically modifiable form fields(+) </li></ul><ul><li>Cleaner Form class and struts configuration f...
A Mini Application
Starting Up … … …  <html:form action=&quot; index &quot;> <P>Click next to enter address</P> <html:submit value=&quot;Next...
Setting the Map <ul><li><action-mappings> </li></ul><ul><ul><li><action path=&quot;/ index &quot;  </li></ul></ul><ul><ul>...
Displaying the Map Contents <ul><li><P>Please enter your address</P> </li></ul><ul><li><html:form action=&quot;address&quo...
Editing Form Fields <ul><li>public class DynamicAddressAction extends Action { </li></ul><ul><ul><li>public ActionForward ...
Populating <ul><li><P>Here are your information</P> </li></ul><ul><li><logic:iterate id=&quot;myAddressMap&quot;  </li></u...
Environments <ul><li>WebSphere Application Developer (WSAD) Integration Edition version 5.1.1 </li></ul><ul><li>WSAD 5.1.1...
Known Use (1/2) <ul><li>Harleysville Insurance  ( www.harleysvillegroup.com ) </li></ul><ul><li>Problem:Commercial Auto ap...
Known Use (2/2) <ul><li>Harleysville Insurance  ( www.harleysvillegroup.com ) </li></ul><ul><li>Solution:Map and List back...
References <ul><li>Official Struts page </li></ul><ul><ul><li>http://struts.apache.org </li></ul></ul><ul><li>JavaPro onli...
Feedback <ul><li>QA on this presentation </li></ul><ul><li>Future communications </li></ul><ul><ul><li>[email_address] </l...
Thank You! Thank you for your participation. Hope to see you in future!
Upcoming SlideShare
Loading in …5
×

Dynamic Struts Forms

19,830 views

Published on

Presented in Denver Java Users Group(http://www.denverjug.org) meeting on Aug 10, 2005

0 Comments
2 Likes
Statistics
Notes
  • Be the first to comment

No Downloads
Views
Total views
19,830
On SlideShare
0
From Embeds
0
Number of Embeds
3
Actions
Shares
0
Downloads
0
Comments
0
Likes
2
Embeds 0
No embeds

No notes for slide
  • Dynamic Struts Forms

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

    ×