📄 dictionarytag.java
字号:
/*
* Created on 2006-7-4 17:00:05
*
* By SinoBest
* Copyright hnisi.com.cn, 2005-2006, All rights reserved.
*/
package cn.com.juneng.system.common.taglib.dic;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import org.apache.commons.beanutils.PropertyUtils;
import cn.com.juneng.system.common.COMMON;
import cn.com.juneng.system.common.cache.DictionaryCache;
import cn.com.juneng.system.common.taglib.HtmlTag;
import cn.com.juneng.system.common.util.SimpleDAO;
/**
* @author yehailong
*
*/
public class DictionaryTag extends HtmlTag {
private String name;
// 字典项的CODE值
private String value;
// 字典KIND
private String kind;
private boolean multiselect;
private boolean readonly;
private boolean onlyshow;
private String showType;
private String attr;
// 值对象来源
private String source;
// 静态字典 staticDic类似如"0:***;1:***;2:***"
private String staticDic;
// 直接使用sql构造字典,sql类似如"select ×× as CODE,×× as DETAIL from ××"
private String sql;
public boolean isMultiselect() {
return multiselect;
}
public void setMultiselect(boolean multiselect) {
this.multiselect = multiselect;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public boolean isReadonly() {
return readonly;
}
public void setReadonly(boolean readonly) {
this.readonly = readonly;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
public String getKind() {
return kind;
}
public void setKind(String kind) {
this.kind = kind;
}
public boolean isOnlyshow() {
return onlyshow;
}
public void setOnlyshow(boolean onlyshow) {
this.onlyshow = onlyshow;
}
public String getSql() {
return sql;
}
public void setSql(String sql) {
this.sql = sql;
}
public String getShowType() {
return showType;
}
public void setShowType(String showType) {
this.showType = showType;
}
public String getAttr() {
return attr;
}
public void setAttr(String attr) {
this.attr = attr;
}
public String getSource() {
return source;
}
public void setSource(String source) {
this.source = source;
}
public String getStaticDic() {
return staticDic;
}
public void setStaticDic(String staticDic) {
this.staticDic = staticDic;
}
public int doStartTag() throws JspException {
try {
String[] code = new String[] {};
if (this.value != null) {
if (this.source != null) {
Object obj = request().getAttribute(this.source);
if (obj == null) {
obj = pageContext.getAttribute((String) source);
}
if (obj == null) {
obj = request().getParameter((String) source);
}
if (obj == null) {
obj = request().getSession().getAttribute(
(String) source);
}
if (obj != null) {
if (obj instanceof Map) {
Map map = (Map) obj;
this.value = (String) map.get(this.value);
} else {
this.value = (String) PropertyUtils
.getNestedProperty(obj, this.value);
}
}
}
if (this.value == null) {
this.value = "";
}
code = this.value.split(",");
}
List list = null;
if (this.kind != null) {
list = DictionaryCache.getInstance().getKindList(this.kind);
} else if (this.sql != null) {
list = SimpleDAO.query(this.sql);
} else if (this.staticDic != null) {
//静态字典构造
list = new ArrayList();
String[] dic = this.staticDic.split(";");
for (int i = 0; i < dic.length; i++) {
Map dicMap = new HashMap();
dicMap.put("CODE",dic[i].substring(0,dic[i].indexOf(":")));
dicMap.put("DETAIL",dic[i].substring(dic[i].indexOf(":")+1));
list.add(dicMap);
}
}
Map allMaps = COMMON.collection2map(list, "CODE");
StringBuffer dispValue = new StringBuffer();
for (int i = 0; allMaps != null && i < code.length; i++) {
Map map = (Map) allMaps.get(code[i]);
if (map != null) {
String detail = (String) map.get("DETAIL");
if (dispValue.length() != 0) {
dispValue.append(",");
}
dispValue.append(detail);
}
}
StringBuffer outStr = new StringBuffer("");
if (this.multiselect && COMMON.isEmpty(this.showType)) {
this.setShowType("checkbox");
}
String attr = this.getAttr();
if (attr == null) {
attr = "";
}
if (this.onlyshow) {
// 仅仅是显示,翻译中文值
outStr.append(dispValue);
} else if (!COMMON.isEmpty(this.showType)) {
if (showType.equals("select")) {
outStr.append("<select name=\"" + this.name + "\" " + attr
+ ">");
outStr.append("<option value=\"\" "
+ (("".equals(this.value)) ? "selected" : "")
+ ">请选择...</option>");
} else {
outStr
.append("<input type=\"hidden\" class='synValue' id=\""
+ this.name
+ "\" name=\""
+ this.name
+ "\" value=\"" + this.value + "\">");
}
for (Iterator iter = list.iterator(); iter.hasNext();) {
Map element = (Map) iter.next();
String c = (String) element.get("CODE");
String d = (String) element.get("DETAIL");
if (showType.equals("select")) {
outStr.append("<option value=\"" + c + "\" "
+ (c.equals(this.value) ? "selected" : "")
+ ">" + d + "</option>");
} else {
// radio、checkbox
outStr.append("<input class='synValue' type=\""
+ this.showType + "\" name=\"_" + this.name
+ "\" ");
outStr.append("value=\"" + c + "\" ");
if (COMMON.arrayIndexOf(code, c) != -1) {
outStr.append("checked ");
}
if (this.readonly) {
outStr.append("disabled ");
}
// outStr.append("onclick=\"clickit('"+this.name+"')\"
// ");
outStr.append(" " + attr + ">");
outStr.append(d);
}
}
if (showType.equals("select")) {
outStr.append("</select>");
}
} else {
outStr.append("<input name=\"_" + this.getName()
+ "\" class=\"selectTextInput\" value=\"" + this.value
+ "\" valuefield=\"c\" readonly src=\"dynamic\"");
outStr.append(" dispValue=\"" + dispValue + "\"");
if (this.readonly) {
outStr.append(" disabled");
}
outStr.append(" multiselect=\"" + this.multiselect + "\">\r\n");
outStr
.append("<img src=\"/basic/query/images/downarrow.gif\" class=\"fireselect\" inputfieldname=\"_"
+ this.name + "\">\r\n");
outStr.append("<div id=\"_dynamic_" + this.name
+ "\" style=\"display:none\">\r\n");
for (Iterator iter = list.iterator(); iter.hasNext();) {
Map element = (Map) iter.next();
outStr.append("<lable c=\"" + element.get("CODE")
+ "\" d=\"" + element.get("DETAIL")
+ "\" p=\"\" />\r\n");
}
outStr.append("</div>\r\n");
}
JspWriter out = pageContext.getOut();
out.println(outStr.toString());
} catch (IOException e) {
e.printStackTrace();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return SKIP_BODY;
}
public int doEndTag() throws JspException {
// this.cleanUp();
try {
// super.getOutput().flush();
} catch (Exception e) {
// ignore
}
return EVAL_PAGE;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -