Apache Struts Dr Jim Briggs Struts 1 What
Apache Struts Dr Jim Briggs Struts 1
What is Struts? • Struts is an open source framework for building Java web applications • Supports MVC/Model 2 • It is an Apache project (struts. apache. org) • Invented by Craig Mc. Clanahan Struts 2
What does Struts do? • Framework assists in building the Controller component of a webapp • Also helps with the View (via some JSP taglibs) • Assumes you can handle the Model! – Usually a set of Java. Beans works best Struts 3
What does a Struts app consist of? • Key classes – Action. Servlet (almost never changed) – Action. Form – Action. Errors – Action. Forward – Action. Mapping • struts-config. xml Struts 4
Action. Form • Represents data on an HTML form • Either implement as: – a Java. Bean that extends org. apache. struts. action. Action. Form – a Dyna. Action. Form where the properties are defined in struts-config. xml • Typically, one Action. Form for each type of HTML form in your application Struts 5
Action • Deals with a specific HTTP request • Extend org. apache. struts. action. Action • Implement your code in method: public Action. Forward execute (Action. Mapping mapping, Action. Form form, Http. Servlet. Request request, Http. Servlet. Response response) throws Exception • Typically one action for each function of your app Struts 6
Action. Errors • Object that you use to store the error messages from your app • Lots of support in the taglibs for outputting error messages – e. g. <html: errors/> tag Struts 7
Action. Forward • • Object returned by an Action Identifies what component is to be executed next Usually a JSP page that displays the response Often an Action will return one of 2 or more Action. Forwards – One for "success" – JSP that displays results – One for "failure" – JSP that displays error messages Struts 8
Action. Mapping • The "junction box" of a Struts webapp • Defines relationship between Action. Forms, Actions and Action. Forwards • Defined in the struts-config. xml file • Easier to change the configuration file than change the code Struts 9
Struts taglibs • Make developing the View (and integrating it with the Controller) easier • Three main libraries: – bean – html – logic • Need appropriate <@taglib> directives Struts 10
Reading • Husted chapters 1 -3 (tutorial and background) • Husted chapters 4 -10 (reference) • Basham chapter 14 • struts-documentation. war Struts 11
- Slides: 11