public class BaseAction<T> extends ActionSupport implements ModelDriven<T> {
private T model;//模型对象
@Override
public T getModel() {
//获得直接超类对象
ParameterizedType genericSuperclass = (ParameterizedType) this.getClass().getGenericSuperclass();
//获取直接超类对象类型
Type[] actualTypeArguments = genericSuperclass.getActualTypeArguments();
//通过反射创建对象
Class<T> entityclass=(Class<T>) actualTypeArguments[0];
try {
model=entityclass.newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return model;
}
在构造方法中动态获取实体类型
最新推荐文章于 2023-02-24 11:15:57 发布