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

与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 方法:pdf html ,分别用来生成pdf html 文件。还有一个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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值