You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@struts.apache.org by Alireza Fattahi <af...@yahoo.com> on 2013/11/15 10:03:02 UTC
Showing Action Exception as Ajax Errors
Hi,
We are using struts2 JSONValidationWithInterceptor. This interceptor grabs the errors from validation and convert them to json message
How can we have same feature! An interceptor which grabs the action exceptions and serialize them as json errors.
I try below interceptor. When an exception happens it works, when there are no errors and I should pass {} to response it is not working!
_______________________________________________
String target=Action.NONE;
try {
target=invocation.invoke();
response.getWriter().print("{}");
response.setContentType("application/json");
return target;
}
catch (BussinessException ex) {
StringBuilder sb = new StringBuilder();
sb.append("{ ");
sb.append("\"errors\": [\"");
sb.append("Bussiness Exception Happend");
sb.append("\"]}");
response.setContentType("application/json");
response.getWriter().print(sb.toString());
return Action.NONE;
}
~Regards,
~~Alireza Fattahi
Antwort: Showing Action Exception as Ajax Errors
Posted by Christoph Nenning <Ch...@lex-com.net>.
> Hi,
>
> We are using struts2 JSONValidationWithInterceptor. This interceptor
> grabs the errors from validation and convert them to json message
> How can we have same feature! An interceptor which grabs the action
> exceptions and serialize them as json errors.
>
> I try below interceptor. When an exception happens it works, when
> there are no errors and I should pass {} to response it is not working!
> _______________________________________________
>
> String target=Action.NONE;
> try {
> target=invocation.invoke();
> response.getWriter().print("{}");
> response.setContentType("application/json");
> return target;
> }
>
invocation.invoke() does these things:
- execute remaining interceptors
- execute action
- execute result (e.g. rendering a JSP)
- execute remaining interceptors again (what they do after
invocation.invoke())
So, when no exception is thrown the response has already been created and
the lines
response.getWriter().print("{}");
response.setContentType("application/json");
have no effect.
You can try to change the action to return Action.NONE
This Email was scanned by Sophos Anti Virus