BaseAction
package com.itheima.web.action.base;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
/**
* 表现层通用实现
* @author Administrator
*
* @param <T>
*/
public class BaseAction<T> extends ActionSupport implements ModelDriven<T> {
private T model;
//model相当于new User
//actualTypeArguments[0];相当于获得User.class
public BaseAction() {
ParameterizedType genericSuperclass = (ParameterizedType)this.getClass().getGenericSuperclass();
//获得BaseAction上声明的泛型数组
Type[] actualTypeArguments = genericSuperclass.getActualTypeArguments();
Class<T> entityClass=(Class<T>) actualTypeArguments[0];
try {
//通过反射创建实例对象
model=entityClass.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
public T getModel() {
return model;
}
}