Sun
          是選擇你愛的人還是愛你的人?
          posts - 8,comments - 14,trackbacks - 0

          今天有人問前臺表單form動態生成,后臺使用DynaActionForm怎么用,說了他沒懂,就寫了個例子。

          Action配置

          <action
                attribute
          ="testMappingForm"
                input
          ="/jsp"
                name
          ="testMappingForm"
                path
          ="/testMapping"
                scope
          ="request"
                type
          ="com.modo.struts.action.TestMappingAction" />


          ActionForm配置

          <form-bean name="testMappingForm" type="org.apache.struts.action.DynaActionForm">
                  
          <form-property name="fnames" type="java.util.HashMap"></form-property>
              
          </form-bean>

          請注意name屬性,這個屬性后面要用到。


          然后是頁面index.jsp,這里只模擬動態

          <html:form action="/testMapping.do" method="post">
             
          <%
             
          for(int i=0;i<5;i++){   
             
          %>
                     
          <html:text property="<%="fnames(name_"+i+")"%>" value="<%="gangye_"+i%>"></html:text><br>
             
          <%}%>
             
          <br>
             
          <html:submit value="Submit Form" />
             
          </html:form>

          請注意html:text的property標簽。


          index.jsp效果如下

          后臺Action

          public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request,
                      HttpServletResponse response) {
                  HashMap   hm
          =(HashMap)((DynaActionForm)form).get("fnames");
                  Iterator it 
          = hm.entrySet().iterator();
                  Map.Entry entry 
          = null;
                  
          while(it.hasNext()){
                      entry  
          = (Map.Entry)it.next();
                      System.out.println(entry.getKey() 
          + " = " + entry.getValue());
                  }
                  
          return null;
              }


          輸出表單項

          name_4 = gangye_4
          name_0 
          = gangye_0
          name_2 
          = gangye_2
          name_1 
          = gangye_1
          name_3 
          = gangye_3



           



           

          posted on 2010-04-20 15:56 .Sun 閱讀(2337) 評論(5)  編輯  收藏 所屬分類: 隨筆

          FeedBack:
          # re: 關于struts動態form_DynaActionForm
          2010-05-18 09:18 | iceblooded
          java.lang.ClassCastException: com.modo.struts.action.TestMappingAction  回復  更多評論
            
          # re: 關于struts動態form_DynaActionForm
          2011-02-24 20:29 | 蔣承洋
          謝謝你,我學到知識了...  回復  更多評論
            
          # re: 關于struts動態form_DynaActionForm
          2011-05-04 16:30 | 盧煜
          證實有效!  回復  更多評論
            
          # re: 關于struts動態form_DynaActionForm
          2011-05-04 16:33 | 盧煜
          牛人,我在這個問題上糾結了一天,試過很多方法,最終問題在你這得到解決。
          小弟感激不禁!  回復  更多評論
            
          # re: 關于struts動態form_DynaActionForm[未登錄]
          2014-04-23 04:00 | lj
          請教下牛人:
          <form-property name="fnames" type="java.util.ArrayList"></form-property>

          在JSP輸入頁面如何取得這個fnames,  回復  更多評論
            
          主站蜘蛛池模板: 邵武市| 彭州市| 萨嘎县| 辛集市| 鹤庆县| 庆元县| 大英县| 蛟河市| 富顺县| 安宁市| 保山市| 乌鲁木齐市| 随州市| 赣州市| 关岭| 汉阴县| 奉节县| 万源市| 全南县| 长兴县| 集安市| 酉阳| 汝城县| 双鸭山市| 明星| 万载县| 英吉沙县| 竹山县| 谢通门县| 邳州市| 蕲春县| 措美县| 溧水县| 资溪县| 泗水县| 盐源县| 万山特区| 乌苏市| 定西市| 涞水县| 固安县|