在我的imageutility.jsp中,在从数据库获取详细信息后,我以表格的形式显示图像,我有一个编辑表单(在imageutility.jsp中),它将图像的id传递给imageedit.java中的doget(),在获得该属性后,将其发送到imageedit.jsp,我将尝试访问该id属性以及用户输入的新名称。
图像实用程序.jsp
for (Images i : li) {
id = i.getId() + "";
name = i.getName();
size = i.getSize() + " kb";
preview = i.getImagePath();
System.out.println(id+" "+name+" "+size);
pageContext.setAttribute("id", id);
pageContext.setAttribute("name", name);
pageContext.setAttribute("size", size);
%>
<tr>
<td>${id}</td>
<td class='ImgName'>${name}</td>
<td>${size}</td>
<td><img width='150px' height='150px' src='" + preview + "'></td>
<td><form action="ImageEdit" method="get"><input type="hidden" name="id" value=${id} /><input type="submit" value="Edit"/></form></td>
<td><form action="ImageDelete"><input type="hidden" value=${id} /><input type="submit" value="Delete" /></form></td>
</tr>
图像编辑.java
@WebServlet("/ImageEdit")
public class ImageEdit extends HttpServlet {
private static final long serialVersionUID = 1L;
protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int id=(int) request.getAttribute("id");
System.out.println(id);
//request.setAttribute(name, o);
request.setAttribute("imageId", id);
request.getRequestDispatcher("ImageEdit.jsp").forward(request, response);
}
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
int imageId = (int) request.getAttribute("imageId");
// int imageId = Integer.parseInt(str) ;
String str = request.getParameter("name");
Session session = HibernateUtil.getSessionFactory().openSession();
session.beginTransaction();
Images img = (Images) session.get(Images.class, imageId);
img.setName(str);
try {
session.update(img);
session.getTransaction().commit();
} catch (Exception e) {
session.getTransaction().rollback();
session.close();
PrintWriter out = response.getWriter();
out.print("error");
return ;
}
session.close();
response.sendRedirect("./ImageUtility.jsp");
}
}
图像编辑.jsp
<html>
<head>
</head>
<body>
<div id ="overlay"></div>
<form method="post" action="ImageEdit" >
<fieldset>
<legend >Change Image Name</legend>
<label>Enter Name : <Input name="name" required></label><br><br>
<input type="submit">
</fieldset>
</form>
</body>
</html>
当我点击这里的编辑按钮
<td><form action="ImageEdit" method="get"><input type="hidden" name="id" value=${id} /><input type="submit" value="Edit"/></form></td>
在imageutility.jsp中,我将要删除的图像的id传递给doget()方法,但在doget()方法中我发现了一个空指针异常。我不知道是什么原因造成的
java.lang.NullPointerException
com.package.ImageUtilityApp.controllers.ImageEdit.doGet(ImageEdit.java:23)
javax.servlet.http.HttpServlet.service(HttpServlet.java:626)
javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
任何建议都会有帮助
1条答案
按热度按时间v8wbuo2f1#
我非常确信“get”表单的表单值会变成查询参数而不是属性。
试着改变你的习惯
doGet()
方法来源:int id=(int) request.getAttribute("id");
收件人:int id=(int) request.getParameter("id");
###旁注我很有信心你不能
String
至和int
就像那样。您最想做的是:
int id = Integer.parseInt(request.getParameter("id"));
但是,如果那样的话id
如果没有传入值,将再次出现空指针异常,因此可能需要先执行空检查。。。然后验证输入值是一个数字。。。等。。。