为什么action中的message不能以json数据形式返回到extjs中,每次alert的时候都是空的,通过firebug也只能看到返回一个boolean值和name、password的json数据,message没有返回。我想知道struts2的action是如何和前台extjs交互数据的。
Struts2 Action类:
public class LoginAction extends BaseAction { private static final long serialVersionUID = 1640171742181587847L;
private String username = "";
private String password = "";
private String msg = "";
private boolean success = false; public String execute() {
String sql = "";
sql = "from AdUserLogin where name='" + username + "' and password='"
+ password + "'";
success = ManagerFactory.getInstance().getCommonDaoIMP().isExist(sql);
if (success) {
this.success = true;
this.msg = "登录成功";
} else {
this.msg = "登录失败";
}
return SUCCESS;
} public String getUsername() {
return username;
} public void setUsername(String username) {
this.username = username;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
} public boolean isSuccess() {
return success;
} public void setSuccess(boolean success) {
this.success = success;
} public String getMsg() {
return msg;
} public void setMsg(String message) {
this.msg = message;
}}extjs4.0代码:
App.LoginDialog = function() {
    return {
        getForm: function() {
            var form = new Ext.form.Panel({
//url:'/login.action',
                labelWidth: 80,
                buttonAlign: 'center',
                bodyStyle: 'padding:20px;',
                frame: true,
                defaultType: 'textfield',
                defaults: {
                    enableKeyEvents: true,
                    allowBlank: false
                },
                items: [{
                    name: 'username',
                    fieldLabel: '用户名称',
                    maxLength:30,
                    listeners: {
                        scope: this,
                        keypress: function(field, e) {
                            if (e.getKey() == 13) {
                                var passwordField = form.getForm().findField('password');
                                if (passwordField) {
                                    passwordField.focus();
                                }
                            }
                        }
                    }
                }, {
                    name: 'password',
                    fieldLabel: '用户密码',
                    inputType: 'password',
                    maxLength:30,
                    listeners: {
                        scope: this,
                        keypress: function(field, e) {
                            if (e.getKey() == 13) {
                                var captchaField = form.getForm().findField('captcha');
                                if (captchaField) {
                                    captchaField.focus();
                                }
                            }
                        }
                    }
                }],
                buttons: [{
                    text: '确定',
                    scope: this,
                    handler: function() {
                        this.submit();
                    }
                }, {
                    text: '重置',
                    scope: this,
                    handler: function() {
                        form.getForm().reset()
                    }
                }]
            });
            return form;
        },        getDialog: function() {
            var form = this.getForm();
            var dlg = Ext.create('widget.window',{
                height: 150,
         width: 400,
         //x: 530,
         //y: 290,
                title: 'KMatrix-Administration System',
                plain: true,
                closable: false,
                resizable: false,
                frame: false,
                layout: 'fit',
                closeAction: 'hide',
                border: false,
                draggable:false,//不能拖拽
                modal: true,//模式窗口
                items: [form]
            });
            this.form = form;
            return dlg;
        },        submit: function() {
            if (this.form.getForm().isValid()) {
var myMask = new Ext.LoadMask(Ext.getBody(), {msg:"正在验证用户名和密码,请稍等..."});
myMask.show();
                this.form.getForm().submit({
     scope:this,
method:'post',
params:"",
url : 'login.action',
     success: function(form, action){
     myMask.hide();
     document.location = 'main.jsp';
// var isSuccess = action.result.success;
     var info = action.result.msg;
     Ext.Msg.alert('提示', info);
// if(isSuccess=='true') {
// Ext.Msg.alert('提示', info);
// window.location = 'main.jsp';
// }else {
// Ext.Msg.alert('提示', info);
// }
     },
     failure: function(form, action){
myMask.hide();
var info = action.result.msg;
     Ext.Msg.alert('提示', info);
}
     })
            }
        },        show: function() {
            if (!this.dlg) {
                this.dlg = this.getDialog();
            }
            this.dlg.show();
        }
    };
}();
struts2配置文件:
<struts>
<package name="com.kmatrix.action" extends="json-default">
<action name="login" class="com.kmatrix.action.LoginAction">
<result type="json"></result>
</action>
</package>
</struts>

解决方案 »

  1.   

    action 通过 response 返回的json 数组回调给页面的~~
      

  2.   

    那为什么我这里的message不能alert出来?
      

  3.   


    通过firebug也只能看到返回一个boolean值和name、password的json数据,message没有返回

    你看到的不是返回值,是你传入的值和执行结果。
    建议你使用response把数据写出去response.setHeader("Cache-Control", "no-cache"); 
    response.setContentType("text/json;charset=UTF-8");
    String jsonString = "{success:"+success +",msgs:'操作已执行'}";
    response.getWriter().write(jsonString);   action的方法不用返回值,页面接受的话,直接就用json方式读取。success : function(data) {
    var  jsondata = Ext.util.JSON.decode(data.responseText);
         if (jsondata.success) {
      Ext.Msg.alert(jsondata.msgs);
         }else{
     Ext.Msg.alert('系统提示', "传入数据异常,请稍后再试!");
         }
    }
      

  4.   

    必须要这样吗?
    String jsonString = "{success:"+success +",msgs:'操作已执行'}";
    response.getWriter().write(jsonString);  如我这个里面的message不能通过action返回得到吗?