SSH公共action集成easyui分頁+模型驅動

package com.kayo.bos.web.action;

import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.Map;

import org.springframework.data.domain.Page;

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> {

    // 模型驅動
    protected T model;

    @Override
    public T getModel() {
        return model;
    }

    // 構造器 完成model實例化
    public BaseAction() {
        // 構造子類Action對象 ,獲取繼承父類型的泛型
        // AreaAction extends BaseAction<Area>
        // BaseAction<Area>
        Type genericSuperclass = this.getClass().getGenericSuperclass();
        // 獲取類型第一個泛型參數
        ParameterizedType parameterizedType = (ParameterizedType) genericSuperclass;
        Class<T> modelClass = (Class<T>) parameterizedType
                .getActualTypeArguments()[0];
        try {
            model = modelClass.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            e.printStackTrace();
            System.out.println("模型構造失敗...");
        }
    }

    // 接收分頁查詢參數
    protected int page;
    protected int rows;

    public void setPage(int page) {
        this.page = page;
    }

    public void setRows(int rows) {
        this.rows = rows;
    }

    // 將分頁查詢結果數據,壓入值棧的方法
    protected void pushPageDataToValueStack(Page<T> pageData) {
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("total", pageData.getTotalElements());
        result.put("rows", pageData.getContent());

        ActionContext.getContext().getValueStack().push(result);
    }
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章