具有不同值的多个提交按钮
Multiple submit buttons with different value
我需要捕获提交按钮的值,但按下按钮只会向我发送第一个生成按钮的值。
这是我的代码
modificarVivienda.jsp:
<table border="1">
<thead>
<tr>
<td>
Codigo vivienda
</td>
<td>
Direccion
</td>
<td>
Numero
</td>
<td>
Tipo vivienda
</td>
<td>
Condominio
</td>
<td>
Rut propietario
</td>
</tr>
</thead>
<tbody>
<c:forEach items="${lstviviendas}" var="v">
<tr>
<td>
${v.cod_vivienda}
</td>
<td>
${v.direccion}
</td>
<td>
${v.numero}
</td>
<td>
${v.tipo_vivienda}
</td>
<td>
${v.nombre_condominio}
</td>
<td>
${v.rut_propietario}
</td>
<input type="text" value="${v.cod_vivienda}" name="cod_vivienda" hidden="true">
<td>
<input type="submit" value="Modificar" id="btnModificar">
</td>
</tr>
</c:forEach>
</tbody>
</table>
Servlet:
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int cod_vivienda = Integer.parseInt(request.getParameter("cod_vivienda"));
DAOVivienda dv = new DAOVivienda();
HttpSession session = request.getSession();
ArrayList<Vivienda> vivienda = dv.buscarPorId(cod_vivienda);
session.setAttribute("viviendaAModificar", vivienda);
response.sendRedirect("vivienda/modificar.jsp");
}
"I need to capture the value of the submit button"
None 的 提交按钮 有一个名称,因此没有为按钮提交任何值。
我想你的意思是说你想要名为 cod_vivienda
.
的相关 <input>
的值
"pressing the button only sends me the value of the first generated button"
这是不正确的。所有 cod_vivienda
值都已提交。您可能刚刚调用了 getParameter("cod_vivienda")
,javadoc 说:
If you use this method with a multivalued parameter, the value returned is equal to the first value in the array returned by getParameterValues
.
如您所见,要获取所有值,您必须调用 getParameterValues("cod_vivienda")
。
要实际获取名为 cod_vivienda
的单个参数,其值与按下的按钮匹配,您应该使用 <button>
元素,它可以将值与按钮相关联,与显示的分开文本。
替换:
<input type="text" value="${v.cod_vivienda}" name="cod_vivienda" hidden="true">
<td>
<input type="submit" value="Modificar" id="btnModificar">
</td>
有:
<td>
<button type="submit" name="cod_vivienda" value="${v.cod_vivienda}">Modificar</button>
</td>
现在,只有 name/value 对按下的按钮会提交给服务器。
我需要捕获提交按钮的值,但按下按钮只会向我发送第一个生成按钮的值。
这是我的代码
modificarVivienda.jsp:
<table border="1">
<thead>
<tr>
<td>
Codigo vivienda
</td>
<td>
Direccion
</td>
<td>
Numero
</td>
<td>
Tipo vivienda
</td>
<td>
Condominio
</td>
<td>
Rut propietario
</td>
</tr>
</thead>
<tbody>
<c:forEach items="${lstviviendas}" var="v">
<tr>
<td>
${v.cod_vivienda}
</td>
<td>
${v.direccion}
</td>
<td>
${v.numero}
</td>
<td>
${v.tipo_vivienda}
</td>
<td>
${v.nombre_condominio}
</td>
<td>
${v.rut_propietario}
</td>
<input type="text" value="${v.cod_vivienda}" name="cod_vivienda" hidden="true">
<td>
<input type="submit" value="Modificar" id="btnModificar">
</td>
</tr>
</c:forEach>
</tbody>
</table>
Servlet:
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
int cod_vivienda = Integer.parseInt(request.getParameter("cod_vivienda"));
DAOVivienda dv = new DAOVivienda();
HttpSession session = request.getSession();
ArrayList<Vivienda> vivienda = dv.buscarPorId(cod_vivienda);
session.setAttribute("viviendaAModificar", vivienda);
response.sendRedirect("vivienda/modificar.jsp");
}
"I need to capture the value of the submit button"
None 的 提交按钮 有一个名称,因此没有为按钮提交任何值。
我想你的意思是说你想要名为 cod_vivienda
.
<input>
的值
"pressing the button only sends me the value of the first generated button"
这是不正确的。所有 cod_vivienda
值都已提交。您可能刚刚调用了 getParameter("cod_vivienda")
,javadoc 说:
If you use this method with a multivalued parameter, the value returned is equal to the first value in the array returned by
getParameterValues
.
如您所见,要获取所有值,您必须调用 getParameterValues("cod_vivienda")
。
要实际获取名为 cod_vivienda
的单个参数,其值与按下的按钮匹配,您应该使用 <button>
元素,它可以将值与按钮相关联,与显示的分开文本。
替换:
<input type="text" value="${v.cod_vivienda}" name="cod_vivienda" hidden="true">
<td>
<input type="submit" value="Modificar" id="btnModificar">
</td>
有:
<td>
<button type="submit" name="cod_vivienda" value="${v.cod_vivienda}">Modificar</button>
</td>
现在,只有 name/value 对按下的按钮会提交给服务器。