Action的抽取CommonAction

package com.lyric.bos.web.action.common;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.lang.reflect.Type;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.struts2.ServletActionContext;
import org.springframework.data.domain.Page;

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

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;
/**
 * 公共Action
 * @author admin
 *
 * @param <T>
 */
public class CommonAction<T> extends ActionSupport implements ModelDriven<T> {


    protected Integer page;//当前页码  private protected public 
    protected Integer rows;//每页显示记录数

    public void setPage(Integer page) {
        this.page = page;
    }
    public void setRows(Integer rows) {
        this.rows = rows;
    }

    //声明模型对象
    private T model;

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

    //构造方法 通过java反射获取实体对象
    public CommonAction(){
        //获取父类的类型
        ParameterizedType genericSuperclass = (ParameterizedType)this.getClass().getGenericSuperclass();
        //获取父类类型上数组
        Type[] actualTypeArguments = genericSuperclass.getActualTypeArguments();
        //实体的类型
        Class<T> entityClass = (Class<T>)actualTypeArguments[0];
        //通过java反射获取实体对象
        try {
            model = entityClass.newInstance();
        } catch (InstantiationException | IllegalAccessException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }

    /**
     * 公共方法:将page对象转json 通过输出流写回页面
     * @param page
     * @param excludes
     * @throws IOException
     */
    public void page2Json(Page<T> page,String[] excludes) throws IOException{
        //返回:Page<T> 获取出total 以及rows 
        long totalElements = page.getTotalElements(); //总记录数
        List<T> content = page.getContent();
        Map<String,Object> map = new HashMap<>();
        map.put("total", totalElements);
        map.put("rows", content);

        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setExcludes(excludes);//排除页面不需要的数据
        //json-lib将上去获取的数据转json
        String json = JSONObject.fromObject(map,jsonConfig).toString();
        //通过输出流写回页面
        ServletActionContext.getResponse().setContentType("text/json;charset=UTF-8");
        ServletActionContext.getResponse().getWriter().print(json);
    }

    /**
     * list集合转json 数据 通过输出流返回浏览器
     * @param list
     * @throws IOException 
     */
    public void list2Json(List<?> list,String[] excludes) throws IOException{
        //排除不需要的数据 对象 属性
        JsonConfig jsonConfig = new JsonConfig();
        jsonConfig.setExcludes(excludes);
        String json = JSONArray.fromObject(list, jsonConfig).toString();
        //json数据 通过输出流返回前台页面
        ServletActionContext.getResponse().setContentType("text/json;charset=UTF-8");
        ServletActionContext.getResponse().getWriter().print(json);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值