我必须将项目添加到现有代码中,但我不知道所有已建立的模式。 我需要从 javascript 中的数字字段中检索一个值,将其存储在 JSP 标记变量中并将其提交给 Java 方法。 我知道 javascript 是客户端和服务器端 Java。
这是一个数字字段“ZONE”,其值必须在循环中恢复
添加的代码是输入“ZONE”、操作按钮“ADD_ELEMENT_LISTE_OUVERTE_1”以及变量和循环“zone”。
谢谢!
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>
将检索 ZONE 变量以循环的 Java 方法
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();
}
}
最佳答案
将其设置为包含在您要提交的表单中的隐藏输入字段的值。
因为我不知道那些是什么 <popo:xxx>
标签代表/生成,它们似乎是自定义标签库的一部分,我无法给出合适的答案。但它基本上应该看起来像这样:
<form ...>
...
<input type="hidden" id="foo" name="foo" />
</form>
可以通过JS设置如下:
document.getElementById("foo").value = yourNewValue;
提交表单后,它将作为服务器端的请求参数以通常的方式提供:
String foo = request.getParameter("foo");
关于java - 如何将 Javascript 变量发送到服务器端?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8881686/