Você está na página 1de 5

Action Class

public class Action extends java.lang.Object An Action is an adapter between the contents of an incoming HTTP request and the corresponding business logic that should be executed to process this request. The controller ( equestProcessor! will select an appropriate Action for each request" create an instance (if necessar#!" and call the execute method. Actions must be programmed in a thread$safe manner" because the controller will share the same instance for multiple simultaneous requests. This means #ou should design with the following items in mind% &nstance and static variables '()T *OT be used to store information related to the state of a particular request. The# 'A+ be used to share global resources across requests for the same action. Access to other resources (,ava-eans" session variables" etc.! '()T be s#nchroni.ed if those resources require protection. (/enerall#" however" resource classes should be designed to provide their own protection where necessar#. 0hen an Action instance is first created" the controller will call setServlet with a non$null argument to identif# the servlet instance to which this Action is attached. 0hen the servlet is to be shut down (or restarted!" the setServlet method will be called with a null argument" which can be used to clean up an# allocated resources in use b# this Action.

Methods:
execute
public ActionForward execute(ActionMapping mapping, ActionForm form, javax.servlet.http.HttpServlet e!uest re!uest, javax.servlet.http.HttpServlet esponse response" throws java.lang.#xception

Process the specified HTTP request" and create the corresponding HTTP response (or forward to another web component that will create it!" with provision for handling exceptions thrown b# the business logic. eturn an ActionForward instance describing where and how control should be forwarded" or null if the response has alread# been completed. Parameters: mapping $ The Action'apping used to select this instance form $ The optional Action1orm bean for this request (if an#! re!uest $ The HTTP request we are processing response $ The HTTP response we are creating Throws: java.lang.#xception $ if the application business logic throws an exception addMessages

protected void addMessages(javax.servlet.http.HttpServlet e!uest re!uest, ActionMessages messages"

Adds the specified messages 2e#s into the appropriate request attribute for use b# the 3html%messages4 tag (if messages56true6 is set!" if an# messages are required. &nitiali.e the attribute if it has not alread# been. Otherwise" ensure that the request attribute is not set. Parameters: re!uest $ The servlet request we are processing messages $ 'essages object Since: )truts 7.8.7 addErrors
protected void addErrors(javax.servlet.http.HttpServlet e!uest re!uest, ActionMessages errors"

Adds the specified errors 2e#s into the appropriate request attribute for use b# the 3html%errors4 tag" if an# messages are required. &nitiali.e the attribute if it has not alread# been. Otherwise" ensure that the request attribute is not set. Parameters: re!uest $ The servlet request we are processing errors $ 9rrors object Since: )truts 7.8.7

Action Config
public class ActionConfig extends java.lang.Object implements java.io.)eriali.able A ,ava-ean representing the configuration information of an configuration file.

$action%

element from a )truts module

Class ActionForm
public abstract class ActionForm extends java.lang.Object implements java.io.)eriali.able An ActionForm is a ,ava-ean optionall# associated with one or more ActionMappings. )uch a bean will have had its properties initiali.ed from the corresponding request parameters before the correspondingAction.execute method is called. 0hen the properties of this bean have been populated" but before the execute method of the Action is called" this bean:s validate method will be called" which gives the bean a chance to verif# that the properties submitted b# the user are correct and valid. &f this method finds problems" it returns an

error messages object that encapsulates those problems" and the controller servlet will return control to the corresponding input form. Otherwise" the validate method returns null" indicating that ever#thing is acceptable and the corresponding Action.execute method should be called. This class must be subclassed in order to be instantiated. )ubclasses should provide propert# getter and setter methods for all of the bean properties the# wish to expose" plus override an# of the public or protected methods for which the# wish to provide modified functionalit#. -ecause Action1orms are ,ava-eans" subclasses should also implement Seriali&able" as required b# the ,ava-ean specification. )ome containers require that an object meet all ,ava-ean requirements in order to use the introspection AP& upon which Action1orms rel#. reset
public void reset(ActionMapping mapping, javax.servlet.http.HttpServlet e!uest re!uest"

eset bean properties to their default state" as needed. This method is called before the properties are repopulated b# the controller. The default implementation does nothing. &n practice" the onl# properties that need to be reset are those which represent chec2boxes on a session$scoped form. Otherwise" properties can be given initial values where the field is declared. &f the form is stored in session$scope so that values can be collected over multiple requests (a 6wi.ard6!" #ou must be ver# careful of which properties" if an#" are reset. As mentioned" session$scope chec2boxes must be reset to false for an# page where this propert# is set. This is because the client does not submit a chec2box value when it is clear (false!. &f a session$ scoped chec2box is not proactivel# reset" it can never be set to false. This method is not the appropriate place to initiali.e form value for an 6update6 t#pe page (this should be done in a setup Action!. +ou mainl# need to worr# about setting chec2box values to false; most of the time #ou can leave this method unimplemented. Parameters: mapping $ The mapping used to select this instance re!uest $ The servlet request we are processing validate
public Action#rrors validate(ActionMapping mapping, javax.servlet.http.HttpServlet e!uest re!uest"

<alidate the properties that have been set for this HTTP request" and return an Action#rrors object that encapsulates an# validation errors that have been found. &f no errors are found" return null or anAction#rrors object with no recorded error messages. The default implementation performs no validation and returns null. )ubclasses must override this method to provide an# validation the# wish to perform. Parameters: mapping $ The mapping used to select this instance re!uest $ The servlet request we are processing

Class ActionMapping

public class ActionMapping extends Action=onfig An ActionMapping represents the information that the controller" e!uest'rocessor" 2nows about the mapping of a particular request to an instance of a particular Action class. The ActionMapping instance used to select a particular Action is passed on to that Action" thereb# providing access to an# custom configuration information included with the ActionMapping object. )ince )truts 7.7 this class extends Action(onfig. N TE $ This class would have been deprecated and replaced b# org.apache.struts.config.Action(onfig except for the fact that it is part of the public AP& that existing applications are using. findForward
public ActionForward findForward(java.lang.String name" 1ind and return the Forward(onfig instance defining

how forwarding to the specified logical name should be handled. This is performed b# chec2ing local and then global configurations for the specified forwarding configuration. &f no forwarding configuration can be found" return null. Parameters: name $ >ogical name of the forwarding instance to be returned

findForwards
public java.lang.String)* findForwards("

eturn the logical names of all locall# defined forwards for this mapping. &f there are no such forwards" a .ero$length arra# is returned.

Class ForwardConfig
public class ForwardConfig extends java.lang.Object implements java.io.)eriali.able A ,ava-ean representing the configuration information of a $forward% element from a )truts configuration file.

Class Message!esources
public abstract class Message!esources extends java.lang.Object implements java.io.)eriali.able /eneral purpose abstract class that describes an AP& for retrieving >ocale$sensitive messages from underl#ing resource locations of an unspecified design" and optionall# utili.ing the MessageFormat class to produce internationali.ed messages with parametric replacement. =alls to getMessage(" variants without a +ocale argument are presumed to be requesting a message string in the default +ocale for this ,<'.

=alls to getMessage(" with an un2nown 2e#" or an un2nown +ocale will return null if the return,ull propert# is set to true. Otherwise" a suitable error message will be returned instead. "MP#EMENTAT" N N TE $ =lasses that extend this class must be )eriali.able so that instances ma# be used in distributable application server environments.

"nterface ModuleConfig
public interface ModuleConfig The collection of static configuration information that describes a )truts$based module. 'ultiple modules are identified b# a prefix at the beginning of the context relative portion of the request ( &. &f no module prefix can be matched" the default configuration (with a prefix equal to a .ero$length string! is selected" which is elegantl# bac2wards compatible with the previous )truts behavior that onl# supported one module.

Você também pode gostar