Pass JSP value to Action

Asked

Viewed 908 times

1

I have the following jsp below, on it the user chooses the status, by choosing the status my javascript shows in an Alert with the information I want to pass to Action to follow the validations and takes the user to another action "locate-stores-state.do", this is occurring, when threshing I get to fall into the Action I wish, but I can’t in any way carry the value of the sgState to Action "locates-stores-state.do"

  <script>
    function mantemEstado(){

        var sgEstado = document.localizarLojasForm["sgEstado"].value;
        alert(sgEstado);
        form.action = "<c:url value="/localiza-lojas-estado.do"/>";
        form.submit();
    }
    </script>

    <div class="fr">

        <div class="caminho-link">
            <a href="#" class="caminho-link-1" target="_blank">Home </a> <font
                face="Arial, Helvetica, sans-serif" color="#007088"
                style="font-weight: bold; font-size: 12px;"> >> </font> <a href="#"
                class="caminho-link-2" target="_blank">Localizar Lojas</a>
        </div>

        <div class="slocator-box">
            <html:form action="/localiza-lojas.do" method="post" styleId="cadastroCliente2">

<html:hidden property="sgEstado"/>

                <div class="slocator-estado">
                    <html:select tabindex="2" onkeypress="Enter(this.tabIndex, event)" property="sgEstado" styleClass="w80" value="Digite seu E-mail" onchange="javascript:mantemEstado();"     >
                        <html:options collection="listaEstados" property="sgEstado" labelProperty="sgEstado" />
                    </html:select>
                </div>


                <div class="slocator-cidade">
                    <html:select tabindex="2" onkeypress="Enter(this.tabIndex, event)" property="dsCidade" styleClass="w80">
                        <html:options collection="listaCidades" property="dsCidade" labelProperty="dsCidade"  />
                    </html:select>
                </div>

                <html:hidden property="sgEstado"/>
                <div class="slocator-bairro">
                    <html:select tabindex="2" onkeypress="Enter(this.tabIndex, event)" property="dsBairro" styleClass="w80">
                        <html:options collection="listaBairros" property="dsBairro" labelProperty="dsBairro" />
                    </html:select>
                </div>
  • 1

    Create an Hidden field in the form, in the function mantemEstado() you assign the value of sgEstado in Hidden.

  • created <html:Hidden Property="sgEstad"/> inside <html:form>, as the value attribute?

  • document.getElementById('sgEstado').value = sgEstado;&#This code goes inside the function, check Hidden id, use the browser’s source code display option. The html is generated by Struts 1.x ?

  • but I didn’t understand how I pass the value to the action, how do I get a print on my value action that I want for example? Thanks for the attention

  • yes, it is generated by the!

1 answer

1

Change this:

<html:hidden property="sgEstado"/>

For this reason:

<input type="hidden"  name="sgEstado"  id="sgEstado"/>

Include this in your script:

document.getElementById('sgEstado').value = sgEstado;

Go to your Actions class and declare this (with getters and setters):

private String sgEstado;

Explanation:

Javascript uses the id to locate the hidden tag (Hidden) and set its value. Struts2 uses the name to set the string value (i.e., the variable name and the tag "name" must be identical).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.