WebWork中的模型驱动问题注意

WebWork中的模型驱动问题注意
// Example 1: This is wrong:
public   class  ViewHeadlineAction  implements  Action, ModelDriven  {
Headline headline 
= new Headline();
public Object getModel() {
return this.headline ;
}

// 
public String execute() {
headline 
= headlineFactory.findLatest();
return SUCCESS;
}

}

b
// Example 2: This is correct:
public   class  ViewHeadlineAction  implements  Action, ModelDriven  {
Headline headline 
= new Headline();
public Object getModel() {
return this.headline ;
}

// 
public String execute() {
headlineFactory.updateToLatest(headline);
return SUCCESS;
}

}

Example 1 attempts to change the instance that getModel() returns. However,
because getModel() is called before execute() and before the object is pushed
onto the value stack, the reference that WebWork uses is the original instance, created
by new Headline(), not the instance loaded from the HeadlineFactory. The
second example resolves this issue by updating the existing instance rather than
having headline point to a new instance.

---------------------------------------------------------
专注移动开发
Android, Windows Mobile, iPhone, J2ME, BlackBerry, Symbian

你可能感兴趣的:(WebWork中的模型驱动问题注意)