每个action都用到的存储代码,抽取出来
package org.com.cay.action;
import java.util.Map;
import org.apache.struts2.interceptor.ApplicationAware;
import org.apache.struts2.interceptor.RequestAware;
import org.apache.struts2.interceptor.SessionAware;
import com.opensymphony.xwork2.ActionSupport;
public class BaseActions extends ActionSupport implements
RequestAware,SessionAware,ApplicationAware, ModelDriven<T>{
private static final long serialVersionUID = 1L;
protected HttpServletRequest req = ServletActionContext.getRequest();
protected HttpServletResponse response = ServletActionContext.getResponse();
protected Map<String, Object> request;
protected Map<String, Object> session;
protected Map<String, Object> application;
protected T model;
@Override
public void setApplication(Map<String, Object> application) {
this.application = application;
}
@Override
public void setSession(Map<String, Object> session) {
this.session = session;
}
@Override
public void setRequest(Map<String, Object> request) {
this.request = request;
}
@Override
public T getModel() {
ParameterizedType pt = (ParameterizedType)this.getClass().getGenericSuperclass(); // 获取泛型父类
Class clazz = (Class<T>) pt.getActualTypeArguments()[0]; // 获取第一个参数的真实类型
try {
model = (T)clazz.newInstance();
} catch (Exception e) {
throw new RuntimeException(e);
}
return model;
}
}
转载于:https://blog.51cto.com/zhuws/1968624