EXT提交服务器的三种方式

一、 EXT提交服务器的三种方式 
   1. EXT的form表单ajax提交(默认提交方式) 
       相对单独的ajax提交来说优点在于能省略写参数数组 

       FormPanel

       在Ext中FormPanel并中并不保存表单数据,其中的数据是由BasicForm保存,在提交表单的时候需要获取当前 FormPanel中的BasicForm来进行提交.      
      获取FormPanel中的BasicForm对象代码如下:

     

var pnlLogin = new Ext.FormPanel({
    //省略
});

//获取BasicForm对象
pnlLogin.getForm();

 

在获取BasicForm对象后便可进行表单的提交操作...  此时需要查一下BasicForm 的API文档, 文档中说,需要调用submit();方法进行提交:

BasicForm submit方法API 写道

submit( Object options ) : BasicForm

Shortcut to do a submit action.

Parameters:

* options : Object
The options to pass to the action (see doAction for details)

Returns:

* BasicForm
this

 

由API文档可以知道,submit方法实际上是调用了BasicForm的doAction()方法, 而doAction放法在API文档中的描述如下:

     BasicForm doAction() API 写道

    

doAction( String/Object actionName, [Object options] ) : BasicForm 

Performs a predefined action (Ext.form.Action.Submit or Ext.form.Action.Load) or a custom extension of Ext.form.Action to perform application-specific processing. 

Parameters: 

* actionName : String/Object 
The name of the predefined action type, or instance of Ext.form.Action to perform. 
* options : Object 
(optional) The options to pass to the Ext.form.Action. All of the config options listed below are supported by both the submit and load actions unless otherwise noted (custom actions could also accept other config options): 
o url : String 

The url for the action (defaults to the form's url.) 
o method : String 

The form method to use (defaults to the form's method, or POST if not defined) 
o params : String/Object 

The params to pass (defaults to the form's baseParams, or none if not defined) 
o headers : Object 

Request headers to set for the action (defaults to the form's default headers) 
o success : Function 

The callback that will be invoked after a successful response. Note that this is HTTP success (the transaction was sent and received correctly), but the resulting response data can still contain data errors. The function is passed the following parameters: 
+ form : Ext.form.BasicForm 
The form that requested the action 
+ action : Ext.form.Action 
The Action class. The result property of this object may be examined to perform custom postprocessing. 

o failure : Function 

The callback that will be invoked after a failed transaction attempt. Note that this is HTTP failure, which means a non-successful HTTP code was returned from the server. The function is passed the following parameters: 
+ form : Ext.form.BasicForm 
The form that requested the action 
+ action : Ext.form.Action 
The Action class. If an Ajax error ocurred, the failure type will be in failureType. The result property of this object may be examined to perform custom postprocessing. 

o scope : Object 

The scope in which to call the callback functions (The this reference for the callback functions). 
o clientValidation : Boolean 

Submit Action only. Determines whether a Form's fields are validated in a final call to isValid prior to submission. Set to false to prevent this. If undefined, pre-submission field validation is performed. 

Returns: 

* BasicForm 
this 

 

其实关于表单提交操作并没有结束, 从doAction方法的描述中可以看出.. 这里实际上是调用了Ext.form.Action这个类, 而submit操作是调用了该类的子类Ext.form.Action.Submit...  绕了一大圈,终于把Ext中FormPanel是如何提交表单的原理搞的差不多了.. 那么下来就可以上代码了:

var winLogin = new Ext.Window({
	title:'登录',
	renderTo:Ext.getBody(),
	width:350,
	bodyStyle:'padding:15px;',
	id:'login-win',
	buttonAlign:'center',
	modal:true,
	items:[{
		xtype:'form',
		defaultType:'textfield',
		bodyStyle : 'padding:5px',
		baseCls : 'x-plaints',
		url:'ajaxLogin.do',
		method:'POST',
		defaults:{
			anchor:'95%',
			allowBlank:false
		},
		items:[{
			id:'loginName',
			name:'loginName',
			fieldLabel:'用户名',
			emptyText:'请输入用户名',
			blankText:'用户名不能为空'
		},{
			id:'password',
			name:'password',
			fieldLabel:'密码',
			blankText:'密码不能为空'
		}]							
	}],
	buttons:[{
		text:'登录',
		handler:function(){
			//获取表单对象
			var loginForm = this.ownerCt.findByType('form')[0].getForm();
			alert(loginForm.getValues().loginName);
			loginForm.doAction('submit', {
				url:'ajaxLogin.do',
				method:'POST',						
				waitMsg:'正在登陆...',
				timeout:10000,//10秒超时,
				params:loginForm.getValues(),//获取表单数据
				success:function(form, action){
					var isSuc = action.result.success;
					if(isSuc) {
						//提示用户登陆成功
						Ext.Msg.alert('消息', '登陆成功..');
					}										
				},
				failure:function(form, action){
					alert('登陆失败');
				}
			});
			this.ownerCt.close();
		}
	}, {
		text:'重置',
		handler:function(){
			alert('reset');
			this.ownerCt.findByType('form')[0].getForm().reset();
		}
	}]						
});
winLogin.show();

 

注意params:loginForm.getValues(),//获取表单数据的部分... 

这里是得到BaiscForm中所有表单元素中的值,并且已String/Object键值对的形式保存。。 该方法在api文档中的描述如下:

      BasicForm getValues API 写道

     

getValues( [Boolean asString] ) : String/Object 

Returns the fields in this form as an object with key/value pairs as they would be submitted using a standard form submit. If multiple fields exist with the same name they are returned as an array. 
Parameters: 

* asString : Boolean 
(optional) false to return the values as an object (defaults to returning as a string) 

Returns: 

* String/Object 

 

如此提交解决了提交表单时无法发送数据的问题.... 

到这里终于解决了 如何提交表单的问题...

 

 为什么没有执行submit中的success方法, failure方法是在什么时候会被执行..

    这里还是需要 查Action类中的success属性的API文档描述...

  

success : Function 

The function to call when a valid success return packet is recieved. The function is passed the following parameters: 

* form : Ext.form.BasicForm 
The form that requested the action 
* action : Ext.form.Action 
The Action class. The result property of this object may be examined to perform custom postprocessing. 

 

 这里 success方法需要两个参数, 尤其是第二个参数的描述: 尤其result, 这里是可以点击的

  点击后随即跳到了Action result属性的描述: 

  Action result属性 API 写道

result : Object 

The decoded response object containing a boolean success property and other, action-specific properties. 

 

有此描述可知,服务器返回的响应中需要包含一个 boolean 型的 success 字段, 该字段会保存在result中,Action会通过获取对该字段的描述 来判断是否执行 success 方法。。

那么服务器如何返回boolean型的success字段呢?   服务器段部分代码如下:

   

try {
	//返回成功标识
	<SPAN style="COLOR: #ff0000">response.getWriter().println("{success:true}");</SPAN>
	response.getWriter().flush();
} catch (IOException e) {
	e.printStackTrace();
} finally {
	try {
		response.getWriter().close();
	} catch (IOException e) {
		e.printStackTrace();
	}
}

 
      将按钮添加单击事件,执行以下方法 
      

   1.  function login(item) {   
   2.             
   3.             if (validatorForm()) {   
   4.                 // 登录时将登录按钮设为disabled,防止重复提交   
   5.                 this.disabled = true;   
   6.   
   7.                 // 第一个参数可以为submit和load   
   8.                 formPanl.form.doAction('submit', {   
   9.   
  10.                     url : 'user.do?method=login',   
  11.   
  12.                     method : 'post',   
  13.   
  14.                     // 如果有表单以外的其它参数,可以加在这里。我这里暂时为空,也可以将下面这句省略   
  15.                         params : '',   
  16.   
  17.                         // 第一个参数是传入该表单,第二个是Ext.form.Action对象用来取得服务器端传过来的json数据   
  18.                         success : function(form, action) {   
  19.   
  20.                             Ext.Msg.alert('操作', action.result.data);   
  21.                             this.disabled = false;   
  22.   
  23.                         },   
  24.                         failure : function(form, action) {   
  25.   
  26.                             Ext.Msg.alert('警告', '用户名或密码错误!');   
  27.                             // 登录失败,将提交按钮重新设为可操作   
  28.                             this.disabled = false;   
  29.   
  30.                         }   
  31.                     });   
  32.                 this.disabled = false;   
  33.             }   
  34.         }  
 

 

 

2. EXT表单的非ajax提交 
    在表单需加入下列代码

   

   1. //实现非AJAX提交表单一定要加下面的两行! onSubmit : Ext.emptyFn, submit : function() {    
   2. //再次设定action的地址    
   3. this.getEl().dom.action ='user.do?method=login'; this.getEl().dom.method = 'post';    
   4. //提交submit   
   5.  this.getEl().dom.submit();   
   6. },   

  

3.EXT的ajax提交

  

   1.   
   2.   
   3. Ext.Ajax.request({   
   4.                                         //请求地址   
   5.                      url: 'login.do',   
   6.                      //提交参数组   
   7.                      params: {   
   8.                          LoginName:Ext.get('LoginName').dom.value,   
   9.                          LoginPassword:Ext.get('LoginPassword').dom.value   
  10.                      },   
  11.                      //成功时回调   
  12.                      success: function(response, options) {   
  13.                         //获取响应的json字符串   
  14.                        var responseArray = Ext.util.JSON.decode(response.responseText);                                                
  15.                             if(responseArray.success==true){   
  16.                                 Ext.Msg.alert('恭喜','您已成功登录!');       
  17.                             }   
  18.                             else{   
  19.                                 Ext.Msg.alert('失败','登录失败,请重新登录');       
  20.                             }   
  21.                     }   
  22.             });  

 

二、利用viewport布局左边区域系统菜单跳转两种方式

    1,使用Ext.get('centerPanel').load(url:"aaa.jsp");url为必选参数还有其他可选参数     请参见api文档。缺点,加入的页面js无效 
    2,使用iframe,具体
         Ext.get('centerPanel').dom.innerHTML='< i f r a m e  src=aaa.jsp>< / i f r a m e >';
       优 点可以在载入的页面动态加载js脚本(推荐使用)

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值