开发者

Cant send the right key to the servlet

I can`t seem to send the right entity key to servlet in my web app. I am using javascript method to submit the form with the data via a button. The code is divide into jstl code:

  <c:if test="${!empty MOFornecedorList}">    
                        <div id="RightColumn">
                              <%-- Search Box --%>
                                         <div class="searchform">
                                              <form id="formsearch" name="formsearch" method="post" action="<c:url value='FProcurar'/>">
                                                <span>
                                                    <input  name="searchBox" class="editbox_search" id="editbox_search"  size="80" maxlength="100" value="Pesquisa" type="text" />
                                                </span>
                                                <input name="btnsearch" class="button_search" value="Pesquisa" type="button"/>
                                              </form>
                                             <div class="clr"></div>
                                         开发者_如何学Python    <h>Criterio de Pesquisa: </h>                                
                                             <select name="Type">                                    
                                                 <option value="1">ID</option>
                                                 <option value="2">Nome</option>
                                                 <option value="3">Email</option>
                                                 <option value="4">Fax</option>
                                                 <option value="5">Endereço</option>                                              
                                             </select>

                                        </div> 
                                 <%-- END Search Box --%>
                         <div class="clr"></div>
                          <table id="ProductTable" class="detailsTable">

                                <tr class="header">
                                    <th colspan="9" >Modificar Fornecedor</th>
                                </tr>

                                <tr class="tableHeading">
                                    <td>ID</td>
                                    <td>Nome</td>
                                    <td>Endereço</td>                                       
                                    <td>Nº de Celular</td>
                                    <td>Nº de Telefone</td>
                                    <td>Email</td>    
                                    <td>Fax</td> 
                                    <td>Descrição</td>
                                    <td></td>
                                </tr>

                                <c:forEach var="MOForn" items="${MOFornecedorList}" varStatus="iter">

                                   <tr class="${'white'} tableRow">   
                                        <td>${MOForn.getFid()}</td>
                                        <td>${MOForn.getFNome()}</td>
                                        <td>${MOForn.getFEndereco()}</td>                                           
                                        <td>${MOForn.getFNCel()}</td>
                                        <td>${MOForn.getFNTel()}</td>
                                        <td>${MOForn.getFEmail()}</td>    
                                        <td>${MOForn.getFFax()}</td>
                                        <td>${MOForn.getFDescricao()}</td>

                                        <td>
                                            <form action="<c:url value='FMOb'/>" method="post" name="FModifi">
                                                <input type="hidden"
                                                       name="MOForn"
                                                       value="${MOForn.fid}">                                                
                                                <input type="button"
                                                       value="Modificar" onclick="ModF()">
                                            </form>
                                        </td> 
                                    </tr>

                                </c:forEach>

                            </table> 
                          </div>
                        </c:if>

the javascript method

 function ModF() {
                    jConfirm('Modificar o Fornecedor?', 'Confirmação', function(r) {

                             if (r == true) {                                     
                                 $("form[name='FModifi']").submit();                           
                                } else {
                                  return false;
                                }
                    });
                }

and the controller code:

//Check if fornecedor as been selected            
            int Fid = Integer.parseInt(request.getParameter("MOForn"));

          //Get fornecedor object and set it to variable
          Forn = transManager.getEnt(Fid,"fornecedor");  

          request.setAttribute("Forn",Forn);      

          PagesInF="FModificar";
          request.setAttribute("PagesInF", PagesInF);
          userPath = "/Fornecedor";             

Now when i test the code the jstl will read 5 records in the item MOFornecedorList in ascending order and a button will be created in the last column.

When the button is pressed for example in the third record the JavaScript method Modf() is invoked and a confirm dialog is shown.

When the user presses the OK button the form FModifi is submitted.

Then the servlet will receive a the request to open the page FMOb where the hidden input for the button pressed will be retrieved and put in a variable type int and some other code will execute.

But the value that the form submit's is the wrong one. ex:

1 - button - MOforn = 1

2 - button - MOforn = 2

3 - button - MOforn = 3 (clicked)

4 - button - MOforn = 4

5 - button - MOforn = 5

The Form should send the value of 3 but sends the value of 5.

So please if anyone as any ideas please share.


You've multiple forms with the same name. Your JS function isn't submitting the form from which it was been called, but it is submitting the last occurrence of the form with that name in the HTML DOM tree.

You need to replace

<input type="button" value="Modificar" onclick="ModF()">

by

<input type="button" value="Modificar" onclick="confirmSubmit(this.form)">

and rewrite the function as follows:

function confirmSubmit(form) {
    jConfirm('Modificar o Fornecedor?', 'Confirmação', function(confirmed) {
        if (confirmed) {
            form.submit();
        }
    }
}

I'd also suggest to use more self-documenting variable and function names like presented above, so that your code is easier understandable and maintainable in long term (not only for yourself, but also for others, for example the ones on Stackoverflow.com from who you expect an answer when you post a question...)

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜