SSH-BOS项目:重构BaseAction以及工具类抽取

重构后(请对照SSH-BOS项目:底层代码抽取(BaseDao、BaseAction)):点击打开链接

package com.xushuai.bos.web.action;

import java.io.IOException;
import java.lang.reflect.ParameterizedType;
import java.util.List;

import javax.servlet.http.HttpServletResponse;

import org.hibernate.criterion.DetachedCriteria;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.xushuai.bos.utils.BOSUtils;
import com.xushuai.bos.utils.PageBean;

import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

public class BaseAction<T> extends ActionSupport implements ModelDriven<T> {
	
//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓
	
	protected PageBean pageBean = new PageBean();
	protected DetachedCriteria criteria = null;
	
	//接收分页参数
	protected int page;
	protected int rows;
	//直接将接收到的参数赋给pageBean
	public void setPage(int page) {
		pageBean.setCurrentPage(page);
	}
	public void setRows(int rows) {
		pageBean.setPageSize(rows);
	}
	
//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑	
	public final static String HOME = "home";
	public final static String LIST = "list";
	
	protected T model;
	//获取运行时期实际类型,并对其进行实例化
	public BaseAction() {
		ParameterizedType type = (ParameterizedType) this.getClass().getGenericSuperclass();
		Class entityClass = (Class) type.getActualTypeArguments()[0];
		
//↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓↓			
		//对查询条件进行初始化,并赋给pageBean
		criteria = DetachedCriteria.forClass(entityClass);
		pageBean.setCriteria(criteria);
//↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑↑	
		
		try {
			model = (T) entityClass.newInstance();
		} catch (InstantiationException e) {
			e.printStackTrace();
		} catch (IllegalAccessException e) {
			e.printStackTrace();
		}
	}
	
	


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

}


BOSUtils:

package com.xushuai.bos.utils;

import java.io.IOException;
import java.util.List;
import java.util.UUID;

import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts2.ServletActionContext;

import com.xushuai.bos.entity.User;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JsonConfig;

public class BOSUtils {
	
	/**
	 * 获取session对象
	 * @return
	 */
	public static HttpSession getSession(){
		return ServletActionContext.getRequest().getSession();
	}
	
	/**
	 * 获取response对象
	 * @return
	 */
	public static HttpServletResponse getResponse(){
		return ServletActionContext.getResponse();
	}
	
	/**
	 * 获取当前登录的用户
	 * @return
	 */
	public static User getLoginUser(){
		return (User) getSession().getAttribute("user");
	}

	/**
	 * 生成32位uuid
	 * @return
	 */
	public static String uuid() {
		return UUID.randomUUID().toString().replace("-", "").toUpperCase();
	}
	
	/**
	 * 将制定对象转换为json对象,并向页面回写
	 * @param o
	 * @param exclude
	 */
	public static void writerJson(Object o,String[] exclude){
		HttpServletResponse response = getResponse();
		response.setContentType("text/json;charset=UTF-8");
		//将PageBean转换为JSON串,返回给页面
		JsonConfig jsonConfig = new JsonConfig();
		//去除不需要返回的值
		jsonConfig.setExcludes(exclude);
		//将查询结果转换为json串
		String json = JSONObject.fromObject(o, jsonConfig).toString();
		//返回json数据
		try {
			response.getWriter().print(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 将指定的List转换
	 * @param list
	 * @param exclude
	 */
	public static void writerJson(List list,String[] exclude){
		HttpServletResponse response = getResponse();
		response.setContentType("text/json;charset=UTF-8");
		//将PageBean转换为JSON串,返回给页面
		JsonConfig jsonConfig = new JsonConfig();
		//去除不需要返回的值
		jsonConfig.setExcludes(exclude);
		//将查询结果转换为json串
		String json = JSONArray.fromObject(list, jsonConfig).toString();
		//返回json数据
		try {
			response.getWriter().print(json);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值