2012-01-16 60 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

相关问题