Struts自定义标签tag

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>

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值