最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

java - How to send a Javascript variable to server side? - Stack Overflow

programmeradmin0浏览0评论

I must add items to an existing code, but I do not know all the patterns established. I need to retrieve a value from a numeric field in javascript, store it in a JSP tag variable and submit it to a Java method. I know javascript is client side and the server-side Java.

This is a numeric field "ZONE" whose value must be recovered for a loop

The added code is input "ZONE", the Action buton "ADD_ELEMENT_LISTE_OUVERTE_1" and the variable and loop "zone".

thanks !

JSP-side code:

<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 method that will retrieve the ZONE variable to loop

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();
    }
}

I must add items to an existing code, but I do not know all the patterns established. I need to retrieve a value from a numeric field in javascript, store it in a JSP tag variable and submit it to a Java method. I know javascript is client side and the server-side Java.

This is a numeric field "ZONE" whose value must be recovered for a loop

The added code is input "ZONE", the Action buton "ADD_ELEMENT_LISTE_OUVERTE_1" and the variable and loop "zone".

thanks !

JSP-side code:

<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 method that will retrieve the ZONE variable to loop

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();
    }
}
Share Improve this question edited Jan 16, 2012 at 15:52 BalusC 1.1m376 gold badges3.7k silver badges3.6k bronze badges asked Jan 16, 2012 at 14:52 frexvillefrexville 171 silver badge7 bronze badges 2
  • For once, there is too much code in the question... In any case, you cannot pass values from JavaScript to JSP to Java (it's the other way around), what you can do is make an Ajax call from JavaScript. – Viruzzo Commented Jan 16, 2012 at 15:04
  • I'm sorry for the whole code, 'cause I wanted to explain may prob. I'll edit it. In the JSP side, I've successed to get item value with the JS function getZoneJS(). Now I want to set it in a variable and submit it to the java method. I don't think AJAX will help me more. – frexville Commented Jan 16, 2012 at 15:18
Add a ment  | 

1 Answer 1

Reset to default 4

Set it as the value of a hidden input field which is enclosed in the form you'd like to submit.

As I have no idea what those <popo:xxx> tags represent/generate, they seem to be part of a custom tag library, I can't give a suitable answer. But it should basically end up to look like this:

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

which you can set as follows by JS:

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

it'll be available as a request parameter in the server side the usual way once the form is submitted:

String foo = request.getParameter("foo");
发布评论

评论列表(0)

  1. 暂无评论