import java.io.IOException; import java.io.PrintWriter; import java.lang.reflect.Method; import java.util.List; import java.util.Map; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.apache.commons.lang.StringUtils; import org.apache.struts2.interceptor.ServletRequestAware; import org.apache.struts2.interceptor.ServletResponseAware; import org.apache.struts2.interceptor.SessionAware; import org.displaytag.tags.TableTagParameters; import org.displaytag.util.ParamEncoder; import com.autocreate.service.BaseService; import com.autocreate.util.JBeanClass; import com.autocreate.util.Log; import com.opensymphony.xwork2.ActionSupport; /** * BaseAction * @author Administrator */ public abstract class BaseAction extends ActionSupport implements ServletRequestAware, ServletResponseAware, SessionAware { private static final long serialVersionUID = -2252914695328460450L; /** * 添加失败 */ public static final String ADDFAILED = "addfailed"; /** * 添加成功 */ public static final String ADDSUCCESS = "addsuccess"; /** * 每页列表,即每页显示的记录数 */ @SuppressWarnings("unchecked") private List list; /** * 当前页码,即当前第几页 */ private int pageNumber = 1; /** * 每页记录数,即一页显示多少条 */ private int pageSize = 20; /** * 总记录数 */ private int fullListSize = 0; /** * 页面传递删除的记录集合 */ private String[] serial; /** * 响应URL */ private String url; /** * 响应内容 */ private String msg; /** * HttpSession */ @SuppressWarnings("unchecked") public Map session; /** * HttpServletRequest */ public HttpServletRequest request; /** * HttpServletResponse */ public HttpServletResponse response; /** * 跳转到添加页面 * @return */ public String add() { return SUCCESS; } /** * 查询 * @return */ public abstract String find(); /** * 查询集合 * @return */ public abstract String list(); /** * 添加 * @return */ public abstract String insert() throws Exception; /** * 修改 * @return */ public abstract String modify() throws Exception; /** * 删除 * @return */ public abstract String delete() throws Exception; /** * 根据对象和页面删传递过来的删除数组,反射删除数据库 * @param serial * @param entityClass * @return * @throws Exception */ @SuppressWarnings("unchecked") protected boolean deleteByStrings(Class entityClass) throws Exception { if (null != serial && serial.length > 0) { BaseService baseService = new BaseService(); Object obj = entityClass.newInstance(); // 创建实例 Method method = entityClass.getMethod("getTablePrimaryKey"); // 执行指定方法 String primaryKey = method.invoke(obj).toString(); for (String str : serial) { obj = JBeanClass.setPropertyValueFromString(obj, primaryKey, str); baseService.delete(obj); } } return false; } /** * 查询一页数据 * @param clazz * @param tableName * @param map * @throws Exception */ @SuppressWarnings("unchecked") protected void queryPageList(String tableName, Class serviceClass, Class entityClass, Map<String, String> map) { try { String pageIndexName = new ParamEncoder("element").encodeParameterName(TableTagParameters.PARAMETER_PAGE); // 页数的参数名 String value = request.getParameter(pageIndexName); pageNumber = StringUtils.isEmpty(value) ? 1 : Integer.parseInt(value); // 当前页数 Class classType = serviceClass; Object obj = serviceClass.newInstance(); // 创建实例 Method method = classType.getMethod("getCount", new Class[] { String.class, Map.class }); // 执行指定方法 fullListSize = Integer.parseInt(method.invoke(obj, new Object[] { tableName, map }).toString()); Method method2 = classType.getMethod("queryPageList", new Class[] { String.class, Class.class, Map.class, int.class, int.class }); // 执行指定方法 list = (List) method2.invoke(obj, new Object[] { tableName, entityClass, map, pageNumber, pageSize }); } catch (Exception e) { Log.getLogger().error("queryPageList error...", e); } } /** * 输入HTML响应内容用于AJAX * @param value 要写的值 */ public void writerHtml(String value) { writer(value, 1); } /** * 输入XML响应内容用于AJAX * @param value 要写的值 */ public void writerXml(String value) { writer(value, 2); } /** * 输入响应内容用于AJAX * @param value * @param type */ private void writer(String value, int type) { PrintWriter out = null; try { out = response.getWriter(); response.setCharacterEncoding("UTF-8"); switch (type) { case 1: response.setContentType("text/html;charset=UTF-8"); break; case 2: response.setContentType("text/xml;charset=UTF-8"); break; } out.write(value); out.flush(); } catch (IOException e) { Log.getLogger().error("Out Writer Error...", e); } finally { out.close(); } } public String getUrl() { return url; } public void setUrl(String url) { this.url = url; } public String getMsg() { return msg; } public void setMsg(String msg) { this.msg = msg; } @SuppressWarnings("unchecked") public void setSession(Map session) { this.session = session; } public void setServletRequest(HttpServletRequest request) { this.request = request; } public void setServletResponse(HttpServletResponse response) { this.response = response; } @SuppressWarnings("unchecked") public List getList() { return list; } @SuppressWarnings("unchecked") public void setList(List list) { this.list = list; } public int getPageNumber() { return pageNumber; } public void setPageNumber(int pageNumber) { this.pageNumber = pageNumber; } public int getPageSize() { return pageSize; } public void setPageSize(int pageSize) { this.pageSize = pageSize; } public int getFullListSize() { return fullListSize; } public void setFullListSize(int fullListSize) { this.fullListSize = fullListSize; } public String[] getSerial() { return serial; } public void setSerial(String[] serial) { this.serial = serial; } }
import java.util.HashMap; import java.util.Map; import com.auto.entity.Roleinfo; import com.auto.service.BaseService; public class RoleinfoAction extends BaseAction { private static final long serialVersionUID = -128991403689032079L; private Roleinfo info; public String find() { if(null != info) { info = new BaseService<Roleinfo>().query(info); } return SUCCESS; } @SuppressWarnings("unchecked") public String list() { Map map = null; if (null != info) { map = new HashMap(); map.put("Operatorid", info.getOperatorid()); } queryPageList("Roleinfo", BaseService.class, Roleinfo.class, map); return SUCCESS; } public String insert() throws Exception { try { return new BaseService<Roleinfo>().insert(info) ? ADDSUCCESS : ADDFAILED; } catch (Exception e) { throw e; } } public String modify() throws Exception { try { if (null != info) { new BaseService<Roleinfo>().update(info); } return ADDSUCCESS; } catch (Exception e) { throw e; } } public String delete() throws Exception { try { deleteByStrings(Roleinfo.class); return ADDSUCCESS; } catch (Exception e) { throw e; } } public Roleinfo getInfo() { return info; } public void setInfo(Roleinfo info) { this.info = info; } }