struts的common类

package wb.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 org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;

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;

public class CommonAction<T> extends ActionSupport implements ModelDriven<T> {
    private T model;
    public T getModel() {
        return model;
    }
    public CommonAction() {
        //获取父类类型
        ParameterizedType superclass = (ParameterizedType)this.getClass().getGenericSuperclass();
        //获取父类上面的泛型数组
        Type[] types = superclass.getActualTypeArguments();
        //获取泛型类型
        Class<T> modelClass = (Class<T>)types[0];
        //通过反射创建对象
        try {
            model = modelClass.newInstance();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    private int page;
    private int rows;
    public int getPage() {
        return page;
    }
    public void setPage(int page) {
        this.page = page;
    }
    public int getRows() {
        return rows;
    }
    public void setRows(int rows) {
        this.rows = rows;
    }

    /*
     * 分页查询结果转json
     */
    public void page2json(Page<T> page, String[] excludes){
        Map<String, Object> map = new HashMap<>();
        if (page != null) {
            map.put("total", page.getTotalElements());
            map.put("rows", page.getContent());
        }
        JsonConfig config = new JsonConfig();
        config.setExcludes(excludes);
        try {
            String json = JSONObject.fromObject(map, config).toString();
            ServletActionContext.getResponse().setContentType("text/json;charset=UTF-8");
            ServletActionContext.getResponse().getWriter().print(json);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /*
     * 查询的结果集转json
     */
    public void list2json(List list,String[] excludes){
        JsonConfig config = new JsonConfig();
        config.setExcludes(excludes);
        try {
            String json = JSONArray.fromObject(list, config).toString();
            ServletActionContext.getResponse().setContentType("text/json;charset=UTF-8");
            ServletActionContext.getResponse().getWriter().print(json);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /*
     * 查询的对象转json
     */
    public void object2json(Object object, String[] excludes){
        JsonConfig config = new JsonConfig();
        config.setExcludes(excludes);
        try {
            String json = JSONArray.fromObject(object, config).toString();
            ServletActionContext.getResponse().setContentType("text/json;charset=UTF-8");
            ServletActionContext.getResponse().getWriter().print(json);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    /*
     * 分页查询
     */
    public Pageable getPageable(){
        Pageable pageable = new PageRequest(getPage() - 1, getRows());
        return pageable;
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值