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