与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 { // 处理默认动作 } } |
上面的代码有两个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
本文介绍如何使用 Struts 的 MappingDispatchAction 类将不同的请求映射到对应的 Action 方法上,包括具体的实现代码及配置示例。
:使用MappingDispatchAction类调用不同的Action方法&spm=1001.2101.3001.5002&articleId=6755879&d=1&t=3&u=ffbac6dadcc7490796950a6301a19739)
1001

被折叠的 条评论
为什么被折叠?



