本文為原創(chuàng),如需轉(zhuǎn)載,請(qǐng)注明作者和出處,謝謝!
上一篇:
Struts1.x系列教程(20):使用EventDispatchAction類處理一個(gè)form多個(gè)submit
與LookupDispatchAction、DispatchAction不同,MappingDispatchAction類并不通過請(qǐng)求參數(shù)來指定動(dòng)作,而是將一個(gè)Struts動(dòng)作對(duì)應(yīng)于一個(gè)Action方法。下面的例子演示了如何使用MappingDispatchAction類來將Struts動(dòng)作和Action方法相對(duì)應(yīng)。
Action類的實(shí)現(xiàn)代碼:
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
{
// 處理默認(rèn)動(dòng)作
}
}
上面的代碼有兩個(gè)Action方法:pdf和html,分別用來生成pdf和html文件。還有一個(gè)unspecified方法用來處理默認(rèn)動(dòng)作。
我們可以使用如下的代碼來配置MyMappingDispatchAction類:
<action path="/pdf" type = "action.MyMappingDispatchAction" parameter="pdf" />
<action path="/html" type = "action.MyMappingDispatchAction" parameter="html" />
可以通過如下的URL來訪問pdf和html動(dòng)作,分別會(huì)調(diào)用MyMappingDispatchAction類的pdf和html方法:
http://localhost:8080/samples/pdf.do
http://localhost:8080/samples/html.do
下一篇:Struts1.x系列教程(22):Tiles框架簡介
新浪微博:http://t.sina.com.cn/androidguy 昵稱:李寧_Lining