【发布时间】:2021-01-27 05:55:52
【问题描述】:
我正在开发一个 Java Web 应用程序,其中我有一个带有表单的 JSP。表单内部是一个表格,显示信息,每一行都有一个“修改”按钮。当按下时,我有一个 Javascript 脚本将表格中的标签替换为表单的输入,用户可以通过单击“接受”按钮填写并发送请求。
但是,当在 servlet 中使用 request.getParameter() 时,它返回 None,就好像输入不存在一样。确实创建了输入,执行了 post,并调用了 servlet 中的适当方法,但是当我尝试访问参数时,它们为 Null。
代码如下:
listadoCatLibro.jsp:
<form id="form-mod" action="/libreria-java/admin/listado-cat-libro/modificar" method="post" enctype="text/plain">
<table class="table">
<thead>
<tr>
<th scope="col">ID</th>
<th scope="col">Descripcion</th>
<th scope="col">Estado</th>
<th scope="col"> </th>
<th scope="col"> </th>
</tr>
</thead>
<tbody>
<c:if test="${requestScope.categorias != null }">
<c:forEach var="cat" begin="0" items="${requestScope.categorias}">
<tr id="row-cat-${cat.getId()}">
<th id="id-lbl-${cat.getId()}" scope="row">${cat.getId()}</th>
<td id="des-lbl-${cat.getId()}">${cat.getDesc()}</td>
<td id="est-lbl-${cat.getId()}">${cat.getEstado()}</td>
<td> <button id="mod-btn-${cat.getId()}"
class="btn btn-secondary float-right"
type="button"
onClick="modificar(${cat.getId()})">Modificar</button></td>
<td> <button id="del-btn-${cat.getId()}"
class="btn btn-secondary float-right"
type="button"
onClick="eliminar(${cat.getId()})">Eliminar</button></td>
</tr>
</c:forEach>
</c:if>
</tbody>
</table>
</form>
categorias.js
function modificar(id){
if (modificando == false){
modificando = true;
var sid = id.toString();
oldRow = document.getElementById('row-cat'+sid);
idlbl = document.getElementById("id-lbl-"+sid);
idTD = document.createElement('td');
newID = document.createElement('input');
newID.type="text";
newID.value=idlbl.innerHTML;
newID.name="inputID";
newID.style.width="50px";
newID.setAttribute("readonly","true");
newID.className="form-control";
idTD.appendChild(newID);
idlbl.parentNode.replaceChild(idTD, idlbl);
desclbl = document.getElementById("des-lbl-"+sid);
descTD = document.createElement('td');
newDesc = document.createElement('input');
newDesc.type="text";
newDesc.value=desclbl.innerHTML;
newDesc.name="inputDesc";
newDesc.className="form-control";
descTD.appendChild(newDesc);
desclbl.parentNode.replaceChild(descTD, desclbl);
estlbl = document.getElementById("est-lbl-"+sid);
estTD = document.createElement('td');
newEst = document.createElement('input');
newEst.type="text";
newEst.value=estlbl.innerHTML;
newEst.name="inputEstado";
newEst.className="form-control";
estTD.appendChild(newEst);
estlbl.parentNode.replaceChild(estTD, estlbl);
modbtn = document.getElementById("mod-btn-"+sid);
newMod = document.createElement('button');
newMod.className="btn btn-primary";
newMod.innerHTML="Aceptar";
modbtn.parentNode.replaceChild(newMod, modbtn);
delbtn = document.getElementById("del-btn-"+sid);
newDel = document.createElement('button');
newDel.className="btn btn-secondary";
newDel.innerHTML="Cancelar";
newDel.setAttribute('type', 'button');
delbtn.parentNode.replaceChild(newDel, delbtn);
}
}
还有 ServletAdmin.java
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
try {
if(isAdmin(request)) {
switch (request.getPathInfo()) {
case "/alta-libro":
this.altaLibro(request, response);
response.sendRedirect("/libreria-java/admin/alta-libro");
break;
case "/listado-cat-libro/alta":
this.altaCatLibro(request, response);
response.sendRedirect("/libreria-java/admin/listado-cat-libro");
break;
case "/listado-cat-libro/modificar":
this.modificarCatLibro(request,response);
response.sendRedirect("/libreria-java/admin/listado-cat-libro");
break;
}
} else {
response.sendRedirect("/libreria-java/home");
}
} catch (CustomException e) {
request.getSession().setAttribute("errorMsg", e.getMessage());
request.getRequestDispatcher( "/WEB-INF/pages/error.jsp" ).forward( request, response );
} catch (Exception ex) {
CustomException e = new CustomException("Error desconocido", "ServletAdmin", ex);
request.getSession().setAttribute("errorMsg", e.getMessage());
request.getRequestDispatcher( "/WEB-INF/pages/error.jsp" ).forward( request, response );
}
}
private void modificarCatLibro(HttpServletRequest req, HttpServletResponse res) {
CtrlCategoria ctrl = new CtrlCategoria();
Categoria c = new Categoria();
System.out.println(req.getParameter("inputID"));
System.out.println(req.getParameter("inputDesc"));
System.out.println(req.getParameter("inputEstado"));
c.setId(Integer.parseInt(req.getParameter("inputID")));
c.setDesc(req.getParameter("inputDesc"));
c.setEstado(req.getParameter("inputEstado"));
ctrl.update(c);
}
}
感谢您提供的任何帮助,并对冗长而混乱的代码表示歉意。我不想编辑太多,以防我不小心遗漏了导致问题的任何内容。
【问题讨论】:
标签: javascript java html jsp servlets