码迷,mamicode.com
首页 > 其他好文 > 详细

struts2的BaseAction<T>继承ActionSupport实现ModelDriven<T>

时间:2014-09-09 15:20:58      阅读:193      评论:0      收藏:0      [点我收藏+]

标签:style   blog   color   io   ar   for   art   div   cti   

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

    private static final long                serialVersionUID    = 1L;
    protected T                                model;
    //页面表单映射到model相当于struts1的formbean

    //将所有的service写入到BaseAction中
    @Resource protected RoleService            roleService;
    @Resource protected DepartmentService    departmentService;
    @Resource protected UserService            userService;
    @Resource protected PrivilegeService    privilegeService;

    // 页码默认为第1页
    protected int                            pageNum                = 1;

    @SuppressWarnings({ "rawtypes", "unchecked" })
    public BaseAction() {
        try {
            // 得到model的类型信息
            ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
            Class clazz = (Class) type.getActualTypeArguments()[0];
            // 通过反射生成model的实例
            model = (T) clazz.newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }

    }

    public T getModel() {
        return model;
    }
}

 

struts2的BaseAction<T>继承ActionSupport实现ModelDriven<T>

标签:style   blog   color   io   ar   for   art   div   cti   

原文地址:http://www.cnblogs.com/q446223455/p/3962316.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!