是否使用 enctype="multipart/form-data" 形成表单?导致访问隐藏字段时出现问题
我创建了一个隐藏的表单元素
<form name="UploadImage" enctype="multipart/form-data" method="post" action="UploadImage">
<label>
</label>
<input name="imgUploadObjId" id="imgUploadObjId" value="52" type="hidden">
//rest of the form here
</form>
,并且我试图在 servlet 中使用这一行获取值(就像我之前所做的那样):
int objId = Integer.parseInt(request.getParameter("imgUploadObjId"));
但是我得到了这个(第 33 行是上面的行):
java.lang.NumberFormatException: null
java.lang.Integer.parseInt(来源未知) java.lang.Integer.parseInt(来源未知) web.objects.UploadImage.doPost(UploadImage.java:33) javax.servlet.http.HttpServlet.service(HttpServlet.java:637) javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
enctype="multipart/form-data" 的表单有什么不同吗?或者你能看到其他一些错误吗?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
Servlet 默认使用
application/x-www-form-urlencoded
编码来解析参数。然而,servlet 中不支持multipart/form-data
编码,直到 Servlet 3.0。getParameter()
调用都将返回null
。在 Servlet 3.0 中,您应该使用
HttpServletRequest#getParts()
而是获取multipart/form-data
请求的所有部分,包括普通表单字段。在 Servlet 3.0 之前,您应该使用 Apache Commons FileUpload 来解析multipart/form-data请求。另请参阅以下答案,了解两种方法的详细示例:如何使用 JSP/Servlet 将文件上传到服务器?
请注意,如果您根本没有使用任何
字段,那么您可以只保留远离
The servlet parses the parameters by default using
application/x-www-form-urlencoded
encoding. Themultipart/form-data
encoding however isn't supported in servlets until Servlet 3.0. ThegetParameter()
calls will all returnnull
.In Servlet 3.0, you should have used
HttpServletRequest#getParts()
instead to get all parts of amultipart/form-data
request, including normal form fields. Prior to Servlet 3.0, you should have used Apache Commons FileUpload to parsemultipart/form-data
requests. See also the following answer for a detailed example of both approaches: How to upload files to server using JSP/Servlet?Note that if you aren't using any
<input type="file">
field at all, then you can just leave the encoding away from the<form>
. It will then default toapplication/x-www-form-urlencoded
.作为解决方法,您还可以在表单的操作属性中添加所需的隐藏参数作为 GET 参数:
这将使
request.getParameter("imgUploadObjId")
调用正常工作。As a workaround, you can also add the required hidden parameters as GET parameters in the form's action attribute:
this will allow the
request.getParameter("imgUploadObjId")
call to work.确实有不一样的地方。
request.getParameter
仅适用于所有其他参数都将被编码到表单本身中,您必须通过直接解析 HTTP 请求的输入流来处理这些参数。
幸运的是,您不是第一个,并且有一些很好的开源库可以解决这个问题。
我一直在使用 Apache FileUpload。您创建一个解析器并向其传递一个请求对象,然后迭代不同的项目。其中之一将是您的隐藏字段。
Indeed there is something different.
request.getParameter
will only work for hardcoded URL parameters specified inaction
attribute of<form>
element. In your case it does not contain any.All other parameters will be incoded into the form itself, which you have to process by parsing HTTP request's input stream directly.
Fortunately, you are not the first and there are some good open-source libraries that take care of this.
I've been using Apache FileUpload. You create a parser and pass a request object to it and then iterate through different items. One of them will be your hidden field.
多部分编码不应影响隐藏文本字段。很可能是别的东西。您可以发布更多 HTML/Servlet 代码吗?
The multi-part encoding shouldn't affect hidden text fields. It is likely something else. Can you post more of the HTML/Servlet code?
不确定这是否有帮助,但我在提交给 struts servlet 的 jsp 页面中使用了多部分表单,并且这些页面具有在我的 Struts Action 类中接收的隐藏字段(包装在 Struts ActionForm 中),所以我认为没有这里有什么困难吗?
您是否尝试过以字符串形式接收该值并查看实际情况?
Not sure if this helps, but I have used multipart forms in jsp pages which are submitted to a struts servlet and these pages have hidden fields which are received in my Struts Action classes (wrapped in Struts ActionForm), so I don't think there is any hard stop here.
Have you tried receiving this value as String and seeing what actually comes there?
您需要检查 servlet 代码本身。您收到请求了吗?您可以调试应用程序,以便在尝试获取值并解析它时查看环境中存在哪些变量。
You'd check the servlet code itself. Are you getting the request? Can you debug the app in order to see which variables are present in the environment when you try to get the value and parse it.
我只为该字段设置了 id 属性,它没有显示在列表项列表中。当我添加名称属性时,它就出现了。
I only had the id atttribute set for the field and it didn't show up in the List items list. When I added the name attribute, it showed up.