7 Replies Latest reply on Dec 13, 2007 9:46 PM by sergeysmirnov

    Error with a4j:form and h:commandLink

    terryb

      I have h:commandLink as below to open a new window. Clicking it does open new window but as 'Page cannot be displayed', and Java script pop error.

      Error:
      Error: 'A4J' is undefiend
      Code:0
      URL: javascript:A4J.AJAX.SubmitForm('_viewRoot','paymentReminderLetter',{'parameters':{'paymentReminderLetter':'paymentReminderLetter'}%20,'actionUrl':'/report/payment-reminder.seam'}%20)
      
      XHTML
      <a4j:form id="paymentReminderLetter" styleClass="edit" ajaxSubmit="true" onsubmit="return validateForm(document.paymentReminderLetter);" reRender="paymentReminderSearchResult">
      
      
      <h:commandLink value="Generate Letter" action="#{paymentReminderLetter.generateLetter}" onclick="return generateLetter(document.paymentReminderLetter);" target="_blank" type="submit"/>
      
      </a4j:form>
      
      
      Form validation javascript function
      <script type="text/javascript">
      //<![CDATA[
       function validateForm(form) {
      
       if (!validateDate(form.elements('paymentReminderLetter:declaredDateFromInputDate'), "Enter valid value for 'Declared date from'.")) {
       return false;
       }
      
       if (!validateDate(form.elements('paymentReminderLetter:declaredDateToInputDate'), "Enter valid value for 'Declared date to'.")) {
       return false;
       }
       return true;
       }
      //]]>
      </script>
      


      replacing h:commandLink with a4j:commandLink doesn't produce error but then it does not open window. seems a4j:commandLink ignores target="_blank".


        • 1. Re: Error with a4j:form and h:commandLink
          • 2. Re: Error with a4j:form and h:commandLink
            terryb

            I tried a4j_commandLink. it doesn't produce error buy it does not open the window. it seems to ignore target="_blank" ?

            my application requires to open new window.

            • 3. Re: Error with a4j:form and h:commandLink

              Did I say a4j:commandLink ?

              • 4. Re: Error with a4j:form and h:commandLink
                terryb

                Sergey, perhaps I am still doing something wrong because I am getting same error even with a4j:htmlCommandLink.

                ...
                <a4j:form id="paymentReminderLetter" styleClass="edit" ajaxSubmit="true" reRender="paymentReminderSearchResult"
                 onsubmit="return validateForm(document.paymentReminderLetter);">
                
                ...
                
                <a4j:htmlCommandLink value="Generate Letter" action="#{paymentReminderLetter.generateLetter}"
                 target="_blank" type="submit"/>
                
                ...
                
                


                It works ok, if I remove target="_blank" from a4j:htmlCommandLink.



                • 5. Re: Error with a4j:form and h:commandLink

                  Why you need ajaxSubmit="true" on the a4j:form ?

                  • 6. Re: Error with a4j:form and h:commandLink
                    terryb

                    Thanks, removing it fixed the the JavaScript error. my first ever use of commandLink, and being new to this stuff it is sometimes mix and match to see what works. appreciate your patience.

                    I'm pretty close now on this one; just one final issue now (I hope):

                    I now have two a4j:commandLink and one a4j:htmlCommandLink on this page. a4j:htmlCommandLink has 'target="_blank'. If I click a4j:htmlCommandLink, a new window is opened with pdf as expected. But clicking any of the a4j:commandLink right after that, has unusual behaviour: It runs the action action associated with a4j:commandLink but it then also runs action associated with a4j:htmlCommandLink - as if I had clicked that one too.

                    I guess it is combination of different types making it harder.

                    
                    XHTML:
                    
                     <a4j:form id="paymentReminderLetter" styleClass="edit" onsubmit="return validateForm(document.paymentReminderLetter);">
                    
                     <rich:separator height="1"/>
                     <div class="dialogtable">
                    
                     <a4j:outputPanel id="formPanel">
                     <h:panelGrid columns="3" rowClasses="prop" columnClasses="name,value,hint">
                     ...
                     ...
                     </h:panelGrid>
                     </a4j:outputPanel>
                     </div>
                     <rich:separator height="1"/>
                     <div>
                     <table >
                     <tr class="prop">
                     <td class="value">
                     <div class="tableControl">
                    
                     <a4j:htmlCommandLink id="checkAvailable" value="Check Available" action="#{paymentReminderLetter.submitSearchAction}" type="submit"
                     reRender="paymentReminderSearchResult"/>
                    
                     <a4j:htmlCommandLink id="reset" value="Reset" action="#{paymentReminderLetter.reset}" type="submit"
                     reRender="paymentReminderSearchResult,formPanel"
                     />
                    
                     <a4j:htmlCommandLink value="Generate Letter" action="#{paymentReminderLetter.generateLetter}"
                     onclick="return generateLetter(document.paymentReminderLetter);" *also having this onclick in does not submit the form.*
                     reRender="paymentReminderSearchResult" *also reRender does not work on this.*
                     target="_blank" type="submit" />
                    
                     </div>
                     </td>
                     </tr>
                     </table>
                     </div>
                     </a4j:form>
                    
                     </td></tr>
                     </table>
                     <rich:spacer height="5"/>
                    
                     <div class="results">
                     <a4j:outputPanel id="paymentReminderSearchResult">
                     ...
                     </a4j:outputPanel
                    
                    <script type="text/javascript">
                    //<![CDATA[
                    
                     function generateLetter(form) {
                    
                     if (!confirm('Generate letters for applications in range\nand flag them as reminded?')) {
                     return false;
                     }
                     return validateForm(form);
                     }
                    
                     function validateForm(form) {
                    
                     if (!validateDate(form.elements('paymentReminderLetter:declaredDateFromInputDate'), "Enter valid value for 'Declared date from'.")) {
                     return false;
                     }
                    
                     if (!validateDate(form.elements('paymentReminderLetter:declaredDateToInputDate'), "Enter valid value for 'Declared date to'.")) {
                     return false;
                     }
                     return true;
                     }
                    
                     focusOn(document.paymentReminderLetter.elements('paymentReminderLetter:declaredDateFromInputDate'));
                    
                    //]]>
                    </script>
                    </ui:define>
                    </ui:composition>
                    
                    paymentReminderLetter:
                    @Name("paymentReminderLetter")
                    @Scope(ScopeType.CONVERSATION)
                    public class PaymentReminderLetter {
                    
                     ...
                     public String reset() throws ApplicationException {
                    
                     this.declaredDateFrom = null;
                     this.declaredDateTo = null;
                     this.docFormat = Constant.Itext.DOC_TYPE_PDF;
                     this.maxLetterPerAttempt = this.MAX_LETTERS_TO_PRINT_DEFAULT;
                     this.maxQueryResults = Constant.Database.MAX_QUERY_RESULT;
                     this.regenerate = false;
                     pageAction();
                     submitSearchAction();
                     return null;
                     }
                    
                     public String submitSearchAction() throws ApplicationException {
                    
                     this.maxQueryResults = Constant.Database.MAX_QUERY_RESULT;
                     submitSearch();
                     return "";
                     }
                    
                     public String generateLetter() throws ApplicationException {
                    
                     this.maxQueryResults = this.maxLetterPerAttempt;
                     submitSearch();
                    
                     if (this.getResultCount() <= 0) {
                     FacesMessages.instance().add(FacesMessage.SEVERITY_INFO, "No declard but unpaid applicants exist within specified criteria.");
                     } else {
                     flagAsReminded();
                     }
                    
                     return null;
                     }
                    
                     private void submitSearch() throws ApplicationException {
                    
                     try {
                     Query query = em.createQuery(this.getEjbql());
                     query.setMaxResults(this.maxQueryResults);
                     paymentReminderLetterList = query.getResultList();
                     this.executeSearch = true;
                     } catch (Exception e) {
                     throw new OnlineException(OnlineException.ERROR_EXTRACTING_RECORD, "Payment Reminder Letter. Error: " + e.getMessage());
                     }
                     }
                    }
                    
                    


                    • 7. Re: Error with a4j:form and h:commandLink

                      a4j:htmlCommandLink is NOT an Ajax link. So, reRender attribute does not make sense on it.

                      a4j:htmlCommandLink is a regular link. So, when you click it, the whole page is reloaded unless you use target attribute.