Optimizing Struts applications with Actionforward

Source: Internet
Author: User

From within a servlet, you can direct the control process to a destination resource by using the forward method of the Javax.servlet.RequestDispatcher class. In the action class of the login application, the code is in the following form:

RequestDispatcher rd = request.getrequestdispatcher (destination);

Rd.forward (request, response);

Where destination is the path to a destination resource.

But in a typical struts application, you can use the Actionforward class as an alternative. The advantage of using this class is that you no longer need to create a RequestDispatcher object and invoke its forward method.

You can use the Actionforward class in the Execute method of an action class. Note that one of the overloaded Execute methods has the following definition, which returns a Actionforward object:

Public Actionforward Execute (

Actionmapping Mapping,

Actionform form, HttpServletRequest request,

HttpServletResponse response)

Throws Exception

Because we hadn't talked about the Actionforward class at the time, the Execute method for all the action classes in the first and second parts of the series returned only null values. Now, in the Execute method of an action class, you can replace the following RequestDispatcher object instance with the Actionforward class:

RequestDispatcher rd = request.getrequestdispatcher (destination);

Rd.forward (request, response);

The new code becomes: Return (new Actionforward (destination));

Building Actionforward Objects

The Actionforward class provides the following five types of constructors:

Public Actionforward ()

Public Actionforward (String path)

Public Actionforward (String path, Boolean redirect)

Public Actionforward (string name, String path, Boolean redirect)

Public Actionforward (string name, String path, Boolean redirect, Boolean contextrelative)

Although these constructors are not required, we should be aware of the following points. In these constructors, the second is probably the most common. The path parameters in the four constructors represent the paths to the destination resource. The redirect Boolean value in the last three constructors indicates whether a redirect (redirect) has been performed. (This value is set to False by default because redirect is slower than forward.) Finally, the Contextrelative Boolean value in the fifth constructor indicates whether the path should be context-relative, not module-relative.

Similarly, a Actionforward instance can have a logical name that you can use to find an instance associated with a particular Actionmapping object. (See part IV of this series about actionmapping.) )

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.