Struts Learning Note3 --- Action

来源:互联网 发布:如何用域名做邮箱 编辑:程序博客网 时间:2024/06/09 21:05

To develop your own Action class, you must complete the following steps. These steps describe the minimum actions that you must take when creating a new Action and are the focus of this section:

  1.Create a class that extends the org.apache.struts.action.Action class.

  2.Implement the appropriate execute() method and add your specific business logic.

  3.Compile the new Action and move it into the Web application's classpath. This would most often be your WEB-INF/classes directory.

  4.Add an <action> element to the application's struts-config.xml file describing the new Action.

Implementing the execute() Method :

    The execute() method has two functions:

Component

Description

ActionMapping

Contains all of the deployment information for a particular Action bean. This class is to determine where the results of the LoginAction will be sent once its processing is complete.

ActionForm

Represents the Form inputs containing the request parameters from the View referencing this Action bean. The reference being passed to our LoginAction points to an instance of our LoginForm.

HttpServletRequest

A reference to the current HTTP request object.

HttpServletResponse

A reference to the current HTTP response object.

As you build your own Action objects, you will notice that they almost all perform the same sequence of events.

  1.Cast the ActionForm reference associated with this Action to your specific ActionForm implementation. In the previous example, we are casting the passed-in ActionForm object to a SkeletonForm.

  2.Add your specific business logic.

  3.Use the ActionMapping.findForward() method to find the ActionForward object that matches the <forward> sub-element in the <action> definition. We look at the <forward> sub-element in the following section.

  4.Return the retrieved ActionForward object. This object routes the request to the appropriate View.

Configuring the Action Class :

Attribute

Description

attribute

Names a request or session scope attribute that is used to access an Action-Form bean, if it is other than the bean's specified "name".

className

Names the fully qualified class name of the ActionMapping implementation class you want to use in when invoking this Action class. If the className attribute is not included, the ActionMapping defined in the ActionServlet's mapping initialization parameter is used.

forward

Represents a Module-relative path of the servlet or JSP resource that will process this request. This attribute is used if you do not want an Action to service the request to this path. The forward attribute is valid only if no include or type attribute is specified.

include

Represents a Module-relative path of the servlet or JSP resource that will process this request. This attribute is used if you do not want an Action to service the request to this path. The include attribute is valid only if no forward or type attribute is specified.

input

Represents a Module-relative path of the input form to which control should be returned if a validation error is encountered. The input attribute is where control will be returned if ActionErrors are returned from the ActionForm or Action objects. (Optional)

name

Identifies the name of the form bean that is coupled with the Action being defined.

path

Represents the Module-relative path of the submitted request. The path must be unique and start with a / character.

parameter

A generic configuration parameter that is used to pass extra information to the Action object defined by this action mapping.

roles

A comma-delimited list of security roles that can access the defined <action /> object.

type

Names the fully qualified class name of the Action class being described by this ActionMapping. The type attribute is valid only if no include or forward attribute is specified.

scope

Names the scope of the form bean that is bound to the described Action. The possible values are request or session. The default value is session.

unknown

If set to true, this <action /> is used as the default action mapping when a request does not match another <action /> element. Only one ActionMapping can be marked as unknown per Module.

validate

If set to true, causes the ActionForm.validate() method to be called on the form bean associated to the Action being described. If the validate attribute is set to false, then the ActionForm.validate() method is not called. The default value is true.

 

A sample <action> sub-element using some of the previous attributes is shown here:

<action-mappings>  <action path="/Lookup"    type="ch04.LookupAction"    name="lookupForm"    input="/index.jsp">    <forward name="success" path="/quote.jsp"/>    <forward name="failure" path="/index.jsp"/>  </action></action-mappings>

  • It performs the user-defined business logic associated with your application.

  • It tells the Framework where it should next route the request.

The Parameters of the Action.execute() Method  :