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);
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.
source share