1.首先需要在web.xml中添加tld
<taglib>
<taglib-uri>/WEB-INF/struts-logic.tld</taglib-uri>
<taglib-location>/WEB-INF/struts-logic.tld</taglib-location>
</taglib>
<taglib>
<taglib-uri>/WEB-INF/gfs.tld</taglib-uri>
<taglib-location>/WEB-INF/gfs.tld</taglib-location>
</taglib>
2.其次需要新建
gfs.tld
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.1//EN" "http://java.sun.com/j2ee/dtds/web-jsptaglibrary_1_1.dtd">
<taglib>
<tlibversion>1.0</tlibversion>
<jspversion>1.1</jspversion>
<shortname>hyt</shortname>
<uri>http://jakarta.apache.org/struts/tags-html</uri>
<tag>
<name>optionDict</name>
<tagclass>com.alipay.gfs.web.jsptag.OptionDictValue</tagclass>
<bodycontent>JSP</bodycontent>
<attribute>
<name>codeId</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>subcodeId</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
<attribute>
<name>subsubcodeId</name>
<required>false</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
<tag>
<name>optionEaccount</name>
<tagclass>com.alipay.gfs.web.jsptag.OptionEaccountValue</tagclass>
<bodycontent>JSP</bodycontent>
</tag>
</taglib>
3.再次新建OptionDictValue
package com.alipay.gfs.web.jsptag;
import java.util.List;
import javax.servlet.jsp.JspException;
import org.apache.struts.taglib.html.Constants;
import org.apache.struts.taglib.html.OptionsCollectionTag;
import org.apache.struts.taglib.html.SelectTag;
import org.apache.struts.util.RequestUtils;
import org.apache.struts.util.ResponseUtils;
import com.alipay.gfs.common.GlobalVar;
import com.alipay.gfs.dao.DictDao;
import com.alipay.gfs.domain.Dict;
public class OptionDictValue extends OptionsCollectionTag {
protected String codeId = "";
protected String subcodeId = null;
protected String subsubcodeId = null;
public String getCodeId() {
return codeId;
}
public void setCodeId(String codeId) {
this.codeId = codeId;
}
public String getSubcodeId() {
return subcodeId;
}
public void setSubcodeId(String subcodeId) {
this.subcodeId = subcodeId;
}
public String getSubsubcodeId() {
return subsubcodeId;
}
public void setSubsubcodeId(String subsubcodeId) {
this.subsubcodeId = subsubcodeId;
}
public int doStartTag() throws JspException {
// Acquire the select tag we are associated with
SelectTag selectTag = (SelectTag) pageContext
.getAttribute(Constants.SELECT_KEY);
if (selectTag == null) {
JspException e = new JspException(messages
.getMessage("optionsCollectionTag.select"));
RequestUtils.saveException(pageContext, e);
throw e;
}
DictDao dao = (DictDao)GlobalVar.wac.getBean("dictDao");
Dict dict = new Dict();
dict.setCodeId(codeId);
dict.setSubcodeId(subcodeId);
dict.setSubsubcodeId(subsubcodeId);
List lstCode = dao.getCodes(dict);
StringBuffer sb = new StringBuffer();
for(int i=0; i<lstCode.size();i++)
{
Dict dct = (Dict)lstCode.get(i);
addOption(sb, dct.getCodeName(), dct.getCodeValue(), selectTag
.isMatched(dct.getCodeValue()));
}
ResponseUtils.write(pageContext, sb.toString());
return SKIP_BODY;
}
}
4.使用标签
<html:select property="revenueTypeTwo">
<html:option value="">请选择</html:option>
<gfs:optionDict codeId="102" subsubcodeId="notnull"></gfs:optionDict>
</html:select>
久久不醉