最近分配到了一个新的任务就是“数据字典”功能,改功能包括“字典分组管理”和“字典条目管理”其中的关系是 一对多关系。
用到的前端框架operamasks ui,mvc层是springmvc
下面是字典分组 效果图
下面是字典条目效果图
因为用到的是operamasks ui 所以在页面中的数据主要是通过ajax异步获取
下面是实现分组功能的 jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>数据字典-类型管理
下面是 字典条目 的jsp页面
<%@ page language="java" contentType="text/html; charset=UTF-8" pageEncoding="UTF-8"%> <%@ taglib prefix="form" uri="http://www.springframework.org/tags/form"%> <%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%> <% String path = request.getContextPath(); String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/"; %>数据字典-内容管理
处理 分组的 action
package org.shield.module.web.controller.admin.dictionary; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.codehaus.jackson.map.ObjectMapper; import org.shield.commons.dictionary.IDictionaryGroupService; import org.shield.commons.dictionary.IDictionaryService; import org.shield.commons.entity.dictionary.DictionaryGroupsObject; import org.shield.omui.GridDataModel; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; /** * 数据字段分组管理 * * @author hhy * */ @Controller @RequestMapping("/admin/dic_groups") public class DictionaryGroupsController { private static final String ERROR="error"; private static final String RESULT="result"; private static final String SUCCESS="success"; @Resource private IDictionaryGroupService dictionaryGroupService; @Resource private IDictionaryService dictionaryService; @RequestMapping(method = RequestMethod.GET) public String view(Model model) { DictionaryGroupsObject dicObject = new DictionaryGroupsObject(); model.addAttribute("dicObject", dicObject); return "/admin/dictionary/dic_groups"; } /** * 获取分页数据 * * @param request * @param response * @param model * @param limit * @param start * @return */ @SuppressWarnings("unused") @RequestMapping(value = "list", method = RequestMethod.GET) @ResponseBody private GridDataModeldicGroupList( @RequestParam(required = false) GridDataModel model, @RequestParam(required = false) int limit, @RequestParam(required = false) int start) throws Exception { if (model == null) { model = new GridDataModel (); } List list = new ArrayList (); int total = (int) dictionaryGroupService.getTotalCount(); list = dictionaryGroupService.findPageByHQL(start, limit, "from DictionaryGroupsObject"); model.setTotal(total); model.setRows(list); return model; } /** * 处理数据字典的 增 删 改 * * @param request * @param response * @param formData * @return * @throws Exception */ @SuppressWarnings({ "unchecked" }) @RequestMapping(value = "save", method = RequestMethod.POST) @ResponseBody public Map save( @RequestParam(required = false) String formData) throws Exception { Map returnMap = new HashMap (); ObjectMapper mapper = new ObjectMapper(); Map map = (Map ) mapper.readValue( formData, Map.class); // 处理添加 if (map.get("insert") != null && !(map.get("insert").toString()).equals("[]")) { List dicList = getListFromJosn(map, "insert"); for (DictionaryGroupsObject dictionaryGroupsObject : dicList) { dictionaryGroupService.createOrModify(dictionaryGroupsObject); } returnMap.put(SUCCESS, "添加成功"); } //处理修改 if (map.get("update") != null && !(map.get("update").toString()).equals("[]")) { List dicList = getListFromJosn(map, "update"); for (DictionaryGroupsObject dictionaryGroupsObject : dicList) { dictionaryGroupService.createOrModify(dictionaryGroupsObject); } returnMap.put(SUCCESS, "修改成功"); } //处理删除 if (map.get("delete") != null && !(map.get("delete").toString()).equals("[]")) { List dicList = getListFromJosn(map, "delete"); for (DictionaryGroupsObject dictionaryGroupsObject : dicList) { dictionaryGroupService.removeById(dictionaryGroupsObject.getId()); } returnMap.put(SUCCESS, "删除成功"); } return returnMap; } /** * 校验编码是否存在 * @param request * @param response * @param value * @return * @throws Exception */ @RequestMapping(value = "exist", method = RequestMethod.GET) @ResponseBody public Map exist( @RequestParam(required = false) String id, @RequestParam(required = false) String value)throws Exception { Map returnMap = new HashMap (); int count = 0; //校验添加 if(id.equals("")){ String hql ="select count(*) from DictionaryGroupsObject dic where dic.groupCode=?"; count =(int) dictionaryGroupService.getTotalCountByHQL(hql,new String[]{value}); if(count>0){ returnMap.put(RESULT, ERROR); }else{ returnMap.put(RESULT, SUCCESS); } return returnMap; } //校验修改 if (value != null && !value.equals("")) { String hql ="select count(*) from DictionaryGroupsObject dic where dic.groupCode=?"; count = (int) dictionaryGroupService.getTotalCountByHQL(hql,new String[]{value}); } if(count>1){ returnMap.put(RESULT, ERROR); }else{ returnMap.put(RESULT, SUCCESS); } return returnMap; } /** * 校验改分组下面是否有字典内容 * @param request * @param response * @param id * @return * @throws Exception */ @RequestMapping(value="existObject",method=RequestMethod.GET) @ResponseBody public Map existObject( @RequestParam(required = false) String id)throws Exception{ Map resultMap = new HashMap (); int count = 0; count = dictionaryService.findDictionaryObjectByGroupId(id).size(); if(count >0){ resultMap.put(RESULT, ERROR); return resultMap; } resultMap.put(RESULT, SUCCESS); return resultMap; } /** * 把josn转成的map再转成List * 工具方法 * @param map * @param todo(insert update delete) * @return */ @SuppressWarnings("unchecked") public List getListFromJosn( Map map, String todo) { List
字典条目实现的action
package org.shield.module.web.controller.admin.dictionary; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.annotation.Resource; import javax.persistence.criteria.CriteriaBuilder.In; import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletResponse; import org.codehaus.jackson.map.ObjectMapper; import org.shield.commons.dictionary.IDictionaryGroupService; import org.shield.commons.dictionary.IDictionaryService; import org.shield.commons.entity.dictionary.DictionaryGroupsObject; import org.shield.commons.entity.dictionary.DictionaryObject; import org.shield.commons.hibernate.dao.support.SearchMap; import org.shield.module.web.form.admin.dictionary.DictionaryObjectModel; import org.shield.omui.ComboDataModel; import org.shield.omui.GridDataModel; import org.springframework.stereotype.Controller; import org.springframework.ui.Model; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.ResponseBody; /** * 数据字典管理 * * @author hhy * */ @Controller @RequestMapping("/admin/dic_content") @SuppressWarnings("unused") public class DictionaryObjcetController { private static final String ERROR="error"; private static final String RESULT="result"; private static final String SUCCESS="success"; @Resource private IDictionaryService dictionaryService; @Resource private IDictionaryGroupService dictionaryGroupService; @RequestMapping(method = RequestMethod.GET) public String view(Model model) { DictionaryObject dicObject = new DictionaryObject(); model.addAttribute("dicObject", dicObject); return "/admin/dictionary/dic_content"; } /** * 获取分组列表(key-value) * * @param request * @param response * @return * @throws Exception */ @RequestMapping(method = RequestMethod.GET, value = "groupList") @ResponseBody public ListgroupList() throws Exception { List resultList = new ArrayList (); resultList = dictionaryGroupService.findAll(); return resultList; } /** * 获取数据字典分页信息 * * @param request * @param response * @param model * @return * @throws Exception */ @RequestMapping(value = "list", method = RequestMethod.GET) @ResponseBody public GridDataModel dicPage( HttpServletRequest request, HttpServletResponse response, @RequestParam(required = false) GridDataModel model, @RequestParam(required = false) String groupId, @RequestParam(required = false) int limit, @RequestParam(required = false) int start) throws Exception { if (model == null) { model = new GridDataModel (); } int total = 0; SearchMap map = dictionaryService.createSearchMap(); if (groupId != null && !groupId.equals("")) { map.eq("groups.id", groupId); } map.orderBy("priority", true); total = (int) dictionaryService.getTotalCountByMap(map); List dicList = new ArrayList (); List dicModelList = new ArrayList (); dicList = dictionaryService.findPageByMap(start, limit, map); // 转成页面model for (DictionaryObject dictionaryObject : dicList) { dicModelList.add(new DictionaryObjectModel(dictionaryObject)); } model.setRows(dicModelList); model.setTotal(total); return model; } /** * 处理数据字典的 增 删 改 * * @param request * @param response * @param formData * @return * @throws Exception */ @SuppressWarnings({ "unchecked" }) @RequestMapping(value = "save", method = RequestMethod.POST) @ResponseBody public Map save(@RequestParam(required = false) String formData) throws Exception { Map returnMap = new HashMap (); ObjectMapper mapper = new ObjectMapper(); Map map = (Map ) mapper.readValue( formData, Map.class); // 处理添加 if (map.get("insert") != null && !(map.get("insert").toString()).equals("[]")) { List dicList = getListFromJosn(map, "insert"); for (DictionaryObject dictionaryObject : dicList) { dictionaryService.createOrModify(dictionaryObject); } returnMap.put(SUCCESS, "添加成功"); } if (map.get("update") != null && !(map.get("update").toString()).equals("[]")) { List dicList = getListFromJosn(map, "update"); for (DictionaryObject dictionaryObject : dicList) { dictionaryService.createOrModify(dictionaryObject); } returnMap.put(SUCCESS, "修改成功"); } if (map.get("delete") != null && !(map.get("delete").toString()).equals("[]")) { List dicList = getListFromJosn(map, "delete"); for (DictionaryObject dictionaryObject : dicList) { dictionaryService.removeById(dictionaryObject.getId()); } returnMap.put(SUCCESS, "删除成功"); } return returnMap; } /** * 校验字典值是否存在 * @param request * @param response * @param value * @return * @throws Exception */ @RequestMapping(value = "existName", method = RequestMethod.GET) @ResponseBody public Map exist( @RequestParam(required = false) String id, @RequestParam(required = false) String value) { Map returnMap = new HashMap (); int count = 0; //校验添加 if(id.equals("")){ String hql ="select count(*) from DictionaryObject dic where dic.value=?"; try { count =(int) dictionaryService.getTotalCountByHQL(hql,new String[]{value}); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } if(count>0){ returnMap.put(RESULT, ERROR); }else{ returnMap.put(RESULT, SUCCESS); } return returnMap; } //校验修改 if (value != null && !value.equals("")) { String hql ="select count(*) from DictionaryObject dic where dic.value=?"; try { count =(int) dictionaryService.getTotalCountByHQL(hql,new Object[]{value}); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } if(count>1){ returnMap.put(RESULT, ERROR); }else{ returnMap.put(RESULT, SUCCESS); } return returnMap; } /** * 把josn转成的map再转成List * 工具方法 * @param map * @param todo(insert update delete) * @return */ @SuppressWarnings("unchecked") public List getListFromJosn( Map map, String todo) { List > dicMap = (List >) map .get(todo); List dicList = new ArrayList (); for (Map map2 : dicMap) { DictionaryObject dicObject = new DictionaryObject(); String groupId =""; if(todo.equals("delete")){ dicObject.setId((String) map2.get("id")); dicList.add(dicObject); return dicList; } if (!todo.equals("insert")) { dicObject.setId((String) map2.get("id")); } try { groupId = dictionaryGroupService.getDictionaryGroupIdByGroupCode((String) map2.get("groupCode")); dicObject.setGroups(dictionaryGroupService.findById(groupId)); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } if(map2.get("priority") instanceof String){ dicObject.setPriority(Integer.parseInt((String)map2.get("priority"))); } if(map2.get("priority") instanceof Integer){ dicObject.setPriority((Integer)map2.get("priority")); } dicObject.setName((String)map2.get("name")); dicObject.setValue((String)map2.get("value")); dicList.add(dicObject); } return dicList; } }
工具类 在action中转换 json
package org.shield.omui; import java.util.ArrayList; import java.util.List; /** * omGrid组件 * 只包装了数据的总条数total和所有数据集合rows,这样包装是为了前台omGrid组件能够识别返回的JSON个数数据。它需要的格式是:{"total":15,"rows":[{childJSON}]} * 将数据包装成GridDataModel格式之后再使用JSON-lib包的工具类JSONObject.fromObject(model) 生成json数据,最好返写回前台,完成omGrid数据的展示。 * @author zzluo * * @param*/ public class GridDataModel { /** * 显示总数 */ private long total; /** * 行数据 */ private List rows = new ArrayList (); public List getRows() { return rows; } public void setRows(List rows) { this.rows = rows; } public long getTotal() { return total; } public void setTotal(long total) { this.total = total; } }