Download
building an application with struts n.
Skip this Video
Loading SlideShow in 5 Seconds..
Building an application … with Struts! PowerPoint Presentation
Download Presentation
Building an application … with Struts!

Building an application … with Struts!

75 Views Download Presentation
Download Presentation

Building an application … with Struts!

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. Building an application … with Struts! Presented by Ted Husted [ ted@husted.com ]

  2. Building an application … with Struts! • What is this presentation layer framework that has gained such widespread popularity? • Struts, a Model-View-Controller framework from Jakarta, allows clean separation between business logic and its presentation. • This session will introduce Struts to those new to it or want a refresher on the basics.

  3. Goal Learn the basics of the Struts framework in the context of bootstrapping an application

  4. About Ted Husted • Lead author, Struts in Action • Struts forum manager for JGuru • Struts Committer (team member) • Member, Apache Software Foundation • Working developer (just like you)

  5. About You • Developed web applications

  6. About You • Developed web applications • Developed Java web applications

  7. About You • Developed web applications • Developed Java web applications • Developed Struts applications

  8. About You • Developed web applications • Developed Java web applications • Developed Struts applications • Read Struts articles

  9. About You • Developed web applications • Developed Java web applications • Developed Struts applications • Read Struts articles • Visited Struts website

  10. About You • Developed web applications • Developed Java web applications • Developed Struts applications • Read Struts articles • Visited Struts website • Read Struts books

  11. About You • Developed web applications • Developed Java web applications • Developed Struts applications • Read Struts articles • Visited Struts website • Read Struts books

  12. Learning Objectives • Recognize a MVC architecture • Fit Struts into an overall development plan • Build a Struts application step by step • Work with fundamental Struts components, like ActionForms and Action classes • Grok the Struts workflow

  13. Talk Roadmap • What are we building it with? • What do we build first? • What do we build next? • Struts: A mile-high view

  14. Model 1 versus MVC/Model 2 • Model 1 – JSP contains business and presentation logic • Model 2 – Servlet contains business logic; JSP contains presentation logic

  15. MVC Stereo System • Media – Model • Speakers – View • Receiver - Controller

  16. Model 1 Stereo System • Walkman • Something breaks; cheaper to replace unit • With MVC/Model 2, if you blow a speaker, you can replace a speaker

  17. Presentation versus Business Logic • Presentation Logic – HTML/JSP • <bean:write name="custBean" property="discount"/> • Business Logic – Java/JDBC • custBean.setDiscount(db.Rate(custKey));

  18. Selecting a MVC framework • Several good choices • Barracuda • JPublish • Mustang • Tapestry • Turbine • WebWorks / Open Symphony

  19. Selecting a MVC framework • Struts – Jack of all trades • Complete enough • Easy enough

  20. Selecting a MVC framework • Struts – Jack of all trades • Complete enough • Easy enough • And, gosh, people like it!

  21. Talk Roadmap • What are we building it with? • What do we build first? • What do we build next? • Struts: A mile-high view

  22. Storyboard • Visio / graphical storyboard • HTML • Submit to next page • <form action="result.html"> • Hardcode a result • Static page with realistic data

  23. Storyboard

  24. What do we build first? • Storyboard • Struts Blank • It’s about the actions • Page 1, Mapping 1

  25. Struts Blank • Empty, semi-complete application • Getting started config files • Initial file structure • Rename WAR to app name • e.g. “building.war”

  26. Struts Blank • web.xml • bootstrap • application.properties • text messages (i18n) • struts-config.xml • framework core • index.jsp, Welcome.jsp

  27. web.xml <servlet> <servlet-name>action</servlet-name> <servlet-class>org.apache.struts.action.ActionServlet</servlet-class> <init-param> <param-name>application</param-name> <param-value>ApplicationResources</param-value> </init-param> <init-param> <param-name>config</param-name> <param-value>/WEB-INF/struts-config.xml</param-value> </init-param><!-- … -->

  28. web.xml <servlet-mapping> <servlet-name>action</servlet-name> <url-pattern>*.do</url-pattern> </servlet-mapping> <taglib> <taglib-uri>/WEB-INF/struts-bean.tld</taglib-uri> <taglib-location>/WEB-INF/struts-bean.tld</taglib-location> </taglib> <!-- . . . -->

  29. struts-config.xml <form-beans> <!-- properties of data-entry forms <form-bean name="logonForm" type="org.apache.struts.example.LogonForm"/> --> </form-beans> <global-forwards> <!-- workflow destinations <forward name="logon" path="/pages/logon.jsp"/> --> </global-forwards>

  30. struts-config.xml <action-mappings> <!-- Default "Welcome" action --> <!-- Forwards to Welcome.jsp --> <action path="/Welcome" forward="/pages/Welcome.jsp"/>

  31. struts-config.xml <!-- Example logon action <action path="/logon" type="org.apache.struts.example.LogonAction" name="logonForm" scope="request" input="/pages/logon.jsp"> </action> --> <!-- Example logoff action <action path="/logoff" type="org.apache.struts.example.LogoffAction"> <forward name="success" path="/pages/index.jsp"/> </action> --> </action-mappings>

  32. application.properties index.title=Struts Starter Applicationindex.heading=Hello World!index.message=To get started on your own application …

  33. Index.jsp <%@ taglib uri="/tags/struts-logic" prefix="logic" %> <logic:redirect forward="welcome"/> <%-- Redirect default requests to Welcome global ActionForward. By using a redirect, the user-agent will change address to match the path of our Welcome ActionForward. --%>

  34. welcome.jsp • index.jsp is registered as welcome page • Struts logic tag redirected to “welcome” forward • Welcome forward = “/pages/Welcome.jsp” • Client requests welcome.jsp, retains session, can now use cookies

  35. Template pattern • Struts is a base-line, “fill-in-the-blanks” framework • Not an omnibus toolkit • Many developer extensions available • Place to plug-in your own extensions • Custom JSP tags or Velocimacros • Data transformations • Workflow heuristics • Business objects

  36. What do we build first? • Storyboard • Struts Blank • It’s about the actions • Page 1, Mapping 1

  37. It’s about the actions • <form action=“client-story”> • Our one and only extension point • HTTP request – GET or POST • Action processes request • Returns response to browser

  38. What do we build first? • HTML Storyboard • Struts Blank • It’s about the actions • Page 1, Mapping 1

  39. Page 1, Mapping 1 • Rename from search.html, result.html • Change actions to result.do and search.do • <form action="/building/search.do"> • <form action="/building/result.do"> • Add "/search" and "/result" mappings • <action path="/search" forward="/search.jsp"/> • <action path="/result" forward="/result.jsp"/> • Click-through – Voila! She works

  40. Voila! She works

  41. What do we build first? • Capture client stories • Build storyboard • Bring over pages from storyboard • Migrate HTML actions to action-mappings

  42. Talk Roadmap • What are we building it with? • What do we build first? • What do we build next? • Struts: A mile-high view

  43. What do we build next? • Forms and Tags • Action Classes

  44. Forms and Tags • ActionForm • Parameter to JavaBean conversion • Validator extension point

  45. Forms and Tags • SearchForm • HTML tags • Net result: Roundtrip

  46. Forms and Tags • SearchForm • Input Properties • county, facility, permit, beforeDate, afterDate • Input Validation • Tests • Messages

  47. SearchForm

  48. SearchForm private String countyCode = null; public String getCountyCode() { return this.countyCode; } public void setCountyCode(String countyCode) { this.countyCode = countyCode; } private String countyName = null; public String getCountyName() { return this.countyName; } public void setCountyName(String countyName) { this.countyName = countyName; }