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

ModelDriven接口封装数据

时间:2018-11-08 21:58:25      阅读:250      评论:0      收藏:0      [点我收藏+]

标签:tac   base   抽象   res   role   row   override   zed   todo   

代码展示:

public abstract class BaseAction<T> extends ActionSupport implements ModelDriven<T> {
    
    @Resource
    protected DepartmentService departmentService;
    
    @Resource
    protected UserService  userService;
    
    @Resource
    protected RoleService roleService;
    
    private Class<T> modelClass=null;
    
    
    protected T model=null;
    
    public BaseAction() {
        ParameterizedType pt = (ParameterizedType) this.getClass().getGenericSuperclass();
        modelClass = (Class<T>) pt.getActualTypeArguments()[0];
        try {
            model=modelClass.newInstance();
        } catch (Exception e) {
            throw new RuntimeException();
        }
        System.out.println("---> clazz = " + modelClass);
    }

    
    @Override
    public T getModel() {
        // TODO Auto-generated method stub
        return model;
    }

总结:对于公共模块,或者是一些业务类对象的注入,我们可以创建一个baseAction的基础抽象类,让基础抽象类去实现ModelDriven,实现这个接口 具体代码:

implements ModelDriven<T>

,它会自动将前端传过来的数据封装成model对象,我们控制层调用数据的时候,可以直接从封装之后的对象中获取想要的属性字段

注意:封装成这个model对象的时候,会出现前端传递过来的字段为null;我们这个时候想要在struts2的action类中编写字段和提供字段的get和set方法类接收参数时,就不需要编写了,直接从model封装对象中获取数据,直接将获取的数据即可;


 

ModelDriven接口封装数据

标签:tac   base   抽象   res   role   row   override   zed   todo   

原文地址:https://www.cnblogs.com/li-xin123456/p/9931961.html

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