利用Struts 處理 web程式時 一些容易出錯的問題

來源:互聯網
上載者:User

1、關於 jsp頁面 表單 和 <DIV>  </DIV> 的問題 。

      之前,項目中有一個頁面,因為頁面上的東西很多,所以用了多個<DIV>  </DIV>,
      然後,在我提交表單時,調用的相關指令碼找不到我提交的form。
      原因是 <DIV>  </DIV> 表示一個地區,當form跨越一個或多個div時就會出現問題,
      from表單必須位於div之中

      之前,項目中有一個頁面,因為頁面上的東西很多,所以用了多個<DIV>  </DIV>,
      然後,在我提交表單時,調用的相關指令碼找不到我提交的form。
      原因是 <DIV>  </DIV> 表示一個地區,當form跨越一個或多個div時就會出現問題,
      from表單必須位於div之中

      for example:
 
<html:form action="/infoManagement" target="hiddenFrame" method="post" enctype="multipart/form-data" style="margin:0px;">                 
      <DIV id="EmployeeInfo" style="display:none" >
          <table width="100%" border="0" cellspacing="0" cellpadding="0" style="padding-left:8px;padding-right:8px">
             <tr>
      <td nowrap>..........</td>
         </tr>
   </table>
     </DIV>

//------------------------------

     <DIV id="CenterContent" style="display:none" >
          <table width="100%" border="0" cellpadding="0" cellspacing="0" style="padding-left:8px;padding-right:8px" >
              <tr>
                  <td>..................</td>
              </tr>
          </table>
     </DIV>
</html:form>

 上面這種情況下,表單跨越多個div, 提交表單時,頁面報錯:該form未定義。

  解決方案1: 2個div合為一個 ,如果有頁面顯示效果的需要,可以對2個table進行相應的樣式控制,然後將form放在div內部
  解決方案2:看你需要 哪些提交的內容,如果只需要其中一個div中的內容,則可以將form只放在其中一個div內,
             比如:

          <DIV id="EmployeeInfo" style="display:none" >
          <table width="100%" border="0" cellspacing="0" cellpadding="0" style="padding-left:8px;padding-right:8px">
             <tr>
      <td nowrap>..........</td>
         </tr>
   </table>
     </DIV>

//------------------------------

     <DIV id="CenterContent" style="display:none" >
        <html:form action="/infoManagement" target="hiddenFrame" method="post" enctype="multipart/form-data" style="margin:0px;">                 
          <table width="100%" border="0" cellpadding="0" cellspacing="0" style="padding-left:8px;padding-right:8px" >
              <tr>
                  <td>..................</td>
              </tr>
          </table>
        </html:form>
     </DIV>

2、關於用struts處理戴上傳檔案的表單問題

     在用struts處理上穿表單時,首先一定要在form 中 加 enctype="multipart/form-data" , 例如
     <html:form action="/infoManagement" target="hiddenFrame" method="post" enctype="multipart/form-data" style="margin:0px;">               
     .......
     </html:form>
     並且,在相應的*form.java中 定義對應的FormFile 對象及(get/set 方法)

     另外注意的一個問題:
     <table>
        <tr>
           <td>
                <logic:notEmpty name="infoForm" property="values(employee).values(photo)">
                <bean:define id="photo" name="infoForm" property="values(employee).values(photo)" type="java.lang.String"/>
                <bean:define id="photoNum" name="infoForm" property="values(employee).values(photoNumber)" type="java.lang.String"/>
                <%--
                document.write("photo="+<%=photo%>);
                document.write("src="+<%=request.getContextPath()+photo%>);
                <html:hidden name="infoForm" property="photoNumber" value="<%=photo%>"/>
                --%>
                <html:hidden name="infoForm" property="values(photoLastName)" value="<%=photoNum%>"/>
                <tr>
                    <td height="20" nowrap><div align="left"><strong><bean:message key="stf.infoCenter.photo" bundle="staffing"/>:</strong></div></td>
                </tr>
                <tr>
                    <td nowrap>
                        <div id="img0" style="display:block"><img src="<%=request.getContextPath()+photo%>" width="250"/></div>
                        <div id="img1" style="display:none"><img src="<%=request.getContextPath()+photo%>" /></div>
                    </td>
                </tr>
                <tr>
                    <td align="center">
                            <button class="button" onclick="fnToggle1(this,'img0','img1','<bean:message key="stf.basicInfo.normalSize" bundle="staffing"/>','<bean:message key="stf.basicInfo.scaleSize" bundle="staffing"/>');"><bean:message key="stf.basicInfo.scaleSize" bundle="staffing"/></BUTTON>
                    </td>
                </tr>
                </logic:notEmpty>
           </td>
        </tr>
     </table>

    上面這段代碼中,注意這一行
    [1]:  <html:hidden name="infoForm" property="values(photoLastName)" value="<%=photoNum%>"/>

    也可以這樣寫
    [2]:  <html:hidden name="infoForm" property="photoLastName" value="<%=photoNum%>"/>

    [注意:這裡的values 只是 在form中定義的一個map, 方便進行頁面 和 Action 中的值傳遞]

    但是,由於這段代碼中包含上串檔案,以及 enctype="multipart/form-data" ,這就使得struts 在組裝form時產生了很大的不同
    比如說我的form名字是 InformationForm infoForm = (InformationForm)form; 以後直接用infoForm進行說明
    用方法[1]進行表單提交,那麼,提交後資訊存放在 infoForm[ (HashMapz)values[]   ]中,

    提取參數方法:
    String str = (String)infoForm.getValue("photoLastName");
        if(str != null && str.trim().length() > 0){
      infoForm.setValue("photo",str);
     }

  
    用方法[2]進行表單提交,那麼,提交後資訊卻被存放在了
    infoForm[(CommonsMultipartRequestHandle)multipartRequestHandle[elementsAll 和elementsText  ]  ]中 。

    所有建議作struts Web項目的同事盡量選擇第一種方法,還有就是在沒有上傳附件需要的頁面不要加 enctype="multipart/form-data" ,
    以免出現其他問題時,調試耗費的時間。

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.