简体   繁体   English

Spring MVC表单:使用Ajax的错误

[英]Spring MVC form:errors using Ajax

I have a question related to Spring 3 MVC, Ajax, and form:errors. 我有一个与Spring 3 MVC,Ajax和form:errors相关的问题。

I have a form I am submitting to the server and I am using Spring MVC. 我有一个表单,我提交给服务器,我正在使用Spring MVC。 I have all the validation, controllers, etc set up and working fine. 我有所有的验证,控制器等设置和工作正常。 When I submit data that fails validation, I get the error message coming back and being displayed on the form as per my form:errors tags in my jsp. 当我提交未通过验证的数据时,我会收到错误消息,并根据我的表单显示在表单上:我的jsp中的错误标记。

My question is... 我的问题是......

If I want to convert over to an Ajax approach, can I still take advantage of all the infrastructure of a "normal" Spring MVC app (ie, specifically the validator and form:errors features) to get the errors back to (and displayed on) the browser? 如果我想转换为Ajax方法,我是否仍然可以利用“普通”Spring MVC应用程序的所有基础结构(即,特别是验证器和表单:错误功能)来将错误恢复到(并显示在)浏览器? I see how to submit Ajax requests, but I don't see how (or if) I can still utilize the form:errors capability of the jsp. 我看到了如何提交Ajax请求,但是我没有看到我是如何(或者如果)仍然可以利用jsp的形式:错误功能。

Does anyone know if this can be done, and if so, point me in the appropriate general direction? 有谁知道这是否可以做到,如果是的话,请指出适当的总体方向?

Thanks in advance. 提前致谢。

You can't, since tags like <form:errors> are evaluated server-side and embed error messages/elements in the HTML content of the HTTP response. 你不能,因为像<form:errors>这样的标签被评估为服务器端,并在HTTP响应的HTML内容中嵌入错误消息/元素。 Since an AJAX request to the server typically returns XML or JSON to a response (and not HTML), which is then parsed by client-side JavaScript, there is no point at which Spring can bind to the request/form and add error messages to the content. 由于对服务器的AJAX请求通常会将XML或JSON返回给响应(而不是HTML),然后由客户端JavaScript解析,因此Spring无法绑定到请求/表单并向其添加错误消息内容。

I have used quite a lot of AJAX when submitting forms in Spring. 在Spring中提交表单时,我使用了相当多的AJAX。 Basically what I have done is that I have created a form.jsp file which I then return from the AJAX call. 基本上我所做的是我创建了一个form.jsp文件然后从AJAX调用返回。 Since the AJAX will return the whole form with all the form tags available, I get the full benefit of server side validation. 由于AJAX将返回包含所有表单标签的整个表单,因此我获得了服务器端验证的全部好处。

So, if I have a page that contains a form, I could, for example, have a div that acts as the form container 因此,如果我有一个包含表单的页面,我可以,例如,有一个div作为表单容器

<div id="form-container">
    <form id="myform">
        Normal form here ...
    </form>
</div>

And when I submit the form, I could load the form.jsp by using jQuery, for example, 当我提交表单时,我可以使用jQuery加载form.jsp,例如,

$('#myform').submit(function() {
    $('#form-container').load('ajax/somehandler');
});

So, all you have to do is to return the form.jsp from the controller with all the normal bindings as you would have with non-AJAX approach as well. 因此,您所要做的就是从控制器返回form.jsp以及所有正常绑定,就像使用非AJAX方法一样。 Do you get the idea? 你明白了吗?

Not really, no. 不是,不是。 The form:error tag does its magic when the page is first generated server-side. form:error标记在页面首次生成服务器端时发挥作用。 If you're not re-generating the page serverside, because you used ajax, none of that is going to run. 如果你没有重新生成页面服务器端,因为你使用了ajax,所以没有一个会运行。 The Errors object itself isn't really returnable all the way back to the client. Errors对象本身并不是真正可以返回到客户端。 You could serialize it to JSON I suppose, but there's an awful lot of stuff in there that you don't need. 您可以将它序列化为JSON我想,但是那里有很多你不需要的东西。 Plus of course it usually only has codes, not actual messages, on it and needs the message source to look up the translations. 当然,它通常只有代码而不是实际消息,并且需要消息源来查找翻译。

Bottom line, you'll need to send the errors manually and handle setting the error messages yourself in javascript when you get back the ajax response. 最重要的是,您需要手动发送错误并在返回ajax响应时自行处理在javascript中设置错误消息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM