Dear Wiki user, You have subscribed to a wiki page or wiki category on "Struts Wiki" for change notification.
The following page has been changed by MichaelJouravlev: http://wiki.apache.org/struts/StrutsManualActionClasses New page: '''Attention: this page describes functionality that is not yet available in GA Struts release''' == Action Classes == The goal of an Action class is to process a request, via its execute method, and return an ActionForward object that identifies where control should be forwarded (e.g. a JSP, Tile definition, Velocity template, or another Action) to provide the appropriate response. == Action As A Servlet == In most Struts-based applications an Action class handles requests of one particular type. Considering that Action class is stateless, it can be thought of as a web service or just a fancy servlet. Action class defines two methods that could be executed in "servlet mode". Choosing one or another depends on your servlet environment: {{{public ActionForward execute(ActionMapping mapping, ActionForm form, ServletRequest request, ServletResponse response) throws Exception; public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception;}}} Since the majority of teams using the framework are focused on building web applications, most projects will only use the "HttpServletRequest" version. A non-HTTP execute() method has been provided for applications that are not specifically geared towards the HTTP protocol. In the MVC/Model 2 design pattern, a typical Action class will often implement logic like the following in its execute method: * Validate the current state of the user's session (for example, checking that the user has successfully logged on). If the Action class finds that no logon exists, the request can be forwarded to the presentation page that displays the username and password prompts for logging on. This could occur because a user tried to enter an application "in the middle" (say, from a bookmark), or because the session has timed out, and the servlet container created a new one. * If validation is not complete, validate the form bean properties as needed. If a problem is found, store the appropriate error message keys as a request attribute, and forward control back to the input form so that the errors can be corrected. * Perform the processing required to deal with this request (such as saving a row into a database). This can be done by logic code embedded within the Action class itself, but should generally be performed by calling an appropriate method of a business logic bean. * Update the server-side objects that will be used to create the next page of the user interface (typically request scope or session scope beans, depending on how long you need to keep these items available). * Return an appropriate ActionForward object that identifies the presentation page to be used to generate this response, based on the newly updated beans. Typically, you will acquire a reference to such an object by calling findForward on either the ActionMapping object you received (if you are using a logical name local to this mapping), or on the controller servlet itself (if you are using a logical name global to the application). In Apache Struts 1.0, Actions called a perform method instead of the now-preferred execute method. These methods use the same parameters and differ only in which exceptions they throw. The elder perform method throws SerlvetException and IOException. The new execute method simply throws Exception. The change was to facilitate the Declarative Exception handling feature introduced in Apache Struts 1.1. The perform method may still be used in Apache Struts 1.1 but is deprecated. The Apache Struts 1.1 method simply calls the new execute method and wraps any Exception thrown as a ServletException. The deprecated perform method was removed in Apache Struts 1.2. == Action As A Code-Behind Class == In Microsoft parlance, a code-behind class implements the program logic of one web resource. Since ASP.Net uses Page Controller paradigm, a web resource is usually a page. Struts web resources are not constrained by one page, one resource can have several corresponding views defined in one or in several JSP files. A code-behind Struts Action: * handles different commands and events corresponding to a web resource (submit phase), and * selects an appropriate view based on current state of web resource (render phase). It is possible to combine processing of both submit and render phases in one Action class, or to split this functionality into two Action classes. Instead of relying on one execute() method a code-behind class uses event handlers for different incoming events. Struts Extras package defines several subclasses of Action class that implement event dispatching: DispatchAction, LookupDispatchAction, MappingDispatchAction, EventDispatchAction. Events and corresponding handlers are usually defined in an action mapping, but the exact way of doing that depended on specific Action subclass that you used. Starting from Struts 1.3.6 dispatching functionality is pushed into base Action class, and now there is a standard way of defining events for a code-behind Action by using <event> elements in an action mapping: {{{<action path = "/login" type = "samples.login.LoginAction" name = "loginform" scope = "session" validate = "false"> <event name="loginEvent" handler="login"/> <event name="logoutEvent" handler="logout"/> <forward name = "notloggedin" path = "/logint.jsp" /> <forward name = "loggedin" path = "/logout.jsp" /> </action>}}} == Action As A Web Component == A web component differs from a web resource in that a component is visually a part of a larger ''composite page''. As such a web component does not need to navigate to a next location. Instead, a web component must either update itself on a composite page in place or reload the whole page after the component updated its state. In-place updating is facilitated using Javascript/XMLHttpRequest (Ajax mode), while full page reload is used if Javascript is turned off on a browser. Struts 1.3.6 allows to create web components in Struts using Struts Action class for event processing, JSP for presentation and an optional Javascript helper for in-place update in Ajax mode. See StrutsComponents section.