JQuery Modal Forms with Struts 1.3

I am building a web application using Struts 1.3 for a class project and I am having some problems with AJAX Struts 1.x compatibility (I heard that 2.x works better with AJAX and jQuery).

Thanks for the answer, this is an updated issue:

I am currently using the trendy jquery UI form in the same jsp and want to submit the form data to the Struts action when the user clicks “create new location” using AJAX. How do I send (and receive) data between a form and a Struts action?

In other words, the connection between:

"Create new venue": function() { $.ajax({ url: "/registered/insertVenue.do", data: }); 

(this is the code for my sumbit button for a modal form, I don’t know how to attach data in such a way that it can be read with the Struts action)

and the 'execute' method of the Struts action (which returns an ActionForward or null).

Thanks again!:)

+4
source share
2 answers

One thing, if you want to return data outside of ActionForward , you must return null . When Struts sees an ActionForward null, it does not redirect.

After doing the following type of design, I used to create JSON Response in Struts:

 public interface Result { public void applyResult(HttpServletRequest request, HttpServletResponse response) throws Exception; } public abstract class ResultBasedAction extends Action { public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception { Result result = execute(mapping, form, request); if (result == null) { throw new Exception("Result expected."); } result.applyResult(request, response); //Finally, we don't want Struts to execute the forward return null; } public abstract Result execute(ActionMapping mapping, ActionForm form, HttpServletRequest request) throws Exception; } public class JsonResult implements Result { private JSONObject json; public JsonResult(JSONObject json) { this.json = json; } public void applyResult(HttpServletRequest request, HttpServletResponse response) throws Exception { response.addHeader("Content-Type", "application/json"); response.getOutputStream().write(json.toString().getBytes("UTF-8")); response.getOutputStream().flush(); } } 

All of your AJAX related responses will implement the ResultBasedAction action for the action and Result for the data that will be sent to the client.

On your ajax, you just need to do an HTTP GET , passing all the parameters to the URL. Make sure the parameters match your Struts ActionForm for the desired Action class.

+4
source

The wireframe really doesn't really matter much in terms of raw JavaScript / jQuery / Ajax.

You can return everything you want from your Struts 1 action. If you want some JSON to return with a status or flash message, you can either write it directly to the response, or return null instead of ActionForward , or create a JSP, to get the desired content and set the appropriate title.

How the return value of an Ajax request is processed, it all depends on the client code: Struts 1 does not care about what type of request it has; he will simply return everything that he set up to spit back.

+3
source

Source: https://habr.com/ru/post/1382414/


All Articles