在Spring MVC中处理带有一系列项目的表单发布 [英] handle form post with a array of items in spring MVC

查看:82
本文介绍了在Spring MVC中处理带有一系列项目的表单发布的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些数据从客户端发送到服务器,并将其处理为文件下载。
我使用的是简单的HTML表单,因为我想初始化文件下载(而不是AJAX)。
表单字段之一是项目数组。 (其他两个是名称和描述字符串)。
在提交表单之前,我正在将该字段序列化为字符串(JSON.stringify)。

I'm trying to send some data from the client side to the server, and have it processed into a file download. I'm using a simple HTML form because I want to initialize a file download (and not AJAX). one of the form fields is an array of items. (the other two are name and description strings). I'm serializing this field to a string (JSON.stringify) before submitting the form.

在服务器端,我尝试了一百万种技巧(@ModelAttribute与@RequestBody,不同的jackson映射bean配置)将其转换为单一类型或三种独立类型(字符串+字符串+列表/数组)。

on the server side I tried a million techniques (@ModelAttribute vs. @RequestBody, different jackson mapping bean configurations) to either convert this to a single type or to three separate types (String + String + List/Array).

我发现的示例仅适用于AJAX ...
任何人都可以提供一个有效的示例或说明吗?

the examples I found were only for AJAX... can anyone supply a working example or a description of one?

======

更新:
我已经实现了一种变通方法,方法是JSON.stringify-ing集合并将其传递到输入之一
中并在服务器上我有:

Update: I've implemented a workaround by JSON.stringify-ing the collection and passing it in one of the inputs, and on the server side I have:

@RequestMapping(method = RequestMethod.POST, value = "exportSectionsToExcel")
  public HttpEntity<byte[]> createExcelWorkBook(@ModelAttribute ExportSectionsListForm exportSectionsListForm) {
Section[] sectionObjects = gson.fromJson(exportSectionsListForm.getSections(), Section[].class);
...

仅带有字符串的ExportSectionsListForm对象:

with ExportSectionsListForm object containing strings only:

public class ExportSectionsListForm {
private String name;
private String url;
private String rssUrl;
private String sections;
...
(omitting ctor, getters and setters)

我找到了这个有希望的链接:
http:// viralpatel.net/blogs/spring-mvc-multi-row-submit-java-list/
但没有尝试-看来我需要动态生成输入元素才能正常工作,但实际上可能是正确的解决方案。有人尝试过吗?

additionally, I found this promising link: http://viralpatel.net/blogs/spring-mvc-multi-row-submit-java-list/ but didn't try it - seems like I'll need to dynamically generate input elements for this to work, but it might actually be the right solution. has anyone tried this?

推荐答案

@ModelAttribute标记将尝试基于表单发布来构建对象。由于您要将表格值序列化为JSON,因此无法正常工作。 @RequestBody只是给您一个代表请求主体的字符串。因此,您可以获取表示要传入的JSON的String,然后使用FlexJSON的Jackson(或您使用的任何JSON库)对JSON进行编组。我不确定这是否是最好的方法。

The @ModelAttribute tag will try to build the object based on form postings. Since you are serializing your form values to JSON, this wont work. @RequestBody simply gives you a String representing the request body. So, you could get the String representing the JSON being passed in, then demarshal the JSON using Jackson of FlexJSON (or whatever JSON library you use). I am not sure this is the best approach, though.

我会问为什么首先需要将表单序列化为JSON。 Spring用Lists / Maps处理表单就好了。只需使用@ModelAttribute提交表单,在Controller上创建数组和List,或任何您期望的内容。因此,如果我正确地解释了您的示例,则我的ModelAttribute看起来像:

I would question why you need to serialize the form to JSON to begin with. Spring handles forms with Lists/Maps just fine. Simply submit the form using the @ModelAttribute, making your "array" and List, or whatever you are expecting, on the Controller. So, if I am interpreting your example correctly, my ModelAttribute would look like:

public class ExportSectionsFormBean {
  private String name;
  private String url;
  private String rssUrl;
  private List<String> sections;
  /* getters/setters */
}

然后我的Controller方法看起来像这样:

Then my Controller method would look like:

@RequestMapping(method = RequestMethod.POST, value = "exportSectionsToExcel")
public HttpEntity<byte[]> createExcelWorkBook(@ModelAttribute ExportSectionsFormBean exportSectionsFormBean ) {
  /* Do whatever with your  */
}

在表单方面,使用Spring JSTL标记,只需使您的 sections字段看起来像:

On the form side, using the Spring JSTL tags, simply make your "sections" fields look like:

<form:input path="sections[0]" />
<form:input path="sections[1]" />

或者,如果您更愿意使用HTML,则

Or, if you'd rather use HTML, then

<input type="text" name="sections[0]" id="sections0" />
<input type="text" name="sections[1]" id="sections1" />

上述JSTL标记生成的内容是什么。只要将 sections的值作为 section [#] = value 放置在HTTP请求中,就一切就绪。

Which is what gets generated by the above JSTL tags. As long as the values for "sections" is put in the HTTP request as 'section[#]=value', you are all set.

这篇关于在Spring MVC中处理带有一系列项目的表单发布的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆