javaajaxstruts2struts-config

How to handle exceptions thrown in a Struts 2 user-defined AJAX result?


I am using Struts 2 user-defined result that is used with an AJAX request.

When it throws an exception, I get a "Struts Problem Report" in html format as a response. This is not very useful. How can I intelligently handle such an exception -- either call the appropriate javascript errorResponse function or take the user to another page?

public class MyResult implements Result {

    @Override
    public void execute(ActionInvocation invocation) {

        if (invocation.getStack().findValue("data") == null) {
            throw MyException("Data is bad.");
        }

        PrintWriter responseStream =
            ServletActionContext.getResponse().getWriter();
        responseStream.println("Data is good.");
        responseStream.close();
    }
}

Solution

  • Either declare an exception handling result or stream something useful back.