MappingDispatchAction类应用

MappingDispatchAction类可以直接URL来调用该类中的方法。如http://localhost:8080/samples/pdf.do

MappingDispatchAction类在struts-extras-1.3.10.jar中需要将struts-extras-1.3.10.jar放入
C:/Program Files/Java/jdk1.5.0_12/jre/lib/ext

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

    Action类的实现代码:

package cn.hxex.tutorial;
import org.apache.struts.actions.MappingtDispatchAction;
import javax.servlet.*;
import javax.servlet.http.*;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import java.util.*;

public class MyMappingDispatchAction extends MappingDispatchAction
{
    public ActionForward test1(ActionMapping mapping, ActionForm form,
        HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException
    {
        // 定义一个名为pdf方法
    }
    public ActionForward test2(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
    {
        // 处理默认动作,如果没有处理直接返回一个null的ActionForward
    }
}


    上面的代码有两个Action方法:test1和test2。还有一个unspecified方法用来处理默认动作。

    我们可以使用如下的代码来配置MyMappingDispatchAction类:

<action path="/pdf" type = "cn.hxex.tutorial.MyMappingDispatchAction" parameter="test1" />
<action path="/html" type = "cn.hxex.tutorial.MyMappingDispatchAction" parameter="test2" />


    可以通过如下的URL来访问test1和test2方法,分别会调用MyMappingDispatchAction类的test1和test2方法:

    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、付费专栏及课程。

余额充值