2012-01-16 53 views
1

我必須添加項目到現有的代碼,但我不知道所有建立的模式。 我需要從javascript中的數值字段中檢索值,將其存儲在JSP標記變量中並將其提交給Java方法。 我知道JavaScript是客戶端和服務器端Java。如何發送一個Javascript變量到服務器端?

這是一個數字字段「ZONE」,其值必須被回收用於循環

添加的代碼輸入「ZONE」,行動布通「ADD_ELEMENT_LISTE_OUVERTE_1」和變量和環「區​​」。

謝謝!

JSP端代碼:

<popo:form> 

    <div class="left"> 
    nombre de colonnes <input type="number" id="zone" name="zone" class="text" maxlength="25" valeur="" /> 
    </div> 
    <div class="right"> 
     <%controller.getContext().getInteger(ListeOuverteContributionDetailController.ZONE) =%> 

     <script type="text/javascript"> 
      function getZoneJS() { 
       var zone=(parseInt(document.form.elements["zone"].value)); 
       if (zone != null){ 
        return zone; 
       }           
      } 
     </script> 

     <popo:action name="ADD_ELEMENT_LISTE_OUVERTE_1" text="Ajouter les colonnes" />         
    </div> 

    <popo:listpanel dtcid="<%=ListeOuverteContributionDetailController.DTC_ELEMENTS_LISTE_OUVERTE%>" readonly="false"> 

     <popo:grid cols="2" colwidth="200px,*"> 
      <popo:iterator> 
       <popo:field name="VALEUR"/> 
       <popo:action name="DELETE_ELEMENT_LISTE_OUVERTE" text="<%=controller.getTextSupprimer()%>" style="icon"/> 
      </popo:iterator> 
     </popo:grid> 
    </popo:listpanel> 

    <div class="left"> 
     <popo:action name="ADD_ELEMENT_LISTE_OUVERTE" text="Ajouter un élément"/> 
    </div> 
    <div class="left"> 
     <%--<popo:link address="<%=AddressItems.IMPORT_LISTE_OUVERTE_ADDRESS.getAddress()%>" fields="CHAMP_ID" objects="<%="FOR_ID," + controller.getChampKey(champId)+","+controller.DTC_ELEMENTS_LISTE_OUVERTE%>" style="button">Importer</popo:link>--%> 
     <popo:action name="IMPORT_LISTE_OUVERTE" text="Importer"/> 
    </div> 
    <div class="left"> 
     <popo:action name="EXPORT_LISTE_OUVERTE" text="Exporter" /> 
    </div> 
    <div class="clear"></div> 

    <div class="buttonbar"> 
     <div class="left"> 
      <popo:link address="<%=AddressItems.SECTION_CONTRIBUTION_DETAIL_ADDRESS.getAddress()%>" fields="SEC_ID" objects="FOR_ID" params="<%="MODE=" + DetailController.MODE_EDIT%>" style="button" confirm="<%=Constantes.MSG_CONFIRM_QUITTER_ECRAN_CREATION_MAJ%>">Abandonner</popo:link> 
     </div> 
     <div class="right"> 
      <popo:action name="SAVE_MODIFICATIONS_LISTE_OUVERTE" text="Enregistrer" isdefaultaction="true"/> 
     </div> 
    </div> 
</popo:form> 

Java方法將檢索ZONE可變循環

public class ListeOuverteContributionDetailController extends AbstractContributionController { 

    /** 
    * Liste des éléments de la liste ouverte. 
    */ 
    public static final String DTC_ELEMENTS_LISTE_OUVERTE = "DTC_ELEMENTS_LISTE_OUVERTE"; 
    public static final String SI_AJOUT_ELEMENT = "SI_AJOUT_ELEMENT"; 
    public static final String ZONE = "ZONE"; 

    public Message executeAddElementListeOuverte1() throws KUserException, KSystemException { 

     final DtCollection<ElementListeOuverte> elementsListeOuverte = getContext().<ElementListeOuverte>getDtCollectionInput(DTC_ELEMENTS_LISTE_OUVERTE).validate(); 
     // la valeur zone qui doit être récupérée de la JSP 
     final int zone = Integer.parseInt(getContext().getString(ZONE)); 
     for (int i = 1; i <= zone; i++) { 
      // On ajoute un élément à la liste des éléments stockée dans le contexte en le flagant "Nouveau" 
      final ElementListeOuverte elementListeOuverte = new ElementListeOuverte(); 
      final ChampContribution champ = getContext().<ChampContribution>getDtObjectInput(getChampKey(getContext().getLong(CHAMP_ID))).validate(); 
      elementListeOuverte.setChampId(champ.getChampId()); 
      elementListeOuverte.setSiNouveau(true); 
      elementListeOuverte.setSiSupprime(false); 
      elementsListeOuverte.add(elementListeOuverte); 

      getContext().put(SI_AJOUT_ELEMENT, true); 
     } 
     return refresh(); 
    } 
} 
+0

一次,在代碼中存在*代碼太多*無論如何,您無法將值從JavaScript傳遞到Java到Java(反過來),你能*做的是做一個Ajax調用f rom JavaScript。 – Viruzzo 2012-01-16 15:04:17

+0

我很抱歉整個代碼,因爲我想解釋可能的概率。我會編輯它。在JSP方面,我已經成功使用JS函數getZoneJS()獲取項目值。現在我想將它設置在一個變量中,並將其提交給java方法。我不認爲AJAX會幫助我更多。 – frexville 2012-01-16 15:18:07

回答

3

其設置爲,其被封閉在所述形式中,一個隱藏的輸入字段的值你我想提交。

由於我不知道這些<popo:xxx>標籤代表/生成的東西,它們似乎是自定義標籤庫的一部分,我無法給出合適的答案。但也應該基本結束了,看起來像這樣:

<form ...> 
    ... 
    <input type="hidden" id="foo" name="foo" /> 
</form> 

,你可以進行如下設置由JS:

document.getElementById("foo").value = yourNewValue; 

這將是可作爲服務器端通常的方式請求參數一旦提交表格:

String foo = request.getParameter("foo"); 
+0

是一個自定義標籤庫。我得到的iput值爲var zone =(parseInt(document.form.elements [「zone」]。value));我想將它設置在一個變量JSP上,並在我的java方法的上下文中使用它。 – frexville 2012-01-16 16:29:42

+1

是的,已經猜到了。我不認爲這個名字是現有的和衆所周知的標籤庫之一。如果它是一個自定義庫,則可能不會期望1:1的支持,因爲沒有人熟悉它。我至少發佈了一個答案,它顯示瞭如何在標準JSP/JS/Servlet中執行此操作,以便您可以將自學習到的經驗應用到自定義標記庫中。如果你根本不明白我的答案,那麼它就停在這裏。 – BalusC 2012-01-16 16:31:23

+0

@frexville在你的工具包中尋找一些'隱藏的輸入'像控件,並相應地修改上面的代碼。 – Apurv 2012-02-08 05:03:56

相關問題