日期:2014-05-16  浏览次数:20389 次

Extjs+Struts2 Ajax 方式提交数据

前台Ext 代码

?

artist: {
            xtype: 'panel', 
            title:'登录',  
            width:350,  
            id:'login-panel',  
            items:[{  
                xtype:'form',  
                id:'artist-form',  
                defaultType:'textfield',  
                bodyStyle : 'padding:5px',  
                method:'POST',  
                defaults:{  
                    anchor:'95%',  
                },  
                items:[{  
                    id:'loginName',  
                    name:'loginName',  
                    fieldLabel:'用户名',   
                },{  
                    id:'password',  
                    name:'password',  
                    fieldLabel:'密码',  
                }] 
            }],
            
            buttons: [{
                text: 'Save',
                handler: function(){
            
            var loginForm = Ext.getCmp('login-form').form; 
            loginForm.doAction('submit', {  
                    url:'login.action',  
                    method:'POST',                        
                    waitMsg:'正在登陆...',  
                    timeout:10000,//10秒超时,  
                    //params:loginForm.getValues(),
                    success:function(form, action){  
            alert('登陆成功'+action.result)
                        var isSuc = action.result.success;
                        if(isSuc) {  
                            //提示用户登陆成功  
                            Ext.Msg.alert('消息', '登陆成功..');  
                        }                                         
                    },  
                    failure:function(form, action){  
                        alert('登陆失败');  
                    }  
                });  
                }
            }
    };

?
struts.xml

?

<package name="login" extends="json-default">
        <action name="login" class="com.junjun.television.web.LoginAction">
            <result type="json" /> 
        </action>
        
    </package>

?LoginAction.java

?

public String execute() {

		Watch watch = new Watch();
		watch.setNum(1);
		watch.setOnLine("asdfasdf");

		JSONObject jo = JSONObject.fromObject(watch);
		String jsonStr = jo.toString();
		System.out.println(jsonStr);

		try {
			// 返回成功标识
			response.getWriter().println("{success:true}");
			response.getWriter().println("{watch:"+jsonStr+"}");
			response.getWriter().flush();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				response.getWriter().close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		return null;

	}

?java转json格式需要的jar包如下

? ?commons-beanutils.jar
???commons-collections-3.2.jar
???commons-lang-2.3.jar
???commons-logging.jar
???ezmorph-1.0.6.jar
???json-lib-2.4-jdk15.jar

?