利用Struts 处理 web程序时 一些容易出错的问题

来源:互联网 发布:新淘宝店铺怎么装修 编辑:程序博客网 时间:2024/05/17 20:22

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" ,
    以免出现其他问题时,调试耗费的时间。