问题描述
我试图通过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.
你只需要放一个到你的项目。 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执行和响应的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!