自定义字典表的使用
2016-05-20 17:34
309 查看
数据库:
表sys_dict
表t_department
DictTag.java
字典表工具DictUtils.java
<ph:dictValue dictCode="${dept.categoryId }"/>
2.在controller中,通过字典工具获取到分类下的每个科室列表,到页面的下拉框。
表sys_dict
表t_department
DictTag.java
public class DictTag extends BodyTagSupport{ /** * 创建日志对象 */ private static final Logger logger = Logger.getLogger(DictTag.class);; /** * 是否拥有权限 */ private String dictCode; /** * {@inheritDoc} * @see javax.servlet.jsp.tagext.BodyTagSupport#setBodyContent(javax.servlet.jsp.tagext.BodyContent) */ public void setBodyContent(BodyContent bc) { super.setBodyContent(bc); } /** * {@inheritDoc} * @see javax.servlet.jsp.tagext.BodyTagSupport#doAfterBody() */ public int doAfterBody() { try { getBodyContent().getEnclosingWriter().println(getBodyContent().getString()); } catch (IOException ee) { ee.printStackTrace(); } return EVAL_PAGE; } /** * {@inheritDoc} * @throws JspException * @throws Exception * @see javax.servlet.jsp.tagext.BodyTagSupport#doStartTag() */ public int doStartTag() throws JspException{ try { JspWriter out = pageContext.getOut(); if(StringUtils.isNotEmpty(dictCode)){ String dictValue = DictUtils.getDictValue(this.dictCode); out.print(dictValue); }else { out.print(""); } } catch (Exception e) { e.printStackTrace(); } return SKIP_BODY; } public String getDictCode() { return dictCode; } public void setDictCode(String dictCode) { this.dictCode = dictCode; } public static void main(String args[]){ String a = "aaa.bbb.ccc"; String[] dickKeys = a.split("\\."); String dictKey = dickKeys[0] + "." + dickKeys[1]; System.out.print(dictKey); } }
字典表工具DictUtils.java
public class DictUtils { public static Map<String,Object> dictMap = new HashMap<String,Object>(); public static Map<String, Object> getDictMap() { return dictMap; } public static void setDictMap(Map<String, Object> dictMap) { DictUtils.dictMap = dictMap; } /** * 通过字典key 获取字典value * @param dictList * @param key * @return */ public static String getDictValue(List<SysDict> dictList,String key){ String dictValue = ""; if(ListUtil.isNotNull(dictList)){ for(int i=0;i<dictList.size();i++){ SysDict dict = dictList.get(i); if(dict.getDictCode().equals(key)){ dictValue = dict.getDictValue(); break; } } } return dictValue; } public static String getDictValue(String dictCode){ String[] dickKeys = dictCode.split("\\."); String dictKey = dickKeys[0] + "." + dickKeys[1]; List<SysDict> dictList = (List<SysDict>)DictUtils.getDictMap().get(dictKey); String dictValue = DictUtils.getDictValue(dictList, dictCode); return dictValue; } }在hnpd-tags.tld文件中引入:
<span style="white-space:pre"> </span><tag> <name>dictValue</name> <tag-class> com.rmyy.web.common.tag.DictTag </tag-class> <body-content>empty</body-content> <attribute> <name>dictCode</name> <required>false</required> <rtexprvalue>true</rtexprvalue> </attribute> </tag>web.xml中:
<span style="white-space:pre"> </span><jsp-config> <taglib> <taglib-uri>/WEB-INF/hnpd-tags.tld</taglib-uri> <taglib-location>/WEB-INF/hnpd-tags.tld</taglib-location> </taglib> </jsp-config>1.查询单条信息时,在controller中获取到t_department表中的category_id内容,在页面通过以下自定义标签ph将dict_code转换为对应的dict_Value
<ph:dictValue dictCode="${dept.categoryId }"/>
2.在controller中,通过字典工具获取到分类下的每个科室列表,到页面的下拉框。
@RequestMapping(value="/m/department/add") public String add(@ModelAttribute Department department,@RequestParam int curPage,ModelMap model){ <span style="color:#ff0000;">List<SysDict> dictList = (List<SysDict>)DictUtils.getDictMap().get("department.category");</span> model.addAttribute("curPage", curPage); model.addAttribute("dictList", dictList); return "m/department/add"; }在页面通过下拉框,选择科室分类。
itemLabel是给用户看的<pre name="code" class="html">itemValue是要存入数据库的
<tr> <td width="10%" align="right" bgcolor="#ffffff">
<span class="fontts"> * </span>选择分类:</td> <td width="40%" align="left" bgcolor="#FFFFFF"> <sf:select path="categoryId" checkItems="N"> <option value="">-请选择-</option> <sf:options items="${dictList }" itemLabel="dictValue" itemValue="dictCode"/> </sf:select> </td> </tr>
相关文章推荐
- asp.net多文件打包下载
- 关于弹窗:自定义PopuWindow和Toast
- C# Compiler Errors
- 第三方库 zlib库 引入到现有项目中
- 升级centos内核到最新版本
- 行式数据库与列式数据库的对比
- Spring6:基于注解的Spring MVC(上篇)
- APC UPS 网络管理卡(型号apc ap9631)的配置
- APC UPS 网络管理卡(型号apc ap9631)的配置
- logcat 命令行只打印某个 app 的 log
- javascript不起作用了
- c++实现读写共享锁
- Oracle PL/SQL入门学习
- 蓝桥杯
- 函数指针总结
- XenServer的架构之XenAPI(3)
- Elasticsearch 安装与集群配置
- Spring5:@Autowired注解、@Resource注解和@Service注解
- Spring4:JDBC
- poj 1011 木棍