struts2 best practice-pitfall in model driven action

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;
}


你可能感兴趣的:(struts2)