Java Spring:多部分表单数据请求:从请求中读取动态参数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19830662/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring : Multipart form data request : Read dynamic parameter from request
提问by Rakesh Soni
I am using Spring framework and able to upload the file on server successfully.
我正在使用 Spring 框架并且能够成功地在服务器上上传文件。
<form action="upload.do" method="post" enctype="multipart/form-data">
<input type="text" name="description" />
<input type="file" name="locationMapFile" />
........
........
<input type="submit" />
</form>
// controller layer
// 控制器层
@RequestMapping(value = "/upload.do", method = {RequestMethod.POST})
public String addEditLocationToCompany(Model model
,@RequestParam("description")String desc
,@RequestParam(value="locationMapFile", required=false) CommonsMultipartFile locationMapFileData)
{
}
till now everything is fine. Now i am adding some dynamic hidden parameter on form using the javascript.
到现在一切都很好。现在我正在使用 javascript 在表单上添加一些动态隐藏参数。
Note : as per setting i am defining the dynamic parameter name and its value like
注意:根据设置,我正在定义动态参数名称及其值,例如
<input type="hidden" name="setting_14" value="abcd">
<input type="hidden" name="setting_5" value="xyz">
How can i fetch these dynamic parameter into Spring controller.
如何将这些动态参数提取到 Spring 控制器中。
I have tried
我试过了
(1) I can not use the @RequestParam since do not want to hard code the parameter name
(1) 我不能使用 @RequestParam 因为不想硬编码参数名称
(2) request.getParameter() : not working and returning null since this is multipart/form-data request
(2) request.getParameter() :不工作并返回 null 因为这是多部分/表单数据请求
(3) I have used this link How to upload files to server using JSP/Servlet?and tried
(3) 我用过这个链接 How to upload files to server using JSP/Servlet?并尝试
List<FileItem> items =
new ServletFileUpload(new DiskFileItemFactory()).parseRequest(request);
but in spring controller itemsis null. so not able to iterate it and fetch the FileItem from it.
但在弹簧控制器中项目为空。因此无法对其进行迭代并从中获取 FileItem。
Please help me to find out the way to get the dynamic parameter's value into the spring framework.
请帮我找出将动态参数的值导入spring框架的方法。
采纳答案by Debojit Saikia
You can use MultipartHttpServletRequestto get the request parameters:
您可以使用MultipartHttpServletRequest来获取请求参数:
@RequestMapping(value = "/upload.do", method = {RequestMethod.POST})
public String addEditLocationToCompany(Model model
,@RequestParam("description")String desc
,@RequestParam(value="locationMapFile", required=false) CommonsMultipartFile locationMapFileData, MultipartHttpServletRequest mrequest)
{
String value = mrequest.getParameter("setting_14");
}
回答by Arsen Alexanyan
Why not just add one hidden field and set it's value by concating all setting values You want? As You said You are using javascript to add those input fields. It will look like
为什么不添加一个隐藏字段并通过连接所有你想要的设置值来设置它的值?正如您所说,您正在使用 javascript 添加这些输入字段。它看起来像
<input type="hidden" name="setting" value="set1.value, set2.value, set3.value">
Then You will be free to hardcode setting
parameter in your @RequestParam
然后你可以自由地setting
在你的@RequestParam