1.忘记了好像前几篇没写BaseAction,这个Action的改进是为了节省开发的代码,将Action中公共的代码提取出来,系统各自的Action只需要继承BaseAction即可代码如下:
package com.icss.oa.base;
import java.lang.reflect.ParameterizedType;
import javax.annotation.Resource;
import com.icss.oa.domain.Department;
import com.icss.oa.domain.User;
import com.icss.oa.service.DepartmentService;
import com.icss.oa.service.RoleService;
import com.icss.oa.service.UserService;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
public class BaseAction<T> extends ActionSupport implements ModelDriven<T> {
@Resource
protected RoleService roleService;
@Resource
protected DepartmentService departmentService ;
@Resource
protected UserService userService ;
protected T model ;
public BaseAction() {
ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
Class clazz = (Class) pt.getActualTypeArguments()[0];
try {
model = (T) clazz.newInstance();
} catch (InstantiationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IllegalAccessException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public T getModel() {
// TODO Auto-generated method stub
return model;
}
}
里面需要注意的有:泛型的使用?如何获取带参数类型的类?如何获得泛型的实例?以及如何返回model?注意注解不能忘记,这里使用了构造方法,因为构造方法是肯定需要执行的,且执行一次
需要将各个持久层实体的servcie层注入进来