The combination of structs and ajax

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.