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

本文介绍了一个Struts2框架下通用Action基类的设计思路及实现代码。该基类实现了ModelDriven接口,用于页面表单数据与模型对象之间的映射,并集成了多种服务接口如RoleService、DepartmentService等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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;
    }
}

 

转载于:https://www.cnblogs.com/sallet/p/3962316.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值