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

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

问题描述

我试图通过jQuery向一个Struts2动作类发送一个AJAX post请求来处理一个表单并接收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来调用动作类的validate和execute方法并在那里打印响应,但我首先要确保只使用动作类。

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获取 ActionErrors ActionMessages

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插件到你的项目。 Struts2通过集成 JSON插件来轻松接收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天全站免登陆