The combination of structs2 and ajax does not capture server errors whether $. post or $. ajax is used. This is not well designed in structs2,
But $. ajax can catch errors in servlet. Because
Public void serviceAction (HttpServletRequest request, HttpServletResponse response, ServletContext context,
ActionMapping mapping) throws ServletException {
Map ExtraContext = createContextMap (request, response, mapping, context );
// If there was a previous value stack, then create a new copy and pass it in to be used by the new Action
ValueStack stack = (ValueStack) request. getAttribute (ServletActionContext. STRUTS_VALUESTACK_KEY );
Boolean nullStack = stack = null;
If (nullStack ){
ActionContext ctx = ActionContext. getContext ();
If (ctx! = Null ){
Stack = ctx. getValueStack ();
}
}
If (stack! = Null ){
ExtraContext. put (ActionContext. VALUE_STACK, valueStackFactory. createValueStack (stack ));
}
String timerKey = "Handling request from Dispatcher ";
Try {
UtilTimerStack. push (timerKey );
String namespace = mapping. getNamespace ();
String name = mapping. getName ();
String method = mapping. getMethod ();
Configuration config = configurationManager. getConfiguration ();
ActionProxy proxy = config. getContainer (). getInstance (ActionProxyFactory. class). createActionProxy (
Namespace, name, method, extraContext, true, false );
Request. setAttribute (ServletActionContext. STRUTS_VALUESTACK_KEY, proxy. getInvocation (). getStack ());
// If the ActionMapping says to go straight to a result, do it!
If (mapping. getResult ()! = Null ){
Result result = mapping. getResult ();
Result.exe cute (proxy. getInvocation ());
} Else {
Proxy.exe cute ();
}
// If there was a previous value stack then set it back onto the request
If (! NullStack ){
Request. setAttribute (ServletActionContext. STRUTS_VALUESTACK_KEY, stack );
}
} Catch (ConfigurationException e ){
// WW-2874 Only log error if in devMode
If (devMode ){
String reqStr = request. getRequestURI ();
If (request. getQueryString ()! = Null ){
ReqStr = reqStr + "? "+ Request. getQueryString ();
}
LOG. error ("cocould not find action or result \ n" + reqStr, e );
}
Else {
If (LOG. isWarnEnabled ()){
LOG. warn ("cocould not find action or result", e );
}
}
SendError (request, response, context, HttpServletResponse. SC _NOT_FOUND, e); // generate an error template page and return it to the client
} Catch (Exception e ){
SendError (request, response, context, HttpServletResponse. SC _INTERNAL_SERVER_ERROR, e); // generate an error template page and return it to the client
} Finally {
UtilTimerStack. pop (timerKey );
}
}
Therefore, all struts2 errors will go to the template page.