- public class BaseAction<T> extends ActionSupport implements ModelDriven<T> {
- protected T model;
- public T getModel() {
- return model;
- }
- //在构造方法中动态获取实体类型,通过反射创建model对象
- public BaseAction() {
- //在构造方法中动态获取实体类型
- ParameterizedType genericSuperclass = (ParameterizedType) this.getClass().getGenericSuperclass();
- //获得baseAction上声明的泛型数组
- Type[] actualTypeArguments = genericSuperclass.getActualTypeArguments();
- Class<T> entityClass = (Class<T>) actualTypeArguments[0];
- //通过反射创建model对象
- try {
- model = entityClass.newInstance();
- } catch (InstantiationException e) {
- e.printStackTrace();
- } catch (IllegalAccessException e) {
- e.printStackTrace();
- }
- }
- }
在构造方法中动态获取实体类型 and 通过反射创建model对象
最新推荐文章于 2021-11-02 14:14:05 发布