若没有实现ModelDriven的接口,Controll的代码会比较冗余,不能实现Controll和Model代码的分离
下面是没有实现ModelDriven接口的SuggestAction
1 package dayday.controll;
2
3 import com.opensymphony.xwork2.ActionSupport;
4 import com.opensymphony.xwork2.ModelDriven;
5 import dayday.model.Product;
6 import dayday.model.Suggest;
7 import dayday.service.AddSuggest;
8
9 /**
10 * Created by I am master on 2017/2/22.
11 */
12 public class SuggestAction extends ActionSupport{
13 //保存对象的信息
14 private Suggest suggest;
15 private AddSuggest addSuggest;
16
17 private String sugName;
18 private String sugEmail;
19 private String sugPhone;
20 private String sugContent;
21
22 public String getSugName() {
23 return sugName;
24 }
25
26 public void setSugName(String sugName) {
27 this.sugName = sugName;
28 }
29
30 public String getSugEmail() {
31 return sugEmail;
32 }
33
34 public void setSugEmail(String sugEmail) {
35 this.sugEmail = sugEmail;
36 }
37
38 public String getSugPhone() {
39 return sugPhone;
40 }
41
42 public void setSugPhone(String sugPhone) {
43 this.sugPhone = sugPhone;
44 }
45
46 public String getSugContent() {
47 return sugContent;
48 }
49
50 public void setSugContent(String sugContent) {
51 this.sugContent = sugContent;
52 }
53
54 @Override
55 public String execute()throws Exception{
56 suggest.setSugContent(getSugContent());
57 suggest.setSugEmail(getSugEmail());
58 suggest.setSugName(getSugName());
59 suggest.setSugPhone(getSugPhone());
60 addSuggest.insertSuggest(suggest);
61 return SUCCESS;
62 }
63 }
实现了ModelDriven接口的SuggestAction接口以后的代码
1 package dayday.controll; 2 3 import com.opensymphony.xwork2.ActionSupport; 4 import com.opensymphony.xwork2.ModelDriven; 5 import dayday.model.Product; 6 import dayday.model.Suggest; 7 import dayday.service.AddSuggest; 8 9 /** 10 * Created by I am master on 2017/2/22. 11 */ 12 public class SuggestAction extends ActionSupport implements ModelDriven<Suggest>{ 13 //保存对象的信息 14 private Suggest suggest; 15 private AddSuggest addSuggest; 16 17 18 @Override 19 public String execute()throws Exception{ 20 addSuggest.insertSuggest(suggest); 21 return SUCCESS; 22 } 23 24 @Override 25 public Suggest getModel() { 26 suggest=new Suggest(); 27 return suggest; 28 } 29 }
明显代码量减少了很多,实现了分离
Action实现ModelDriven接口运行的原理
1 1). 先会执行 ModelDrivenInterceptor 的 intercept 方法. 2 3 public String intercept(ActionInvocation invocation) throws Exception { 4 //获取 Action 对象: SuggestAction 对象, 此时该 Action 已经实现了 ModelDriven 接口 5 //public class SuggestAction extends ActionSupport, implements ModelDriven<Employee> 6 Object action = invocation.getAction(); 7 8 //判断 action 是否是 ModelDriven 的实例 9 if (action instanceof ModelDriven) { 10 //强制转换为 ModelDriven 类型 11 ModelDriven modelDriven = (ModelDriven) action; 12 //获取值栈 13 ValueStack stack = invocation.getStack(); 14 //调用 ModelDriven 接口的 getModel() 方法 15 //即调用 EmployeeAction 的 getModel() 方法 16 /* 17 public Employee getModel() { 18 employee = new Employee(); 19 return employee; 20 } 21 */ 22 Object model = modelDriven.getModel(); 23 if (model != null) { 24 //把 getModel() 方法的返回值压入到值栈的栈顶. 实际压入的是 EmployeeAction 的 employee 成员变量 25 stack.push(model); 26 } 27 if (refreshModelBeforeResult) { 28 invocation.addPreResultListener(new RefreshModelBeforeResult(modelDriven, model)); 29 } 30 } 31 return invocation.invoke(); 32 } 33 34 2). 执行 ParametersInterceptor 的 intercept 方法: 把请求参数的值赋给栈顶对象对应的属性. 若栈顶对象没有对应的属性, 则查询 35 值栈中下一个对象对应的属性... 36 37 3). 注意: getModel 方法不能提供以下实现. 的确会返回一个 Suggest 对象到值栈的栈顶. 但当前 Action 38 的 suggest 成员变量却是 null. 39 40 public Suggest getModel() { 41 return new Suggest(); 42 }