EXT JS 5 / Spring MVC:如何使用spring mvc将ext js表单绑定到java对象

时间:2014-08-26 13:43:12

标签: java spring forms spring-mvc extjs

我想用对象Java绑定我的ext js表单。

现在我可以像这样单独绑定每个字段:

我的表格:

Ext.define('MyApp.view.login.Login', {
    extend: 'Ext.window.Window',
    alias: 'widget.login',

    requires : ['Ext.form.Panel', 'Ext.layout.container.Fit'],

    autoShow: true,
    height: 200,
    width: 400,
    layout: 'fit',
    iconCls: 'key',
    title: "Login",
    closeAction: 'hide',
    closable: false,
    draggable: false,
    resizable: false,

    items: [
        {
            xtype: 'form',
            frame: false,
            bodyPadding: 15,
            layout: 'anchor',
            defaults: {
                anchor: '100%',
                labelWidth: 100
            },

            defaultType: 'textfield',
            items: [
                {
                    name: 'user',
                    fieldLabel: "Login ",
                    allowBlank: false,
                    msgTarget: 'under',
                    emptyText: 'Login',
                    maxLength: 25
                },
                {
                    inputType: 'password',
                    name: 'password',
                    fieldLabel: "Password",
                    emptyText:'Password',
                    allowBlank: false,
                    msgTarget: 'under',
                    maxLength: 15,
                    enableKeyEvents: true,
                    id: 'login-password'
                }
            ],

            dockedItems: [
                {
                    xtype: 'toolbar',
                    dock: 'bottom',
                    items: [
                        {
                            xtype: 'tbfill'
                        },
                        {
                            xtype: 'button',
                            itemId: 'cancel',
                            text: 'Cancel'
                        },
                        {
                            xtype: 'button',
                            itemId: 'submit',
                            formBind: true,
                            text: "Submit"
                        }
                    ]
                }
            ]
        }
    ]
});

我提交方法的一部分:

     loginForm.submit({
        method : 'POST',
        url: '/myapp/login/login.sp',
        success: function(form, action) {
            Ext.Msg.alert('Success', action.result.msg);
        },
        failure: function(form, action) {
            switch (action.failureType) {
                case Ext.form.action.Action.CLIENT_INVALID:
                Ext.Msg.alert('Failure', 'Form fields may not be submitted with invalid values');
                break;
                case Ext.form.action.Action.CONNECT_FAILURE:
                Ext.Msg.alert('Failure', 'Ajax communication failed');
                break;
                case Ext.form.action.Action.SERVER_INVALID:
                Ext.Msg.alert('Failure', action.result.msg);
            }
        }

    });

我的Java控制器登录方法:

@RequestMapping(value="login/login", method= RequestMethod.POST)
public void checkConnection(@RequestParam(value="user", required = true) String user, @RequestParam(value="password", required = true) String password) {
    logger.info("LOGIN !!!");
}

我想要这样的事情:

@RequestMapping(value="login/login", method= RequestMethod.POST)
public void checkConnection(@Annotation LoginModel) {
    logger.info("LOGIN !!!");
}

我的LoginModel就像这样:

public class LoginModel {

    /** User */
    private String user;

    /** Password */
    private String password;

}

问候。

1 个答案:

答案 0 :(得分:1)

我相信@ModelAttribute就是你所需要的。它告诉Spring从模型中检索参数。

@RequestMapping(value="login/login", method= RequestMethod.POST)
public void checkConnection(@ModelAttribute LoginModel loginModel) {
    logger.info("LOGIN !!!");
}