Java spring控制器中两个相同类型的对象';s方法(会话和非会话)非会话正在替换会话
我在Spring控制器中有一个方法,它接受两个类型相同的参数 其中一个来自会话,另一个来自表单提交(UI) 问题是,在控制器方法中,我的非会话对象正在替换会话对象。我不确定这是什么错误,或者我犯了一些愚蠢的错误 spring版本3.1.2 spring mvc/mvc web 3.1.0 控制器:Java spring控制器中两个相同类型的对象';s方法(会话和非会话)非会话正在替换会话,java,spring,session,spring-mvc,Java,Spring,Session,Spring Mvc,我在Spring控制器中有一个方法,它接受两个类型相同的参数 其中一个来自会话,另一个来自表单提交(UI) 问题是,在控制器方法中,我的非会话对象正在替换会话对象。我不确定这是什么错误,或者我犯了一些愚蠢的错误 spring版本3.1.2 spring mvc/mvc web 3.1.0 控制器: @Controller @RequestMapping("/tspadmin") @SessionAttributes({"currentUser"}) public class TSPAdminUs
@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{
密码:函数(val,字段){
if(field.initialPassField){
var pwd=field.up('form').down('#'+field.initialPassField);
返回(val==pwd.getValue());
}
返回true;
},
passwordText:'密码不匹配'
});
var create_user=Ext.create('Ext.form.Panel'{
扩展:'Ext.form.Formpanel',
autoScroll:是的,
标题:“创建用户”,
边界:错,
布局:{
类型:“hbox”,
对齐:“拉伸”
},
renderTo:document.body,
项目:[{
xtype:'面板',
边界:错,
保证金:'20',
宽度:“40%”,
默认值:{
宽度:300
},
项目:[
//comboboxs.tspNameCombo,
{
xtype:'textfield',
allowBlank:false,
fieldLabel:“用户名”,
名称:“用户名”,
宽度:350,
},
{
xtype:'textfield',
fieldLabel:“密码”,
名称:“密码”,
输入类型:“密码”,
正则表达式:/^(?=.[A-Z])(?=.[A-Z])(?=.[0-9])(?=.[#?!@$%^&*-])。{8,}$/,,
regexText:“错误密码长度至少应为8个字符,应为字母数字,必须至少包含一个特殊字符和一个大写字母。”,
验证器:函数(v){
返回/^(?=.[A-Z])(?=.[A-Z])(?=.[0-9])(?=.[#?!@$%^&*-])。{8,}$/.test(v)?true:“无效数字”;
},
id:'通行证'
},
{
xtype:'textfield',
fieldLabel:“确认密码”,
名称:“确认密码”,
vtype:“密码”,
输入类型:“密码”,
initialPassField:'pass'//初始密码字段的id
},
{
xtype:'textfield',
allowBlank:false,
fieldLabel:“电子邮件”,
名称:'电子邮件',
vtype:“电子邮件”,
宽度:350,
}, {
xtype:“textfield”,
allowBlank:是的,
正则表达式:/^[0-9\+\-\+$/,
fieldLabel:'电话号码',
名称:“电话1”,
regexText:“错误输入的数字无效。”,
验证器:函数(v){
return/^[0-9\+\-\+$/.test(v)?true:“无效数字”;
}
},
{
xtype:“textfield”,
allowBlank:false,
正则表达式:/^[0-9\+\-\+$/,
fieldLabel:“手机号码”,
姓名:‘电话2’,
regexText:“错误输入的数字无效。”,
验证器:函数(v){
return/^[0-9\+\-\+$/.test(v)?true:“无效数字”;
}
},
]
}],
按钮符号:'左',
按钮:[{
文本:“保存”,
保证金:'20',
处理程序:函数(){
var form=create_user.getForm();
提交表格({
url:'tspadmin/createUser',
成功:功能(形式、行动){
Ext.Msg.alert('Success',action.result.message,function()){
location.reload();
});
},
失败:功能(形式、动作){
Ext.Msg.alert('Failure',action.result.message);
}
});
}
}, {
文本:“取消”,
保证金:'20',
处理程序:函数(){
this.up('form').getForm().reset();
}
},
]
});
来自文档
SessionAttributes:“这通常会列出模型属性的名称,这些属性应该透明地存储在会话或一些会话存储中,用作表单支持bean”
你的代码工作正常,只是用错了东西
看看这个:
如果要存储并从会话中获取用户:
1) 创建保存用户信息的会话bean并将其注入控制器(使用aop:scoped proxy)
2) 将用户直接存储在会话中,并在控制器方法中添加HttpSession参数。
3) Spring Security有一些实用程序来存储登录的用户请发布视图(JSP)代码。您使用的是弹簧式TLD吗?如果将表单元素绑定到模型u,则sud的“user”对象也作为零件模型(@modeldattribute(“user”)user)。我假设您新创建的用户数据是从表单填充的。我使用Extjs作为视图部分,不将表单元素绑定到模型。我理解,但问题不在于获取或设置会话属性。。。。这很好。事实上,我的非会话对象(用户)来自UI,它正在替换会话对象(currentUser)。您如何告诉spring第一个参数(用户)应该来自会话?我猜对于这个参数,spring会尝试注入一个具有相同类型的bean,即来自model属性的用户。
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();
}
},
]
});