自定义JSP标签

 

一个项目中会在多个JSP界面中使用下拉框,日期控件,分页控件。若能将此类控件抽象出来,做成一个JspTag的话,那使用起来就方便多了。同时便于JSP的阅读,更主要的是方便了项目的管理与维护。

下面以行业下拉框控件为例介绍自定义JspTag,尤其注意红色标注处,从对应关系可以找到运用规律:

首先建立/ WEB-INF/tags/my-tag.tld 描述文件:

<?xml version="1.0" encoding="UTF-8" ?>
<taglib xmlns="
http://java.sun.com/xml/ns/j2ee "
xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance "
xsi:schemaLocation="
http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-jsptaglibrary_2_0.xsd "
version="2.0">
    <description>my tag</description>
    <display-name>my tag</display-name>
    <tlib-version>1.0</tlib-version>
    <short-name>my-tag</short-name>
    <uri>/ my-tag </uri>

    <tag>
    <!--
          * 行业下拉框控件
          * 使用说明:
          * jsp中使用范例(最短模式):<htdz-tag:TradeSelectTag selected="${tradeid}"/>
          * 1. name可以不写,默认值为"tradeid"
          * 2. id可以不写,默认值为"tradeSelect"
          * 3. onchange可以不写,默认值为"findShopByTrade()";如果为οnchange="none",则不生成onchange事件
          * 4. firstoption为首个下拉框的text。可以不写。
          * 5. firstvalue为首个下拉框的value。可以不写。
          *   
          *    关于firstoption和firstvalue的使用:
          *    (1)如果两者均无,则默认添加option='==行业==',value='0'
          *    (2)如果只有firstoption,没有firstvalue,则此option的value为'0';
          *    (3)如果只有firstvalue,没有firstoption,则firstvalue无效,形同两者均无。
          *   
          * 6. selected为下拉框选中的值,可以不写。
    -->
        <description>行业下拉框控件</description>
        <name>TradeSelectTag</name>
        <tag-class>com.test.util.tag.TradeSelectTag </tag-class>
        <body-content>JSP</body-content>
        <attribute>
            <description>name属性,用于服务器端接收,默认值为"tradeid"</description>
            <name>name</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
         </attribute>
        <attribute>
            <description>id属性,用于客户端脚本,默认值为"tradeSelect"</description>
            <name>id</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
         </attribute>
        <attribute>
            <description>onchange事件,默认值为"findShopByTrade()";如果为οnchange="none",则不生成onchange事件</description>
            <name>onchange</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
         </attribute>
        <attribute>
            <description>首个下拉框的text</description>
            <name>firstoption</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
         </attribute>
        <attribute>
            <description>首个下拉框的value</description>
            <name>firstvalue</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
         </attribute>
        <attribute>
            <description>下拉框选中的值</description>
            <name>selected</name>
            <required>false</required>
            <rtexprvalue>true</rtexprvalue>
         </attribute>
    </tag>
</taglib>

下面是最主要的com.test.util.tag.TradeSelectTag,它继承TagSupport,重写了doStartTag()绘制JSP元素:

public class TradeSelectTag extends TagSupport {

    //以下属性与配置中对应
    private String name;
    private String id;
    private String firstoption;
    private String firstvalue;
    private String onchange;
    private String selected;

    public TradeSelectTag() {
    }

    @SuppressWarnings( { "unchecked", "static-access" })
    public int doStartTag() throws JspException {
        if (name == null || name.length() == 0)
            name = "tradeid";
        if (id == null || id.length() == 0)
            id = "tradeSelect";
        if (onchange == null || onchange.length() == 0)
            onchange = "findShopByTrade()";
         StringBuffer html = new StringBuffer();
        html.append("<select name=/"");
        html.append(name);
        html.append("/" id=/"");
        html.append(id);
        html.append("/" ");
         if (!"none".equals(onchange.toLowerCase())) {// 如果οnchange="none",则不添加onchange事件
            html.append("οnchange=/"");
            html.append(onchange);
            html.append("/"");
        }
        html.append(">");

        html.append("<option value=/"");
        if (firstvalue != null)
            html.append(firstvalue);
        else
            html.append("0");
        html.append("/" ");
        if (selected == null || selected.length() == 0 || "0".equals(selected))
            html.append("selected");
            html.append(">");
        if (firstoption != null && firstoption.length() > 0)
            html.append(firstoption);
        else
            html.append("==行业==");
        html.append("</option>");

        try {

            //MyParameter.TRADETYPE这个Map中存有行业ID与对应的名称,这里忽略这个类...
            if (MyParameter.TRADETYPE != null && MyParameter.TRADETYPE.size() > 0) {
                Iterator iterator = MyParameter.TRADETYPE.entrySet().iterator();
                while (iterator.hasNext()) {
                    Map.Entry entry = (Map.Entry) iterator.next();
                    Object key = entry.getKey();
                    Object value = entry.getValue();
                    html.append("<option value=/"");
                    html.append(key);
                    html.append("/" ");
                    if (selected != null && selected.length() > 0 && key != null&& key.toString().equals(selected))
                        html.append("selected");
                        html.append(">");
                        html.append(value);
                        html.append("</option>");
                    }

                }
            html.append("</select>");
            pageContext.getOut().println(html.toString());
        } catch (Exception e) {
            throw new JspException(e.getMessage());
        }
        return this.SKIP_BODY;
    }

    public void setName(String newName) {
        name = newName;
    }

    public void setId(String newId) {
        id = newId;
    }

    public void setFirstoption(String firstoption) {
        this.firstoption = firstoption;
    }

    public void setOnchange(String onchange) {
        this.onchange = onchange;
    }

    public void setSelected(String selected) {
        this.selected = selected;
    }

    public void setFirstvalue(String firstvalue) {
        this.firstvalue = firstvalue;
    }
}

若要在JSP中使用自定义标签,web.xml中<jsp-config/>的配置必不可少:

<jsp-config>

    <taglib>
        <taglib-uri>/ my-tag </taglib-uri>
        <taglib-location>/ WEB-INF/tags/my-tag.tld </taglib-location>
     </taglib>

</jsp-config>

JSP中在相应的位置只用这么使用即可:

<my-tag:TradeSelectTag selected="${tradeid}"/>

怎么样,很方便吧

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值