BaseAction编写:免去一些重复的代码,比如继承ActionSupport和实现ModelDriven接口
1.BaseAction
package com.learning.crm.base; import java.lang.reflect.ParameterizedType; import java.lang.reflect.Type; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.ActionSupport; import com.opensymphony.xwork2.ModelDriven; public class BaseAction<T> extends ActionSupport implements ModelDriven<T>{ private T t;//必须进行实例化 private Class<?> clazz; //初始化得到T public BaseAction() throws Exception { super(); ParameterizedType parameterizedType= (ParameterizedType) this.getClass().getGenericSuperclass(); clazz= (Class<?>) parameterizedType.getActualTypeArguments()[0]; t=(T) clazz.newInstance(); } @Override public T getModel() { return t; } //...这里省略service注入;; //一些共有的操作 /** * 1.压栈 * @param object */ public void push(Object object) { ActionContext.getContext().getValueStack().push(object);; } /** * 2.存入actionContext * @param key * @param object */ public void put(String key,Object object) { ActionContext.getContext().put(key,object);; } }
2.使用时只需继承BaseAction