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

被折叠的 条评论
为什么被折叠?



