PDA

View Full Version : [JSP] ottenere name e value da un tag <input>


Alhazred
24-07-2008, 17:59
Ho una form creata dinamicamente in una jsp a seguito di una query sul DB.
Le righe sono il contenuto del result set ottenuto e rappresentano un elenco di ingredienti.
Alla fine di ogni riga c'è un campo di testo nel quale inserire la quantità che si vuole acquistare di quell'ingrediente.

<input type="text" id="ingrediente<%= i %>" name="<%= rs.getString("nome") %>" value="0" />

l'attributo name contiene il nome dell'ingrediente, value la quantità che se ne vuole acquistare.
Ho aggiunto l'attributo id in modo che ogni input si chiami in modo facilmente rintracciabile nella servlet visto che altrimenti non saprei come fare prendendo l'attributo name che varia a seconda della query.

Il mio problema è questo: nella servlet mi serve sapere sia l'attributo name che value per ogni campo che presenti un valore > 0, ma non so come fare, riesco a prendere value, ma non name.

Come posso fare?

questa e la form:

<form name="rimanenze" action="VerificaRimanenze" method="post">
<table width="650" border="0" cellpadding="6">
<tr>
<td width="25%"><strong>Ingrediente</strong></td>
<td width="25%"><p align="center"><strong>Unità di misura</strong></p></td>
<td width="20%"><p align="center"><strong>Disponibilità</strong></p></td>
<td width="20%"><p align="center"><strong>Soglia</strong></p></td>
<td width="10%"><p align="center"><strong>Acquista</strong></p></td>
</tr>
<% rs = IngrController.getInEsaurimento();
while(rs.next()) { %>
<tr>
<td><%= rs.getString("nome") %> </td>
<td><%= rs.getString("unitaDiMisura") %></td>
<td><%= rs.getString("disponibilita") %></td>
<td><div align="center"><%= rs.getString("soglia") %></div></td>
<td><div align="center"><input type="text" id="ingrediente<%= i %>" name="<%= rs.getString("nome") %>" value="0" onChange="selezionato(this)" /></div></td>
</tr>
<%i++; } %>
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td><input type="hidden" name="totale_selezionati" value="0"/>&nbsp;</td>
<td><input type="submit" name="acquista" value="Acquista" /></td>
</tr>
</table>

selezionato() è una funzione javascript che tiene il conto di quanti ingredienti hanno il campo value > 0, ovvero quanti sono gli ingredienti che si intende acquistare e aggiorna il campo nascosto totale_selezionati.

Questo è il metodo doPost della servlet

protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int i = Integer.parseInt(request.getParameter("totale_selezionati")); //numero di ingredienti con quntità scelta > 0
int j = 0; //conta gli ingredienti con quantità > 0 trovati
int aux = 0; //cicla su tutti gli ingredienti nel form
String[][] ingrediente = new String[i][2];

while (j < i) { //finché il numero di ingredienti con quantità > 0 trovati non raggiunge il valore conosciuto
if(Integer.parseInt(request.getParameter("ingrediente"+aux)) > 0) {
ingrediente[j][0] = ; //qui dovrei prendere l'attributo name
ingrediente[j][1] = request.getParameter("ingrediente"+aux); //questo prende l'attributo value
j++;
aux++;
}
else
aux++;
}

IngrController.aggiungiIngredienteMagazzino(ingrediente,i);

RequestDispatcher dispatcher = request.getRequestDispatcher("/verificarimanenze.jsp");
dispatcher.forward(request, response);
}

Alhazred
25-07-2008, 13:58
Ho risolto aggiungendo un altro campo nascosto.