【发布时间】: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