【问题标题】:Two object of same type in spring controller's method (session and non session) non session is replacing sessionspring 控制器方法中的两个相同类型的对象(会话和非会话)非会话正在替换会话
【发布时间】:2014-01-18 08:53:30
【问题描述】:

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

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

春季版 3.1.2 spring 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();
    }
},

]


});

【问题讨论】:

  • 请发布视图(JSP)代码。你在使用 Spring Form tlds 吗?如果您将表单元素绑定到模型,那么您的“用户”对象也作为模型的一部分(@ModelAttribute("user") User user )。我假设您新创建的用户数据是从表单中填充的。
  • 我使用 Extjs 作为视图部分,而不是将表单元素绑定到模型。

标签: java spring session spring-mvc


【解决方案1】:

来自文档 会话属性:"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 有一些实用程序来存储登录的用户

【讨论】:

  • 我很感激,但问题不在于获取或设置会话属性......这很好。事实是为什么我来自 UI 的非会话对象(用户)正在替换会话对象(当前用户)。
  • 你如何告诉 spring 第一个参数(用户)应该来自会话?我猜想对于这个参数spring会尝试注入一个相同类型的bean,也就是model属性中的user。
猜你喜欢
  • 2011-11-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-03-19
  • 2013-08-26
  • 1970-01-01
  • 2013-12-01
  • 2020-01-13
相关资源
最近更新 更多