Struts2 Action execution and response through AJAX

不问归期 提交于 2019-12-17 19:56:41

问题


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.

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.

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?


回答1:


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.

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.

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.

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.



来源:https://stackoverflow.com/questions/18928936/struts2-action-execution-and-response-through-ajax

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!