Spring MVC 上传文件并将解析后的数据发送回来

发布于 2024-12-19 15:59:03 字数 470 浏览 0 评论 0原文

我正在为我的 Web 应用程序使用 Spring MVC。我正在实现文件上传功能。按照教程,我能够通过“文件”输入类型的提交操作将上传的文件发送到服务器。然后我在服务器端解析上传的 xls 文件,并需要将解析后的数据(3 个自定义对象列表)发送回同一表单进行显示。我通过 ModelAttribute 将数据发回。

但是,我现在的问题是,在客户端,我需要在我的 javascript 中使用这些自定义对象列表,但我只能通过 jstl 标签库检索自定义对象的每个字段,但我无法在我的 javascript 中获取这些自定义对象我需要它们来实现其他逻辑。

然后我尝试了一个Ajax文件上传插件,因为ajax调用可以返回JSON响应,这些对象可以在Javascript中使用。但无法让插件正常工作。

我已经被这个问题困扰好几天了>.<有人可以帮忙吗?在 Javascript 中使用 ModelAttribute 的解决方案或 Ajax 调用解决方案都可以。非常感谢!!!

I'm using Spring MVC for my web application. I'm implementing the file upload functionality. Following the tutorial, I was able to send the uploaded file to server through submit action of input type of "file". Then I parsed the uploaded xls file at server side and need to send parsed data (3 lists of custom objects) back to the same form to display. I sent the data back through ModelAttribute.

However, my problem now is that on the client side, I need to use those lists of custom objects in my javascript, but I can only retrieve each field of the custom objects through jstl tag library but I cannot get those custom objects in my javascript where I need them for other logic implementation.

Then I have tried an Ajax file upload plug-in because ajax call can return JSON response which objects can be used in Javascript. But cannot get the plug-in work properly.

I have been stuck on this problem for several days>.< Can anybody help on this? Either a solution on using ModelAttribute in Javascript or a Ajax call solution is ok. Thank you very much!!!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

平定天下 2024-12-26 15:59:03

有什么理由不能简单地将模型对象“扁平化”为 HTML,然后使用 DOM API 从 Javascript 访问它们?

您说您返回 3 个自定义对象列表。那么,对于每个列表,您可以构建一个(隐藏的)HTML 表,例如,如果您在名为 people< 的 ModelAttribute 中有一个 Person 对象列表, /code>:

<table id="people-table">
  <c:forEach var="person" items="${people}">
    <tr id="${person.id}>
      <td class="person-name">${person.name}</td>
      <td class="person-age">${person.age}</td>
      <td class="person-height">${person.height}</td>
    </tr>
  </c:forEach>
</table>

现在在 Javascript 中,您可以迭代 people-table 中的行,执行您需要执行的任何操作 - 例如使用 jQuery

$("#people-table tr").each(
    var personId = $(this).attr("id");
    var personName = $(this).find("td.person-name").text();
    // etc, etc
    // Do something with these as required
);

可能不是最优雅的解决方案,但至少很容易调试 - 您始终可以使用 Firebug 等检查 table 来查看数据是否正确,然后您将知道是否继续调试服务器- 端或在 Javascript 中。

Is there any reason why you can't simply "flatten" your model objects into HTML and then access them from Javascript using the DOM API?

You said you return 3 lists of custom objects. So how about for each list, you build up a (hidden) HTML table, for example if you have a list of Person objects in a ModelAttribute called people:

<table id="people-table">
  <c:forEach var="person" items="${people}">
    <tr id="${person.id}>
      <td class="person-name">${person.name}</td>
      <td class="person-age">${person.age}</td>
      <td class="person-height">${person.height}</td>
    </tr>
  </c:forEach>
</table>

Now in your Javascript, you iterate over the rows in people-table, doing whatever you need to do - for example using jQuery:

$("#people-table tr").each(
    var personId = $(this).attr("id");
    var personName = $(this).find("td.person-name").text();
    // etc, etc
    // Do something with these as required
);

It's possibly not the most elegant solution but it's easy to debug at least - you can always just inspect the table with Firebug etc to see if the data is correct, and from there you'll know whether to continue debugging the server-side or in the Javascript.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文