struts2 best practice-pitfall in model driven action

本文探讨了在使用Struts框架的ModelDriven特性时容易忽视的一个陷阱:当在执行过程中改变了模型对象引用所指向的对象时,会导致模型与框架持有的模型不同步,从而引发数据不一致的问题。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

We should note one pitfall to avoid. By the time the execute() method of your ModelDriven action has been invoked, the framework has obtained a reference to your model object, which it’ll use throughout the request. Since the framework acquires its reference from your getter, it won’t be aware if you change the model field internally in your action. This can cause some data inconsistency problems. If, during your execution code, you change the object to which your model field reference points, your action’s model will then be out of sync with the one still held by the framework. The following code snippet demonstrates the problem:
public String execute(){
user = new User();
user.setSomething();
getPortfolioService().createAccount( user );
return SUCCESS;
}
private User user = new User();
public Object getModel() {
return user;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值