Issue
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?
Solution
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.
Answered By - Roman C