(声明:目前p4jmvc内测版只支持Spring MVC,仅作各位技术参考。)
附件源码下载地址:http://download.csdn.net/detail/partner4java/5220557
作用:简化Controller,常用的CURD操作,Controller不需要写一行代码,只需要继承即可。
注意: p4jmvc主要借助了p4jorm。
如:我们对城市管理的Controller(对城市信息修改删除等,只需要传入固定参数id)
继承自BaseManageView,只需要完成额外两点:
1、传入两个泛型类型,entity和formbean类型;
2、带参数构造器,传入给BaseManageViewP4jDao。
@Controller @RequestMapping("/manage/city/*") public class CityManageView extends BaseManageView<City, CityForm>{ @SuppressWarnings("unused") private CityService cityService; @Autowired public CityManageView(CityService cityService) { super(cityService); this.cityService = cityService; } }核心功能由类BaseManageView提供(具体查看附件源码):
package com.partner4java.mvc.controller; import java.util.LinkedHashMap; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springframework.beans.BeanUtils; import org.springframework.beans.BeansException; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.ModelAttribute; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import com.partner4java.mvc.formbean.BaseForm; import com.partner4java.orm.bean.page.PageData; import com.partner4java.orm.bean.page.PageIndex; import com.partner4java.orm.dao.P4jDao; import com.partner4java.orm.dao.enums.OrderType; import com.partner4java.tools.generic.GenericsGetHelper; import com.partner4java.tools.reflection.ReflectionDataHelper; /** * Controller基础类<br/> * 封装了基本的CURD视图功能,目前只适用于Spring MVC。<br/> * <br/> * <b>注意:</b>视图的存放位置和命名必须是固定的,以viewResolver的prefix为根,必须将实体的CURD视图存放于与实体名称相同的目录中:<br/> * 以名为Authority实体为例,分别存放为"authority/manage.jsp", * "authority/add.jsp","authority/edit.jsp" * * @author partner4java * * @param <E> * 当前管理实体对象类型(entity) * @param <F> * 当前管理FormBean对象类型(entity) */ public class BaseManageView<E, F extends BaseForm> { public static final String SET_VISIBLE = "setVisible"; public static final String VISIBLE = "visible"; public static final String ID = "id"; public static final String DELETE_DO = "delete.do"; public static final String CLOSE_DO = "close.do"; public static final String OPEN_DO = "open.do"; public static final String EDIT_DO = "edit.do"; public static final String EDIT = "/edit"; public static final String ADD_DO = "add.do"; public static final String ADD = "/add"; public static final String VIEW_LIST_DO = "viewlist.do"; public static final String VIEW_LIST = "/viewlist"; public static final String REDIRECT_VIEW_LIST = "redirect:viewlist.do"; protected String entityClassNameL; protected String entityClassFullName; protected String formBeanFullName; protected String view_list; protected String add; protected String edit; protected P4jDao<E> dao; private Log logger = LogFactory.getLog(BaseManageView.class); @SuppressWarnings("unchecked") protected Class<E> entityClass = GenericsGetHelper.getSuperGenericsClass(this.getClass(), BaseManageView.class.getTypeParameters()[0]); @SuppressWarnings("unchecked") protected Class<F> formClass = GenericsGetHelper.getSuperGenericsClass(this.getClass(), BaseManageView.class.getTypeParameters()[1]); /** * 必须调用本有参构造器 * * @param dao * 子类对应的Service */ public BaseManageView(P4jDao<E> dao) { super(); this.dao = dao; this.formBeanFullName = formClass.getName(); this.entityClassFullName = entityClass.getName(); this.entityClassNameL = entityClass.getSimpleName().toLowerCase(); view_list = entityClassNameL + VIEW_LIST; add = entityClassNameL + ADD; edit = entityClassNameL + EDIT; } /** * 显示默认视图<br/> * 支持自动查询和自动回显 * * @param model * spring的Model对象 * @param form * 当前传入FormBean * @return */ @RequestMapping(VIEW_LIST_DO) public String viewList(Model model, @ModelAttribute("form") F form) { return doViewList(model, form, null); } /** * 信息暂时执行方法 * * @param model * @param form * @param orderby * @return */ protected String doViewList(Model model, F form, LinkedHashMap<String, OrderType> orderby) { try { PageData<E> pageData = dao.query(form, new PageIndex(form.getCurrentPage()), orderby); model.addAttribute("pageData", pageData); } catch (Exception e) { e.printStackTrace(); } return view_list; } /** * 添加视图转发 * * @param model * spring的Model对象 * @return */ @RequestMapping(value = ADD_DO, method = RequestMethod.GET) public String addView(Model model) { try { model.addAttribute("form", Class.forName(formBeanFullName).newInstance()); } catch (Exception e) { e.printStackTrace(); } return add; } /** * 添加对象动作执行 * * @param model * spring的Model对象 * @param form * 当前传入FormBean * @return 管理视图 */ @RequestMapping(value = ADD_DO, method = RequestMethod.POST) public String add(Model model, @ModelAttribute("form") F form) { try { @SuppressWarnings("unchecked") E entity = (E) Class.forName(entityClassFullName).newInstance(); BeanUtils.copyProperties(form, entity); dao.save(entity); } catch (Exception e) { e.printStackTrace(); } return REDIRECT_VIEW_LIST; } /** * 修改视图转发 * * @param model * spring的Model对象 * @param id * 修改对象的id * @return */ @RequestMapping(value = EDIT_DO, method = RequestMethod.GET) public String editView(Model model, @RequestParam(ID) int id) { try { E entity = dao.get(id); idExistException(entity,"editView"); @SuppressWarnings("unchecked") F form = (F) Class.forName(formBeanFullName).newInstance(); BeanUtils.copyProperties(entity, form); form.setId(id); model.addAttribute("form", form); } catch (Exception e) { e.printStackTrace(); } return edit; } /** * 修改动作执行<br/> * 此form需要包含一个“”同名字段 * * @param model * spring的Model对象 * @param form * 当前传入FormBean * @param id * 修改对象的id * @return 管理视图 */ @RequestMapping(value = EDIT_DO, method = RequestMethod.POST) public String edit(Model model, @ModelAttribute("form") F form) { try { E entity = dao.get(form.getId()); idExistException(entity,"edit"); BeanUtils.copyProperties(form, entity); dao.update(entity); } catch (BeansException e) { e.printStackTrace(); } return REDIRECT_VIEW_LIST; } /** * 删除动作执行 * * @param id * 删除对象的id * @return */ @RequestMapping(value = DELETE_DO, method = RequestMethod.GET) public String delete(@RequestParam("id") int id) { try { dao.delete(id); } catch (Exception e) { e.printStackTrace(); } return REDIRECT_VIEW_LIST; } /** * 关闭此条数据<br/> * 需要注意的是,此实体对象需要遵循的为包含一个为“private boolean visible;”的字段 * * @param id * 数据id * @return */ @RequestMapping(value = CLOSE_DO, method = RequestMethod.GET) public String close(@RequestParam(ID) int id) { try { E entity = dao.get(id); idExistException(entity,"close"); if (logger.isDebugEnabled()) { logger.debug("close() entity:" + entity.getClass() + " value:" + entity.toString()); } ReflectionDataHelper.invokeMethod(entity, SET_VISIBLE, false); dao.update(entity); } catch (Exception e) { e.printStackTrace(); } return REDIRECT_VIEW_LIST; } /** * 打开此条数据<br/> * 需要注意的是,此实体对象需要遵循的为包含一个为“private boolean visible;”的字段 * * @param id * 数据id * @return */ @RequestMapping(value = OPEN_DO, method = RequestMethod.GET) public String open(@RequestParam(ID) int id) { try { E entity = dao.get(id); idExistException(entity,"open"); if (logger.isDebugEnabled()) { logger.debug("open() entity:" + entity.getClass() + " value:" + entity.toString()); } ReflectionDataHelper.invokeMethod(entity, SET_VISIBLE, true); dao.update(entity); } catch (Exception e) { e.printStackTrace(); } return REDIRECT_VIEW_LIST; } private void idExistException(E entity,String methodName) { if(entity == null){ throw new IllegalArgumentException(methodName + " id is not exist"); } } }