getParameter returning the same value

Asked

Viewed 215 times

0

I have a table where there’s a column

ID - NAME - ACTION
1 -- USU1 -- VER
2 -- USU2 -- VER
3 -- USU3 -- VER

in JSP I am storing the id inside a Hidden input.

<input type="hidden" value="${operador.id }" name="idOperador"/>

In Java I call this value through

int id = Integer.parseInt(request.getParameter("idOperador"));

Only independent of the item I click on "VIEW", it is taking the value of the first row of the table. In this case the id "1" or the first of the next pages.

Some guidance ?

Part of the code: This is within a table, where one of the columns (actions) consists of the action buttons (view, change and delete). When selecting the change option, it is taking the id of the first row of the table.

Note: this inside a foreach (JSTL)

<td class="actions">
   <input type="hidden" value="${operador.id }" name="idOperador"/>
   <div align="center">
      <div class="btn-group" role="group">
      <button type="button" class="btn bg-cyan waves-
      effect">Visualizar</button>
      <button type="submit" name="command" value="DadosAlterarOperador" 
      class="btn bg-cyan waves-effect">Alterar</button>
      <button type="button" class="btn btn-danger waves-
      effect">Excluir</button>
   </div>
 </div>
</td>
  • Apparently you are only setting a single ID in the Hidden field. Post the entire code snippet so we can analyze it better.

  • @Marquezani, I changed the question by adding a piece of code.

2 answers

0

inserir a descrição da imagem aqui

In this example I do not get several "ids" but the one I click on the respective button.

His ID is saved to the JSP foreach and then picked up a single ID with getParameter in Java.

  • I can only take it when I don’t use <form action="" method="POST/GET"></form>

  • I changed my code so that when I click on the "Change" button it already passes in the URL the command and also the id parameter. controller.do? command=Dataltelteoperator&idoperator=${operator.id} Only this way is bad, because the ID is exposed in the URL. Have some way to hide ?

0

The method getParameter() return only the first element found. In your case as is a list of elements the ideal is to use the method getParameterValues()

//o metodo retorna um array de Strings
String[] ids = request.getParameterValues("idOperador");

for(int i = 0;i < ids.length; i++)
{
   //pega o id desejado da lista
   int id = Integer.parseInt(ids[i]);
}
  • I’ll run the tests, thank you !

  • , I will reply as "answer" and no comment to display an image.

Browser other questions tagged

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