抽象BaseAction对进行ModelDriven实现

利用Java反射技术对ModelDriven实现抽象BaseAction,其代码如下:

public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T>{

	@Resource
	protected RoleService roleService;
	@Resource
	protected DepartmentService departmentService;
	@Resource
	protected UserService userService;
	@Resource
	protected PrivilegeService privilegeService;

	protected T model;

	public BaseAction() {
		try {
			// 得到model的类型信息
			ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
			Class clazz = (Class) pt.getActualTypeArguments()[0];

			// 通过反射生成model的实例
			model = (T) clazz.newInstance();
		} catch (Exception e) {
			throw new RuntimeException(e);
		}
	}

	public T getModel() {
		return model;
	}
}

猜你喜欢

转载自free0007.iteye.com/blog/1944830