有些Action类不代表任何Model对象,他们得功能仅限于提供显示服务,也就是控制单元。如果Action实现了
ModelDriven接口,那么该拦截器会将getModel()方法返回对象置于栈顶。
对应Aciton类,实现了ModelDRiven类
public class EmployeeAction implements RequestAware, ModelDriven<Employee>, Preparable{
private Dao dao = new Dao();
/**
* 使用ModelDriven而不在Aciton中声明employee各个对象
*/
private Employee employee;
public String update(){
dao.update(employee);
return "success";
}
public void prepareUpdate(){
employee = new Employee();
}
public String edit(){
return "edit";
}
public void prepareEdit(){
employee = dao.get(employeeId);
}
public String save(){
dao.save(employee);
return "success";
}
public void prepareSave(){
employee = new Employee();
}
public String delete(){
dao.delete(employeeId);
return "success";
}
@Override
public Employee getModel() {
return employee;
}
}
ModelDriven源码部分
@Override
public String intercept(ActionInvocation invocation) throws Exception {
Object action = invocation.getAction();
if (action instanceof ModelDriven) {
ModelDriven modelDriven = (ModelDriven) action;
ValueStack stack = invocation.getStack();
Object model = modelDriven.getModel();
if (model != null) {
stack.push(model);
}
if (refreshModelBeforeResult) {
invocation.addPreResultListener(new RefreshModelBeforeResult(modelDriven, model));
}
}
return invocation.invoke();
}