2012-11-05 82 views
1

使用AJAX我用AJAX驗證驗證以下驗證在Struts2

<tr> 
     <td width="20" style='color: red'> 
     <img src="images/icon-star.png" width="16" height="16" /> 
     </td> 
     <td id="lblCustomBillNo" style="width: 15%" class="searchCriteriaCellLbl"> 
     The custom Bill Number 
     </td> 
     <td width="5" class="searchCriteriaCellLbl">:</td> 
     <td class="searchCriteriaCellVal"> 
      <s:textfield id="customBillNo" name="customBillNo" size="20" maxlength="24" style="width: 200px" /> 
     </td> 
     <td class="errorFlag" style="color: red" valign="middle"> 
      <s:fielderror fieldName="customBillNo" /> 
     </td> 
    </tr> 
    <tr> 
     <td width="20" style='color: red'> 
     <img src="images/icon-star.png" width="16" height="16" /> 
     </td> 
     <td id="lblBillNo" style="width: 15%" class="searchCriteriaCellLbl"> 
     <s:property value="%{getText('billNo')}" /> 
     </td> 
     <td width="5" class="searchCriteriaCellLbl">: 
     </td> 
     <td class="searchCriteriaCellVal"> 
      <s:textfield label="billNo" id="billNo" name="billNo" size="20" maxlength="24" style="width: 200px" /> 
     </td> 
     <td class="errorFlag" style="color: red" valign="middle"> 
      <s:fielderror fieldName="billNo" /> 
     </td> 
    </tr> 
    <tr> 
     <td width="20" style='color: red'> 
     <img src="images/icon-star.png" width="16" height="16" /> 
     </td> 
    <td id="lblCarrierNo" style="width: 15%" class="searchCriteriaCellLbl"> 
     The carrier Number 
    </td> 
    <td width="5" class="searchCriteriaCellLbl">: 
    </td> 
    <td class="searchCriteriaCellVal"> 
     <s:textfield label="carrierNo" id="carrierNo" name="carrierNo" size="20" maxlength="24" style="width: 200px" /> 
    </td> 
    <td class="errorFlag" style="color: red" valign="middle"> 
     <s:fielderror fieldName="carrierNo" /> 
    </td> 
</tr> 

我用下面的國際化中如果看不到的i18n文件

errors.required=${getText(fieldName)} requireddd 

錯誤,這個驗證文件

<validators>  
    <field name="customBillNo"> 
     <field-validator type="requiredstring" short-circuit="true"> 
      <param name="trim">true</param> 
      <message key="errors.required" /> 
     </field-validator> 
    </field> 
    <field name="billNo"> 
     <field-validator type="required" short-circuit="true"> 
     <message key="errors.required" /> 
     </field-validator> 
    </field> 
    <field name="carrierNo"> 
     <field-validator type="required" short-circuit="true"> 
      <message key="errors.required" /> 
     </field-validator> 
    </field> 
    </validators> 

和我把這個JavaScript使用ajax驗證

 function validate(){ 

      //document.all.loading.style.display = 'block'; 

      var searchUrl = 'AddEnteringApproval_approval'; 

      var params = ''; 

      var elemArray = document.mainForm.elements; 
      for (var i = 0; i < elemArray.length;i++) 
      { 
       var element = elemArray[i]; 

       var elementName= element.name; 
       if(elementName=='formAction') 
        continue; 
       params += '&' + elementName+'='+ encodeURIComponent(element.value); 
      } 

      params += '&struts.enableJSONValidation=true&struts.validateOnly=true'; 

      createXmlHttpObject(); // this is my function that prepare ajax 

      sendRequestPost(http_request,searchUrl,false,params); 

      postValidation(); 

    } 

    function postValidation() { 
     var form = $('#mainForm'); 

     var text = http_request.responseText; 

     //clear previous validation errors, if any 
     StrutsUtils.clearValidationErrors(form); 
     alert(text) 
     //get errors from response 
     //var text = request.responseText; 
     var errorsObject = StrutsUtils.getValidationErrors(text); 

     //show errors, if any 
     if(errorsObject.fieldErrors) 
     { 
     StrutsUtils.showValidationErrors(form, errorsObject); 
     } 
     else 
     { 
     //good to go, regular submit 
     form.submit(); 
     } 
    } 


     /* This is one of the functions that doesn't work using the simple theme, so I redefined it. 
     This can be changed to clear the previous errors, as it does in the commented example 
     cleaning the errors on divErrors. 
     As I am just showing the messages with alerts I don't need to clear anything, 
     but the method still need to be redefined, even if it is empty. 
    */ 

    StrutsUtils.clearValidationErrors = function(form, errors) { 
     //clear the div errors 
     //$('#divErrors').empty(); 
    } 


    /* This method is responsible to show the messages. 
     The original versions works with the xhrml and css-xhtml theme but doesn't work with the simple theme 
     so I override the method with another implementation that shows the messages using alerts. 
     You can change the implementation to show the messages as you want, 
     but as the previous method this has to be redefined. 
    */ 

    StrutsUtils.showValidationErrors = function(form, errors) { 

     if(errors.fieldErrors) 
     {alert((errors.fieldErrors)) 
      for(var fieldName in errors.fieldErrors) 
      { 
       alert("errors.fieldErrors[fieldName] " + errors.fieldErrors[fieldName]); 

       for(var i = 0; i < errors.fieldErrors[fieldName].length; i++) 
       { 
        alert('Field ->' + fieldName + '\nError -> ' + errors.fieldErrors[fieldName][i]); 
       } 
      } 
     } 
    }; 

但是當我執行的代碼我沒有得到任何有組織的JSON文本我在警告消息框顯示,字段名稱不喜歡的人在錯誤信息,第二個字段名稱丟失,第三字段名稱被剪切(即carrierNo變成rNo)。

你能幫助我嗎?我想在JSON錯誤的字段名稱錯誤消息文本

符合我只是想出了什麼問題,但我不知道爲什麼會發生,以及爲什麼。 它總是刪除前6個字符。爲什麼出現這種情況

enter image description here

+0

客戶端和服務器端驗證是兩個單獨的問題。從問題中刪除所有服務器端詳細信息(包括標籤),並有更多機會解決它。 – Quaternion

+0

它是使用ajax進行服務器端驗證,我把我使用的驗證xml加上我用來讀取JSON的jvascript。告訴我你認爲什麼是無用的? – user1512999

+0

我只是想出了什麼是問題,但我不知道爲什麼會發生,爲什麼。 它總是刪除前6個字符。爲什麼會發生 – user1512999

回答

1

好,我想通了這個問題。 這是一個,但org.apache.struts2.interceptor.validation.JSONValidationInterceptor

它消除了由於6個字符這個不完整的if語句

sb.append((validationAware instanceof ModelDriven) ? ((String)fieldError.getKey()).substring(6) : (String)fieldError.getKey()); 

此錯誤是支柱2.1.8

fuond

它應該是這樣的

sb.append(((validationAware instanceof ModelDriven)) && (fieldErrorKey.startsWith("model.")) ? fieldErrorKey.substring(6) : fieldErrorKey); 

它在後來的支柱releses糾正。我糾正了這個問題。我想我必須分享面對問題的人的信息。