struts1.x Series Tutorial (18): Using the Dispatchaction class to invoke multiple action methods

Source: Internet
Author: User
Tags abstract

When using a struts action, each action needs to write a class and be configured in Struts-config.xml. This is a huge amount of work for a Web program that has a lot of action. For this reason, struts provides the Dispatchaction class, which allows you to invoke an action as a method. Specifies the action to invoke in the Web browser by requesting parameters.

Although the Dispatchaction class is an abstract class, it does not have an abstract method. Therefore, displatchaction subclasses do not implement any of the methods in the Displatchaction class. But if you want to process the action code, you must write the action method based on the appropriate action. An action method is identical to the Execute method except that the method name is different from the Execute method. However, when writing the action method, be aware that the action method name must be consistent with the value of the request parameter used to specify the action (both uppercase and lowercase must be consistent). The following example shows the implementation of the method and action by the Dispatchaction class. In this example, there are three methods: fr, en and unspecificed. Where Fr and en are two action methods that are used to forward the current page to French and English pages, and when the request parameter used to specify the action does not exist, the unspecificed method is called (in this method the current page is forwarded to the Chinese page). The request parameter used in this program to specify the action is language (the reader can specify its own request parameters).

In the <samples Engineering directory >\src\action directory to create a Mydispatchaction.java file, the code is as follows:

 package action; 
Import Javax.servlet.RequestDispatcher;
Import javax.servlet.http.*;
Import org.apache.struts.action.*;
Import org.apache.struts.actions.*;
public class Mydispatchaction extends Dispatchaction
{
//forward to French page
public Actionforward F R (actionmapping mapping, actionform form,
HttpServletRequest request, httpservletresponse response)
{
Try
{
RequestDispatcher rd = Request.getrequestdispatcher ("/newglobal.jsp ? language=fr ");
Rd.forward (request, response);
}
catch (Exception e)
{
}
return null;

//forward to English page
public actionforward en (actionmapping mapping, actionform form,
HttpS Ervletrequest request, HttpServletResponse response)
{
Try
{
Requestdi Spatcher rd = Request.getrequestDispatcher ("/newglobal.jsp?language=en");
Rd.forward (request, response);
}
catch (Exception e)
{
}
return null;

//This method is invoked when LANGUAGE=FR and language=en are not used as Access parameters
Protected Actionforward unspecified (actionmapping Mappi Ng, Actionform form,
HttpServletRequest request, httpservletresponse response)
throws Ex Ception
{
Try
{
//forward to Chinese page
RequestDispatcher Rd = Request.getrequestdispatcher ("/newglobal.jsp?language=zh");
Rd.forward (request, response);
}
catch (Exception e)
{
}
return null;
}
}

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.