Struts Basics ============= The Theory JSP Pages and servlets are used together to deploy web applications that conform to MVC (Model-View-Controller) design pattern. The terms Model 2 (Model 1 meaning JSP only) and MVC are used interchangably now. Struts 1.0 was released 7/2001. So let's hope it's stable. MVC Paradigm (again) Application flow is mediated by the Controller in his case HTTP requests to an appropriate handler (Model). The Model should encapsulate the application's business logic or state. Control is then forwarded back through the Controller to the appropriate View. Controller -> Handler (Model) -> View (back to the user) The forwarding can be determined by consulting a set of mappings, usually loaded from a database or configuration file. This provides loose coupling between the View and the Model. This hopefully will make the app easier to create and maintain. The Model System State and Business Logic JavaBeans --------------------------------------------------- The model portion of MVC can be divided into two major parts: Internal State - Nouns (things) Actions - Verbs (change the stage of those things) Many applications represent the internal system of the system as a set of one or more JavaBeans. The bean properties represent the details of the system state. Struts strongly reccomends that you separate the business logic ("how it's done") from the role that "Action" classes play ("what to do"). The View - JSP Pages and Presentation Components ------------------------------------------------ View of MVC is done with JSP pages which can contain static HTML or XML text called template text, plus the ability to insert dynamic content based on the interpretation (at page request time) of special action tags. Struts includes a set of custom tag libraries that facilitate creating user interfaces that are fully internationalized, and that interact gracefully with "ActionForm" beans that are part of the Model portion of the system. The Controller: Action Servlet and ActionMapping ------------------------------------------------ Primarmy component of the Controller is a servlet of class "ActionServlet". This servlet is configured by defining a set of "ActionMappings". An ActionMapping defines a "path" that is matched against the request URI of the incoming request, and usually specifies the fully qualified classname of an "Action" class. All Actions are subclassed from org.apache.struts.action.Action Action encapsulate the business logic, interpret the outcome, and ultimately dispatch control to the appropriate View component to create the response. Struts also supports the ability to use ActionMapping classes that have additional properties beyond the standard ones required to operate the framework. This allows you to store additional information specific to your application, but still utilize the remaining features of the framework. In addition Struts lets you define logical "names" to which control should be forwarded so that an action method can ask for the "Main Menu" page (for example) without knowing what the actual name of the corresponding JSP page is. These features greatly assist you in separating the control logic (what to do) with the view logic (how it looks). Struts Control Flow ------------------- Control layer of a MVC style app in Struts includes a contrller servlet, developer-defined request handlers, and several supporting objects View layer of the MVC app is supported by struts with its own custom tag libraries. Some of these access the control-layer objects. Others are generic tags found convenient when writing applicaitons. Other taglibs including JSTL can be used with struts. Velocity Templates and XSLT can also be used with Struts. Model Layer in a MVC application is often project specific. Struts is designed to make it easy to access the biz end of your app but leaves that part of the programming to other products like JDBC, Enterprise Java Beans, Object Relational Bridge, or Simper to name a few. Ok let's see how this all fits together. Upon initialization, the controller parses a configuration file(struts-config.xml) and uses it to deploy other control layer objects. Together these objects form the Struts Configuration. The Struts Configuration defines the "ActionMappings" (org.apache.struts.action.ActionMappings) for an app. The struts controller servlet consults the ActionMappings as it routes HTTP requests to other components in the framework. Requests may be forwarded to JavaServerPages or Action subclasses provided by the Struts developer. Often a request is first forwared to an action then to a JSP. The mappings help the controller turn HTPP requests into application actions: Request --> Action --> JSP page An individual ActionMapping (org.apache.struts.action.ActionMapping) will usually contain a number of properties including: * a request path (URI) * the object type (Action subclass) * other prperties as needed. The action object can handle the request and respond to the client or indicate that control should be forwarded elsewhere. For example if a login succeeds, a login action may wish to forward the request onto the mainMenu page. Action objects have access to the application's controller's servlet, and so have access to that servlets methods. When forwarding control, an Action object can indirectly forward one or more shared objects, including JavaBeans.