与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
{
// 处理默认动作
}
}
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类:
我们可以使用如下的代码来配置MyMappingDispatchAction类:
<
action
path
="/pdf"
type
= "action.MyMappingDispatchAction"
parameter
="pdf"
/>
< action path ="/html" type = "action.MyMappingDispatchAction" parameter ="html" />
< 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
http://localhost:8080/samples/pdf.do
http://localhost:8080/samples/html.do