struts1 一次提交多个ActionForm

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即可。 

<action-mappings> 
   <action path="/MyAction" type="MyAction" name="MyForm1" /> 
</action-mappings> 

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没有继承关系。 

猜你喜欢

转载自bigdragon.iteye.com/blog/2256219
今日推荐