随笔 - 312, 文章 - 14, 评论 - 1393, 引用 - 0
数据加载中……

Struts1.x系列教程(21):使用MappingDispatchAction类调用不同的Action方法

本文为原创,如需转载,请注明作者和出处,谢谢!

上一篇:Struts1.x系列教程(20):使用EventDispatchAction类处理一个form多个submit   

    与LookupDispatchAction、DispatchAction不同,MappingDispatchAction类并不通过请求参数来指定动作,而是将一个Struts动作对应于一个Action方法。下面的例子演示了如何使用MappingDispatchAction类来将Struts动作和Action方法相对应。

Action类的实现代码:

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 
    {
       
//  处理默认动作
    }
}

上面的代码有两个Action方法:pdfhtml,分别用来生成pdfhtml文件。还有一个unspecified方法用来处理默认动作。
    我们可以使用如下的代码来配置MyMappingDispatchAction类:

 

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

     可以通过如下的URL来访问pdf和html动作,分别会调用MyMappingDispatchAction类的pdf和html方法:

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

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


下一篇:Struts1.x系列教程(22):Tiles框架简介





Android开发完全讲义(第2版)(本书版权已输出到台湾)

http://product.dangdang.com/product.aspx?product_id=22741502



Android高薪之路:Android程序员面试宝典 http://book.360buy.com/10970314.html


新浪微博:http://t.sina.com.cn/androidguy   昵称:李宁_Lining

posted on 2009-02-24 19:55 银河使者 阅读(2864) 评论(0)  编辑  收藏 所属分类: javaweb 原创struts1.x


只有注册用户登录后才能发表评论。


网站导航: