您的位置:首页 > 移动开发

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

2013-12-20 21:11 951 查看
与LookupDispatchAction、DispatchAction不同,MappingDispatchAction类并不通过请求参数来指定动作,而是将一个Struts动作对应于一个Action方法。下面的例子演示了如何使用MappingDispatchAction类来将Struts动作和Action方法相对应。
Action类的实现代码:
package action;
import org.apache.struts.actions.MappingDispatchAction;
publicclass 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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: