如果我在编辑时创建一个按钮表单,它给了我空指针。另一方面,如果我使用超链接,则根本没有问题。但是我怎么能正确传递值?我想传递的是action = edit
public class WaterController extends HttpServlet {
...
private static final String LIST_PIPES="WEB-INF/jsp/listPipess.jsp";
private static final String INSERT_EDIT = "WEB-INF/jsp/pipe.jsp";
private static final String EDIT_MODIFY = "WEB-INF/jsp/edit.jsp";
private static final String DELETE = "WEB-INF/jsp/delete.jsp";
private PipeD pipeD;
...
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
String forward = null;
String action=request.getParameter("action");
if(action.equals("list")){
request.setAttribute("pipes", pipeD.getAllPipes());
forward = LIST_PIPES;
} else if(action.equals("insert")) {
forward=INSERT_EDIT;
} else if(action.equals("edit")) {
request.setAttribute("pipeId", request.getParameter("pipeId"));
request.setAttribute("pipeSerial", request.getParameter("pipeSerial"));
request.setAttribute("pipeType", request.getParameter("pipeType"));
request.setAttribute("pipePrice", request.getParameter("pipePrice"));
request.setAttribute("dateOrdered", request.getParameter("dateOrdered"));
forward=EDIT_MODIFY;
} else if(action.equals("delete")) {...}
RequestDispatcher rd = request.getRequestDispatcher(forward);
rd.forward(request, response);
}
-----------------------------------在JSP(edit.jsp)中----- -----------
...
<form action="BookController?action=edit&id=${p.pipeId}&price=${p.pipePrice}" method="post">
<br/> Pipe Id: [READ]<input type="text" name="pipeId" value='<c:out value="${p.pipeId}"/>' readonly="readonly"/><c:out value="${p.pipeId}"/>
<br/> Pipe Serial: [READ]<input type="text" name="pipeSerial" value='<c:out value="${p.pipeSerial}"/>' readonly="readonly"/><c:out value="${p.pipeSerial}"/>
<br/> Pipe Type: [READ]<input type="text" name="pipeType" value='<c:out value="${p.pipetype}"/>' readonly="readonly"/><c:out value="${p.bookAuthor}"/>
<br/> Pipe Type: <input type="text" name="pipeType" value='<c:out value="${p.pipeType}"/>' />
<br/> Date Ordered (mm/dd/yyyy) [READ]: <input type="text" name="dateOrdered" value='<fmt:formatDate value="${p.dateOrdered}" pattern="MM/dd/yyyy"/>'readonly="readonly"/>
<br/><input type="submit" value="update"/><fmt:formatDate value="${p.dateOrdered}" pattern="MM/dd/yyyy"/>
</form>
...
------------------ listPipe.jsp -------
<td><form action="BookController?action=edit"><input type="submit" value="Edit"></form></td>
这一行是我想要重定向到编辑网址的地方。 (当点击立即空指针时)
我得到的错误是&#34;中的java.lang.NullPointerException; if(action.equals(&#34; list&#34;)){&#34;
答案 0 :(得分:0)
在表单提交期间,如果方法为GET
类型,则请求参数可能不会传递到服务器端。
将方法类型更改为POST
或使用form
标记内的隐藏字段(如下所示)将参数传递给服务器端
<form action="BookController">
<input type="hidden" name="action" value="edit">
<input type="submit" value="Edit">
</form>