---
title: "struts1 一次提交多个ActionForm"
author: "wangyanlong0107"
date: "2015-11-11T17:10:58Z"
tags: []
categories: ["企业架构"]
slug: "struts1-actionform"
draft: false
lastmod: "2015-11-11T17:10:58Z"
id: 2256219
url: "https://bigdragon.iteye.com/blog/2256219"
---

JSP中有两个**actionform**放在同一个form中,分别对应两个数据库表，现在用的是struts1,如何实现在同一个form中提交，交给后台的action处理?

 

struts1不支持一个页面多个actionform，想复用form就继承。 但是不能复用另外的actoin，只能用html写个表单提交到那个写好的action。

 

1)   
如果只提交一次form，然后调用对应的action，然后在action里面进一步把form中的值取出来，然后插入或者更新到两张数据库表中的话，可以采用下面的方式。 

复用已有的两个form bean，让其中的一个form bean继承自另一个form bean。比如： 

public class MyForm2 extends ActionForm{ 

} 

public class MyForm1 extends MyForm2{ 

} 

然后在struts-config.xml里面只配置MyForm1即可。 

&lt;action-mappings&gt;   
   &lt;action path="/MyAction" type="MyAction" name="MyForm1" /&gt;   
&lt;/action-mappings&gt; 

public class MyAction extends Action {   
    public ActionForward execute(   
      ActionMapping mapping,ActionForm form,   
      HttpServletRequest request, HttpServletResponse response)    throws Exception {   
                 MyForm1 myForm = (MyForm1) form;   
            }   
} 

最好还是单独定义一个form bean,里面包含所有的form项目，这样比较好理解，毕竟MyForm1和MyForm2没有继承关系。