I extend XT AJAX MVC framework,XT AJAX MVC framework successful return after calling a javascript function, and when abnormal also called a javascript function。
springxt.js :
CallBackJsHandler :Code:doAjaxSubmitCallBack : function(options, sourceElement, serverParams, requestParams) { var ajaxActionType = "ajax-submit"; var eventId = "callBackJs"; var form = document.getElementById(options.formId); var url = form.action; if (requestParams && requestParams.enableUpload && requestParams.enableUpload == true) { var parameters = this.createIFrameRequestParamsObject(ajaxActionType, eventId, sourceElement, serverParams); var iframeRequest = new IFrameRequest(form, url, parameters); this.configureLoadingInfo(requestParams, iframeRequest); iframeRequest.sendRequest(); } else { var ajaxRequest = new AjaxRequest(url); ajaxRequest.addFormElements(form); ajaxRequest.setQueryString(ajaxRequest.getQueryString() + "&" + this.ajaxParameter + "=" + ajaxActionType + "&" + this.eventParameter + "=" + eventId + "&" + ( options.callback != null ? ( "callback=" + options.callback):"" ) + "&" + ( options.errorHandler != null ? ( "errorHandler=" + options.errorHandler):"" ) + this.createSimpleQueryString(sourceElement) + this.createJSONQueryString(serverParams)); this.configureLoadingInfo(requestParams, ajaxRequest); if(options.async!=null&&options.async==false) ajaxRequest.setAsync(false); if(options.httpMethod!=null&&options.httpMethod=="get") ajaxRequest.setUseGET(); else ajaxRequest.setUsePOST(); ajaxRequest.sendRequest(); } }
AjaxExceptionHandleImpl :Code:public class CallBackJsHandler extends AbstractAjaxHandler{ private String encoding = AjaxResponseImpl.DEFAULT_ENCODING; public String getEncoding() { return encoding; } public void setEncoding(String encoding) { this.encoding = encoding; } public AjaxResponse callBackJs(AjaxSubmitEvent event) { String callBack = event.getHttpRequest().getParameter("callback"); if(callBack==null||callBack.equals("")) throw new RuntimeException("回调函数不可为空!"); ExecuteJavascriptFunctionAction action = new ExecuteJavascriptFunctionAction(callBack,event.getModel()); // Create a concrete ajax response: AjaxResponse response = new AjaxResponseImpl("UTF-8"); // Add the action: response.addAction(action); return response; } }
config file:Code:public class AjaxExceptionHandleImpl implements AjaxExceptionHandler { private String exceptionHandler; public String getExceptionHandler() { return exceptionHandler; } public void setExceptionHandler(String exceptionHandler) { this.exceptionHandler = exceptionHandler; } public AjaxResponse handle(HttpServletRequest request, Exception ex) { String errorHandler = request.getParameter("errorHandler"); if(errorHandler!=null&&!errorHandler.equals("")){ this.exceptionHandler = errorHandler; } Map model = new HashMap(); model.put("message", ex.getMessage()); AjaxResponse ajaxResponse = new AjaxResponseImpl(); ExecuteJavascriptFunctionAction action = new ExecuteJavascriptFunctionAction( this.exceptionHandler , model); ajaxResponse.addAction(action); return ajaxResponse; } }
test.html:Code:<bean id="callBackJsHandler" class="mvc.convention.ajax.CallBackJsHandler"/> <bean id="ajaxInterceptor" class="org.springmodules.xt.ajax.AjaxInterceptor"> <property name="handlerMappings"> <props> <prop key="/**/*.action">callBackJsHandler</prop> </props> </property> </bean> <!-- AJAX Exception Handler --> <bean id = "ajaxExceptionHandle" class="mvc.convention.ajax.AjaxExceptionHandleImpl"> <property name="exceptionHandler"> <value>errorHandler</value> </property> </bean> <bean id="ajaxExceptionResolver" class="org.springmodules.xt.ajax.AjaxExceptionHandlerResolver"> <property name="exceptionMappings"> <map> <entry key="java.lang.Exception" value-ref="ajaxExceptionHandle"/> </map> </property> </bean>
Code:<html> <head> <meta http-equiv="Content-Type" content="text/html; charset=UTF-8"> <script type="text/javascript" src='js/springxt.js'></script> <script type="text/javascript" > function debug(obj) { if(obj) { var s = ''; for(var itm in obj) { s += 'obj.' + itm + '=' + obj[itm] + ';\r\n'; } alert(s); } } function responseMessage(person){ alert(person.name); debug(person.person); } function errorHandler(error){ debug(error); } </script> </head> <body> <form method="POST" action="/xt/convention/convention.action" id="form1"> <input type="input" name="id" /> <input type="input" name="age" /> <input type="input" name="name" /> <input type="button" value="vraptor's easy ajax" name="click" onclick="XT.doAjaxSubmitCallBack({formId:'form1',callback:'responseMessage',errorHandler:'errorHandler'}, this);" > </form> </body> </html>
I hope everyone to give me some advices.


Reply With Quote
