您的位置:首页 > 其它

自定义字典表的使用

2016-05-20 17:34 309 查看
数据库:

表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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: