Unlike Lookupdispatchaction and Dispatchaction, the Mappingdispatchaction class does not specify an action by requesting parameters, but instead corresponds a struts action to an action method. The following example shows how to use the Mappingdispatchaction class to match the struts action with the action method.
Implementation code for the Action class:
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
{
// 处理默认动作
}
}
The above code has two action methods: PDF and HTML, which are used to generate PDFs and HTML files, respectively. There is also a unspecified method for handling default actions.
We can use the following code to configure the Mymappingdispatchaction class:
<action path="/pdf" type = "action.MyMappingDispatchAction" parameter="pdf" />
<action path="/html" type = "action.MyMappingDispatchAction" parameter="html" />
You can access PDFs and HTML actions by using the following URL, which calls the PDF and HTML methods of the Mymappingdispatchaction class, respectively:
Http://localhost:8080/samples/pdf.do
Http://localhost:8080/samples/html.do