Download presentation
Presentation is loading. Please wait.
Published byRonald Greer Modified over 9 years ago
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 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 Hardcode a result Static page with realistic data
23
Storyboard
25
What do we build first? Storyboard Struts Blank It’s about the actions Page 1, Mapping 1
26
Struts Blank Empty, semi-complete application Getting started config files Initial file structure Rename WAR to app name e.g. “building.war”
27
Struts Blank web.xml bootstrap application.properties text messages (i18n) struts-config.xml framework core index.jsp, Welcome.jsp
28
web.xml action org.apache.struts.action.ActionServlet application ApplicationResources config /WEB-INF/struts-config.xml
29
web.xml action *.do /WEB-INF/struts-bean.tld /WEB-INF/struts-bean.tld
30
struts-config.xml --> -->
31
struts-config.xml
32
struts-config.xml -->
33
application.properties index.title=Struts Starter Application index.heading=Hello World! index.message=To get started on your own application …
34
Index.jsp <%-- 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. --%>
36
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
37
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
38
What do we build first? Storyboard Struts Blank It’s about the actions Page 1, Mapping 1
39
It’s about the actions Our one and only extension point HTTP request – GET or POST Action processes request Returns response to browser
40
What do we build first? HTML Storyboard Struts Blank It’s about the actions Page 1, Mapping 1
41
Rename from search.html, result.html Change actions to result.do and search.do Add " /search " and " /result " mappings Click-through – Voila! She works
42
Voila! She works
43
What do we build first? 1) Capture client stories 2) Build storyboard 3) Bring over pages from storyboard 4) Migrate HTML actions to action- mappings
44
Talk Roadmap What are we building it with? What do we build first? What do we build next? Struts: A mile-high view
45
What do we build next? Forms and Tags Action Classes
46
Forms and Tags ActionForm Parameter to JavaBean conversion Validator extension point
47
Forms and Tags SearchForm HTML tags Net result: Roundtrip
48
Forms and Tags SearchForm Input Properties county, facility, permit, beforeDate, afterDate Input Validation Tests Messages
49
SearchForm
50
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; }
51
SearchForm public ActionErrors validate(ActionMapping mapping, HttpServletRequest request) { ActionErrors errors = new ActionErrors(); required(errors,getCountyCode(),"county"); required(errors,getFacilityCode(),"facility"); required(errors,getPermitCode(),"permit"); required(errors,getBdMonth(),"before"); required(errors,getAdMonth(),"after"); return errors; }
52
SearchForm protected void required(ActionErrors errors, String field, String name) { if ((null==field) || (0==field.length())) { errors.add(name, new ActionError("errors.required",name)); } } ApplicationResources.properties {0} is required.
53
HTML Tags Name of County: - select county- Oklahoma Magrathea
54
FormBeans <form-bean name="searchForm" type="app.http.SearchForm" />
55
ActionMapping <action path="/result" forward="/result.html" name="searchForm" scope="request" validate="true" input="/search.do" />
56
Roundtripping If input fails, returns to search.jsp Any selection retained by JSP tags via the SearchForm If input passes, continues to Struts Action or another location (URI) SearchForm is saved in request scope under attribute name “searchForm”
58
What do we build next? Forms and Tags Action Classes
59
Input from ActionForms Output ActionForwards Pass data through context Business logic adapter List searchPermits(String countyCode, String facilityCode, String permitCode, Date before, Date after);
60
Action Classes public class SearchAction extends Action { public ActionForward perform( ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException { SearchForm sf = (SearchForm) form;
61
Action Classes List result = Repository.searchPermits( sf.getCountyCode(), sf.getFacilityCode(), sf.String permitCode, sf.getBefore(), sf.getAfter()); if (null==result) return mapping.findForward("failure"); request.setAttribute(“LIST”,list); return mapping.findForward("success"); }
62
Action Classes Servlet delegate Singletons Thread-safe May be shared by ActionMappings Decorator pattern
63
Struts: A mile high view
72
validation
82
Summary In this talk, we walked through the initial steps most people would take in order to start work on a Struts application. We covered: Selecting a MVC architecture and the web platform Building a Struts application from storyboards and client stories The overall Struts architecture and control flow
83
For more information Tutorials Articles and presentations Books Seminars Mailing List Archives Data Access Systems Presentation Systems Code Generators and GUIs Contributor Taglibs Projects and Examples Struts Special Interest Groups and more. The best resource for finding our more about Struts is still the Resources page on the Struts web site. It is regularly updated and contains links to everything Struts, including
84
Conclusion Make sure that Struts is the right tool for the job Build your application, then adapt it to Struts Struts is your presentation layer, but your application has layers of its own.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.