Action层代码封装模板

来源:互联网 发布:淘宝店招设计素材 编辑:程序博客网 时间:2024/06/05 06:19

模板action

package cn.jxlg.oa.util;

import java.lang.reflect.ParameterizedType;

import javax.annotation.Resource;

import cn.jxlg.oa.domain.User;
import cn.jxlg.oa.service.DepartmentService;
import cn.jxlg.oa.service.ForumService;
import cn.jxlg.oa.service.PrivilegeService;
import cn.jxlg.oa.service.ReplyService;
import cn.jxlg.oa.service.RoleService;
import cn.jxlg.oa.service.TopicService;
import cn.jxlg.oa.service.UserService;

import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;

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

    // =============== ModelDriven的支持 ==================

    protected T model;

    public BaseAction() {
        try {
            // 通过反射获取model的真实类型
            ParameterizedType pt = (ParameterizedType) this.getClass()
                    .getGenericSuperclass();
            Class<T> clazz = (Class<T>) pt.getActualTypeArguments()[0];
            // 通过反射创建model的实例
            model = clazz.newInstance();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    public T getModel() {
        return model;
    }

    // =============== Service实例的声明 ==================
    @Resource
    protected RoleService roleService;
    @Resource
    protected DepartmentService departmentService;
    @Resource
    protected UserService userService;
    @Resource
    protected PrivilegeService privilegeService;

    @Resource
    protected ForumService forumService;
    @Resource
    protected TopicService topicService;
    @Resource
    protected ReplyService replyService;

    /**
     * 获取当前登录的用户
     *
     * @return
     */
    protected User getCurrentUser() {
        return (User) ActionContext.getContext().getSession().get("user");
    }

    // ============== 分页用的参数 =============

    protected int pageNum = 1; // 当前页
    protected int pageSize = 10; // 每页显示多少条记录

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }
}


使用案例

public class ForumAction extends BaseAction {}

0 0
原创粉丝点击