Struts2 Action 通过 AJAX 执行和响应 [英] Struts2 Action execution and response through AJAX

查看:20
本文介绍了Struts2 Action 通过 AJAX 执行和响应的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试通过 jQuery 将 AJAX 发布请求发送到 Struts2 操作类以处理表单并接收 JSON 响应,但我还没有弄清楚如何去做.

I am trying to send an AJAX post request via jQuery to a Struts2 action class for processing a form and receiving JSON response, but I haven't figured out how to do it.

首先,我制作了表单并为该表单设置了 Struts 以使其在没有 JavaScript 的情况下工作(尝试进行渐进式增强),现在我正在尝试通过 jQuery 使其工作.我可以轻松地创建一个新的 servlet,它会调用 action 类的验证和执行方法并在那里打印响应,但我首先要确保可以只使用 action 类.

First, I made the form and set up Struts for this form to work without JavaScript (trying to do progressive enhancement), and now I am trying to get it to work through jQuery. I could easily just create a new servlet that would call the validate and execute methods of the action class and print the response there, but I first want to make sure that it is possible to use just the action class.

我已经阅读了有关使用内置 struts 验证器进行 Ajax 验证的文章,但我不想这样做.至少有一种方法可以通过 Ajax 获取 ActionErrorsActionMessages 吗?

I've read about Ajax validation using the built in struts validator, but I don't want to do that. Is there at least a way to get the ActionErrors and ActionMessages via Ajax?

推荐答案

通过将 jQuery 集成到框架中,Struts2 可以轻松发送 Ajax 请求,该框架可用于处理表单和对数据进行一些验证.

Sending Ajax request is so easy with Struts2 via the integration of jQuery into framework that could be used for processing a form and doing some validations of data.

您不需要做太多事情,只需放一个 jQuery 插件 到您的项目.通过集成 JSON 插件,使用 Struts2 也可以轻松接收 JSON 响应.JSON 插件向项目添加了 JSON 结果类型,可用于返回 JSON 响应.

You don't need to do much just put a jQuery plugin to your project. Receiving JSON response also easy with Struts2 via the integration of JSON plugin. The JSON plugin adds a JSON result type to the project that could be used to return JSON response.

如果您使用 servlet 来验证和执行操作类的方法,您将无法使用框架提供的功能,即插件,您必须自行承担风险.

If you are using servlet to validate and execute methods of the action class you can't use the features provided by the framework, i.e. plugins and you have to do it yourself on your own risk.

如果您不想使用 Struts2 验证框架,那么您希望如何获得您没有使用的操作错误或操作消息?这些操作错误动作消息只是包含文本的简单集合.因此,您可以毫无问题地通过 Ajax 返回它们.

If you don't want to use the Struts2 validation framework, so how would you like to get action errors or action messages that you didn't use? These action errors and action messages are just simple collections that contain text. So, you could return them via Ajax without any problem.

这篇关于Struts2 Action 通过 AJAX 执行和响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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