弹簧控制器方法中的两个相同类型的对象(会话和非会话)非会话正在替换会话



>我在 Spring 控制器中有一个方法,它接受两个相同类型的参数其中一个来自会话,另一个来自表单提交 (UI)。

问题是在控制器方法中,我的非会话对象正在替换我的会话对象。我不确定这是一些错误,或者我犯了一些愚蠢的错误。

春季版本 3.1.2春季 MVC/MVC-Web 3.1.0

控制器:

@Controller
@RequestMapping("/tspadmin")
@SessionAttributes({"currentUser"})
public class TSPAdminUserController {
@Autowired
private TSPAdminUserService tspAdminUserService;
@RequestMapping(value = "/createUser")
public @ResponseBody Object createUser(User user,@ModelAttribute("currentUser")   User currentUser){
             //Here user(from UI) object is replacing currentUser
    //processing
}
}

我这样做是因为这里一个用户正在创建另一个用户,我想在持久之前将某个会话用户的属性设置为另一个用户。

视图:

    Ext.apply(Ext.form.field.VTypes, {
    password: function(val, field) {
        if (field.initialPassField) {
            var pwd = field.up('form').down('#' + field.initialPassField);
            return (val == pwd.getValue());
        }
        return true;
    },
    passwordText: 'Passwords does not not match'
});

var create_user = Ext.create('Ext.form.Panel', {
extend:'Ext.form.Formpanel',
autoScroll: true,
title: 'Create User',
border:false,
layout: {
    type: 'hbox',
    align: 'stretch'
},
renderTo: document.body,
items : [ {
    xtype : 'panel',
    border : false,
    margin : '20 20 20 20',
    width : '40%',
    defaults : {
        width : 300
    },
    items : [
        //comboboxs.tspNameCombo,
        {
            xtype : 'textfield',
            allowBlank : false,
            fieldLabel : 'User Name',
            name : 'userName',
            width : 350,
        },
        {
            xtype:'textfield',
            fieldLabel: 'Password',
            name: 'password',
            inputType: 'password',
            regex: /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$/,
              regexText: "<b>Error</b></br>Password should be of minimum 8 character length, it should be alphanumeric ,must contain at least one special character,one Upper case letter.",
             validator: function(v) {
             return  /^(?=.*?[A-Z])(?=.*?[a-z])(?=.*?[0-9])(?=.*?[#?!@$%^&*-]).{8,}$/.test(v)?true:"Invalid Number";
             },
            id: 'pass'
        },
        {
            xtype:'textfield',
            fieldLabel: 'Confirm Password',
            name: 'confirmPassword',
            vtype: 'password',
            inputType: 'password',
            initialPassField: 'pass' // id of the initial password field
        },
        {
            xtype : 'textfield',
            allowBlank : false,
            fieldLabel : 'Email',
            name : 'email',
            vtype : 'email',
            width : 350,
        },  {
            xtype:"textfield",
            allowBlank : true,
            regex: /^[0-9+-_]+$/,
            fieldLabel: 'Telephone Number',
            name: 'telephone1',
            regexText: "<b>Error</b></br>Invalid Number entered.",
            validator: function(v) {
                return /^[0-9+-_]+$/.test(v)?true:"Invalid Number";
            }
        },
        {
            xtype:"textfield",
            allowBlank : false,
            regex: /^[0-9+-_]+$/,
            fieldLabel: 'Mobile Number',
            name: 'telephone2',
            regexText: "<b>Error</b></br>Invalid Number entered.",
            validator: function(v) {
                return /^[0-9+-_]+$/.test(v)?true:"Invalid Number";
            }
        },
    ]
}],
buttonAlign:'left',
buttons: [{
    text: 'Save',
    margin : '20 20 20 20',
    handler: function () {
        var form = create_user.getForm();
        form.submit({
            url:'tspadmin/createUser',
            success : function(form, action) {
                Ext.Msg.alert('Success',action.result.message,function(){
                    location.reload();
                });
            },
            failure : function(form, action) {
                Ext.Msg.alert('Failure',action.result.message);
            }
        });
    }
}, {
    text: 'Cancel',
    margin:'20 20 20 20',
    handler: function() {
        this.up('form').getForm().reset();
    }
},
]

});

来自文档会话属性:"This will typically list the names of model attributes which should be transparently stored in the session or some conversational storage, serving as form-backing beans" 你的代码工作正常,你只是使用了错误的东西。

看看这个: http://www.intertech.com/Blog/understanding-spring-mvc-model-and-session-attributes/

如果要存储用户并从会话中获取用户:1)创建保存用户信息的会话bean并将其注入控制器(使用aop:scoped-proxy)2) 将用户直接存储在会话中,并在控制器方法中添加 HttpSession 参数。3)Spring Security有一些实用程序来存储登录的用户

最新更新