📄 displaycodetag.java
字号:
package com.szmx.tlms.admin.web.tag;
import com.szmx.framework.spring.context.ApplicationContextUtil;
import com.szmx.tlms.admin.service.CodeService;
import com.szmx.tlms.admin.model.Code;
import javax.servlet.jsp.tagext.TagSupport;
import javax.servlet.jsp.JspException;
import javax.servlet.jsp.JspWriter;
import java.io.IOException;
import java.util.List;
import java.util.Iterator;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.displaytag.tags.el.ExpressionEvaluator;
public class DisplayCodeTag extends TagSupport {
private final Log log = LogFactory.getLog(getClass());
private String codeType;
private String codeValue;
private String lang;
private String selectedValue;
private static final int DISPLAY_ONECODE = 0;
private static final int DISPLAY_CHILDCODES = 1;
public DisplayCodeTag() {
this.lang = "CN";
}
public String getCodeType() {
return codeType;
}
public void setCodeType(String codeType) {
this.codeType = codeType;
}
public String getLang() {
return lang;
}
public void setLang(String lang) {
this.lang = lang;
}
public String getCodeValue() {
return codeValue;
}
public void setCodeValue(String codeValue) {
this.codeValue = codeValue;
}
public String getSelectValue() {
return this.selectedValue;
}
public void setSelectValue(String value) {
this.selectedValue = value;
}
private void evalParams(int dispType) throws JspException {
ExpressionEvaluator eval = new ExpressionEvaluator(this, pageContext);
if (dispType == DISPLAY_ONECODE) {
this.codeValue = eval.evalString("codeValue", this.codeValue);
this.codeType = eval.evalString("codeType", this.codeType);
}
if (dispType == DISPLAY_CHILDCODES) {
this.codeType = eval.evalString("codeType", this.codeType);
if (selectedValue != null) {
this.selectedValue = eval.evalString("selectValue", this.selectedValue);
}
}
}
private int checkDisplayType() {
if (codeValue == null || codeValue.equals("")) {
return DISPLAY_CHILDCODES;
} else
return DISPLAY_ONECODE;
}
public int doStartTag() throws JspException {
CodeService service = (CodeService) ApplicationContextUtil.getBean("codeService");
int displayType = checkDisplayType();
evalParams(displayType);
switch (displayType) {
case DISPLAY_ONECODE:
Code code;
try {
code = service.getCodeByTypeAndValue(codeType, codeValue);
} catch (Exception e) {
log.error(e);
throw new JspException(e);
}
if (code != null) {
JspWriter out = pageContext.getOut();
try {
if (lang.equals("CN"))
out.print(code.getCodeCnDesc());
else
out.print(code.getCodeEnDesc());
} catch (IOException e) {
log.error(e);
throw new JspException(e);
}
}
break;
case DISPLAY_CHILDCODES:
try {
List codeList = service.getCodesByCodeType(codeType);
JspWriter out = pageContext.getOut();
for (Iterator i = codeList.iterator(); i.hasNext();) {
Code co = (Code) i.next();
StringBuffer html = new StringBuffer();
if (lang.equals("CN")) {
html.append("<option value='").append(co.getCodeValue()).append("' ");
if (selectedValue != null && selectedValue.equals(co.getCodeValue())) {
html.append("selected");
}
html.append(">").append(co.getCodeCnDesc()).append("</option>");
} else {
html.append("<option value='").append(co.getCodeValue()).append("' ");
if (selectedValue != null && selectedValue.equals(co.getCodeValue())) {
html.append("selected");
}
html.append(">").append(co.getCodeEnDesc()).append("</option>");
}
out.print(html);
}
} catch (Exception e) {
log.error(e);
throw new JspException(e);
}
break;
default:
break;
}
return super.doStartTag();
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -