Announcement Announcement Module
Collapse
No announcement yet.
XT AJAX extends Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • XT AJAX extends

    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 :
    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();
            }
        }
    CallBackJsHandler :
    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;
    	 }
    
    	
    }
    AjaxExceptionHandleImpl :
    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;
    	}
    
    }
    config file:


    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>
    test.html:

    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.
    Last edited by liulei; Sep 19th, 2007, 10:01 PM.

  • #2
    Hi,

    please can you better explain what are you trying to achieve, or what kind of problems are you experiencing?

    Thanks,
    Cheers,

    Sergio B.

    Comment


    • #3
      1. I request the url may not document.URL
      2. The page may have multiple form,but in the XT AJAX only use first form.

      Therefore, based on two points, I made changes, when the call doAjaxAction or doAjaxSubmit I designated a formId, url is form action ,parameter is form parameter.



      XT AJAX using server-side javascript output, but when I use the YUI or the dojo, this approach can't help me, so I defined a javascript call back function.


      When throw exception , I hope a javascript function call, so I defined an error handler function。

      XT AJAX request should add some parameters, such as httpMethod, async etc (like DWR)
      Last edited by liulei; Sep 19th, 2007, 05:17 AM.

      Comment


      • #4
        Originally posted by liulei View Post
        1. I request the url may not document.URL
        2. The page may have multiple form,but in the XT AJAX only use first form.
        1. The request URL *must* be the document URL for security reasons. Retrieving data from other URLs will be addressed in future through cross domain Ajax.

        2. There already is an issue about that: http://opensource.atlassian.com/proj...browse/MOD-213
        I'm planning to fix it after the upcoming 0.9 release.

        Cheers,

        Sergio B.

        Comment


        • #5
          I mean document url is http://test.com/index.html, ajax request url is http://test.com/person.do ,they under same domain,but they is different url.

          Comment

          Working...
          X