struts1.x Series Tutorial (21): Using the Mappingdispatchaction class to invoke different action methods

Source: Internet
Author: User

Unlike Lookupdispatchaction and Dispatchaction, the Mappingdispatchaction class does not specify an action by requesting parameters, but instead corresponds a struts action to an action method. The following example shows how to use the Mappingdispatchaction class to match the struts action with the action method.

Implementation code for the Action class:

package action;
import org.apache.struts.actions.MappingDispatchAction;
……
public class MyMappingDispatchAction extends MappingDispatchAction
{
    public ActionForward pdf(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        //  生成pdf文件
    }
    public ActionForward html(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        //  生成html文件
    }
    public ActionForward unspecified(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        //  处理默认动作
    }
}

The above code has two action methods: PDF and HTML, which are used to generate PDFs and HTML files, respectively. There is also a unspecified method for handling default actions.

We can use the following code to configure the Mymappingdispatchaction class:

<action path="/pdf" type = "action.MyMappingDispatchAction" parameter="pdf" />
<action path="/html" type = "action.MyMappingDispatchAction" parameter="html" />

You can access PDFs and HTML actions by using the following URL, which calls the PDF and HTML methods of the Mymappingdispatchaction class, respectively:

Http://localhost:8080/samples/pdf.do

Http://localhost:8080/samples/html.do

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.