/** * 抽象类BaseAction * * @author weiwei */ public abstract class TreeMenuBaseAction { protected TreeMenuService service = IOC.getBean(TreeMenuCons.IOC_SERVICE_BEAN_ID()); protected DWZ dwz = IOC.getBean(DWZCons.IOC_DWZ_BEAN_ID()); protected TreeMenu treeMenu; protected Long[] ids; protected Long id = 0L; protected String keyword = ""; protected Long treeMenuId = 0L; @Int(mess = "导航菜单ID应该为数字格式") protected Long navMenuId = 0L; protected int pageNum = 1; protected int numPerPage = 20; public void setTreeMenu(TreeMenu treeMenu) { this.treeMenu = treeMenu; } public void setKeyword(String keyword) { this.keyword = keyword; } public void setPageNum(int pageNum) { this.pageNum = pageNum; } public void setNumPerPage(int numPerPage) { this.numPerPage = numPerPage; } public void setTreeMenuId(Long treeMenuId) { this.treeMenuId = treeMenuId; } public void setNavMenuId(Long navMenuId) { this.navMenuId = navMenuId; } public void setIds(Long[] ids) { this.ids = ids; } public void setId(Long id) { this.id = id; } }
// IOC,注入对象到pojo private void injectIocBean() throws Exception { fields = ru.getFields(); if (fields == null) return; for (Field f : fields) { Class<?> type = f.getType(); Ioc ioc = f.getAnnotation(Ioc.class); if (ioc == null) continue; String beanId = ""; if (ioc.value().trim().length() == 0) beanId = type.getSimpleName(); else beanId = CommonUtil.parsePropValue(ioc.value()); Method setter = ru.getSetter(f.getName()); if (setter == null) continue; setter.invoke(this.actionObject, IOC.getBean(beanId)); } }
private void injectActionCxt2Pojo(ReflectUtil ru) throws Exception { HttpServletRequest req = this.context.getRequest(); HttpServletResponse res = this.context.getResponse(); PrintWriter out = this.context.getWriter(); HttpSession session = this.context.getSession(); ActionProp actionProp = this.context.getActionProp(); QueryParams queryParams = this.context.getQueryParams(); for (String n : ru.getFieldsName()) { Method m = ru.getSetter(n); if (m == null) continue; Class<?> clazz = m.getParameterTypes()[0]; if (Context.class.isAssignableFrom(clazz)) { m.invoke(ru.getObject(), this.context); } else if (HttpServletRequest.class.isAssignableFrom(clazz)) { m.invoke(ru.getObject(), req); } else if (HttpServletResponse.class.isAssignableFrom(clazz)) { m.invoke(ru.getObject(), res); } else if (PrintWriter.class.isAssignableFrom(clazz)) { m.invoke(ru.getObject(), out); } else if (ServletOutputStream.class.isAssignableFrom(clazz)) { m.invoke(ru.getObject(), this.context.getOut()); } else if (HttpSession.class.isAssignableFrom(clazz)) { m.invoke(ru.getObject(), session); } else if (ActionProp.class.isAssignableFrom(clazz)) { if (actionProp == null) actionProp = new ActionProp(clazz.getName()); this.context.setActionProp(actionProp); m.invoke(ru.getObject(), actionProp); } else if (QueryParams.class.isAssignableFrom(clazz)) { m.invoke(ru.getObject(), queryParams); } else if (Validation.class.isAssignableFrom(clazz)) { m.invoke(ru.getObject(), this.context.getValidation()); } else { /* 如果找不到注入的类型,则尝试从IOC容器获取 */ Object obj = IOC.getBean(n); if (obj != null) m.invoke(ru.getObject(), obj); } } }