自定义标签

目录:

1.自定义JSP的页面标签

2. 自定义JSTL函数标签

内容:

1.自定义JSP

   《1》在web-inf目录下新建一个.tld文件(和web.xml同一目录)如:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE taglib PUBLIC "-//Sun Microsystems, Inc.//DTD JSP Tag Library 1.2//EN" "http://java.sun.com/dtd/web-jsptaglibrary_1_2.dtd">
<taglib>
  <tlib-version>1.0</tlib-version>
  <jsp-version>1.2</jsp-version>
  <short-name>caac</short-name>
<tag>
    <name>dataBrowse</name>
    <tag-class>com.ac.core.taglib.DataBrowseTag</tag-class>
    <body-content>JSP</body-content>
    <attribute>
      <name>id</name>
      <required>true</required>
      <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
      <name>tmpId</name>
      <required>true</required>
      <rtexprvalue>false</rtexprvalue>
    </attribute>
    <attribute>
      <name>param</name>
      <required>true</required>
      <rtexprvalue>true</rtexprvalue>
    </attribute>
  </tag>
</taglib>
《2》新建DataBrowseTag类,继承BodyTagSupport

public class DataBrowseTag extends BodyTagSupport {
    private static final long serialVersionUID = 1L;
    private String param;
    private String tmpId;
    private String id;

    public DataBrowseTag() {
        super();
    }

    public void release() {
        super.release();
        param = null;
        tmpId = null;
        id = null;
    }

    public int doStartTag() throws JspException {
        return EVAL_BODY_INCLUDE;
    }

    public int doEndTag() throws JspException {
        HttpServletRequest req = (HttpServletRequest)pageContext.getRequest();
        JdbcTemplate jt = Config.getInstance().getJdbcTemplate();//获取操作数据库的对象
        String sql = "SELECT WR_CONT FROM HND_TMP_WHERE WHERE ID='"+ tmpId + "'";
        //System.out.println(sql);
        System.out.println(param);
        sql = (String) jt.queryForObject(sql, String.class);
        String[] params = param.split(",");
        List fList = jt.queryForList(sql,params);
       req.setAttribute(id, fList);      //如果要返回值则将此值设置到相应的变量中

       ///如果需要直接向页面上输出内容,则应如下操作
      

        try {
            pageContext.getOut().print(results);//results中存放的为要向页面输出的内容,包括html代码
        } catch (IOException ex) {
            ex.printStackTrace();
            throw new JspException("Error - tag.getProperty : IOException ");
        }

      ///
        return EVAL_PAGE;
    }
     public String getParam() {
        return param;
    }

    public void setParam(String param) {
        this.param = param;
    }

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getTmpId() {
        return tmpId;
    }

    public void setTmpId(String tmpId) {
        this.tmpId = tmpId;
    }
}

《3》在页面中

   引入:% @taglib uri="/WEB-INF/agile.tld" prefix="ac" %

  使用:

<ac:dataBrowse id="gbType" tmpId="CH900000000000000000000000000201" param='<%=(String)tmpParam%>'/>

//id同样可以用request.getAttribute(“gbType”)来获取值
    <logic:iterate id="sysRow" name="gbType" indexId="iIndex">

  </logic:iterate>

2.自定义JSTL函数标签

    《1》在web-inf目录下新建一个.tld文件(和web.xml同一目录),其内容参考jstl-1.2.jar包中的fn.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>JSTL 1.1 functions library</description>
              <display-name>JSTL functions</display-name>
              <tlib-version>1.1</tlib-version>
              <short-name>jspJstl</short-name>
             <uri>http://com.fendou/jsp/jstl/functions/jspJstl</uri>
              <function>
                    <description>
                          Removes white spaces from both ends of a string.
                    </description>
                    <name>getDate</name>
                    <function-class>com.fendou.showDate</function-class>
                    <function-signature>String getDate()</function-signature>   
              </function>

        </taglib>

    其中<short-name>jspJstl</short-name>中的jspJstl要与页面中的prefix同名,
        <uri>http://com.fendou/jsp/jstl/functions/jspJstl</uri>中的自己命名,但在jsp页面中要手工复制导入到uri中
        <name>getDate</name>,<function-signature>String getDate(java.util.Date)</function-signature>两个标签中的getDate一定要相同,也就是name就是函数名
        String 是函数的返回值(不需要写指定的包),java.util.Date为函数的参数,必需加上指定的包名
        <function-class>com.fendou.showDate</function-class>中的为对应类所在的包全路径加上类名   
    《2》在src目录下编写一个类showDate,其中函数为public static String  getDate(Date date){};//必需为public static 方法
    《3》在jsp页面中引用自定义的标签函数:
        <%@ taglib prefix="jspJstl" uri="http://com.fendou/jsp/jstl/functions/jspJstl" %>
        ${jspJstl:getDate(new Date())}//其中getDate为手动创建的tld文件中的name

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值