jstl標籤基礎開發步驟

來源:互聯網
上載者:User

標籤:tle   1.0   mic   return   賦值   vax   writer   out   ide   

step1. 導包(匯入要使用的標籤的jar檔案)。step2. 使用taglib指令引入要使用的標籤。taglib指令:    uri:標籤的命名空間。    prefix:命名空間的別名。註:    命名空間:是為了區分同名的元素而添加的首碼。

自訂標籤:
step1. 寫一個java類,繼承SimpleTagSupport類。step2. 在doTag方法裡面,編寫處理邏輯。step3. 描述標籤。(.tld檔案裡面)    注: <body-content>的值可以是        empty    沒有標籤體。           scriptless  可以有標籤體,但是標籤體的內容不能夠出現                    java代碼。        JSP       可以有標籤體,標籤體的內容可以出現java代碼。        (只有複雜標籤技術才支援JSP這個值,簡單標籤技術只支援        empty和scriptless)。
簡單日期標籤datetag.tld例:
<?xml version="1.0" encoding="UTF-8" ?><taglib xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-jsptaglibrary_2_1.xsd"    version="2.1">    <tlib-version>1.1</tlib-version>    <short-name>d1</short-name>    <uri>http://aliyun.com</uri>    <tag>        <!-- name:表示自訂標籤 <c1:date名字相同 -->        <name>date</name>        <tag-class>tag.DateTag</tag-class>        <body-content>empty</body-content>        <attribute>            <!-- name:表示自訂標籤 <c1:date datetag名字相同 -->            <name>datetag</name>            <!-- true,表示可以使用el運算式來賦值 -->            <required>true</required>            <rtexprvalue>false</rtexprvalue>        </attribute>    </tag></taglib>
日期java標籤類:
package tag;/** * 自訂日期標籤 */import java.io.IOException;import java.text.SimpleDateFormat;import java.util.Date;import javax.servlet.jsp.JspException;import javax.servlet.jsp.JspWriter;import javax.servlet.jsp.PageContext;import javax.servlet.jsp.tagext.SimpleTagSupport;public class DateTag extends SimpleTagSupport {    private String datetag;    public String getDatetag() {        return datetag;    }    public void setDatetag(String datetag) {        this.datetag = datetag;    }    @Override    public void doTag() throws JspException, IOException {        PageContext pc = (PageContext) getJspContext();        SimpleDateFormat sdf = new SimpleDateFormat(datetag);        String date = sdf.format(new Date());        JspWriter out = pc.getOut();        out.println(date);    }}

jstl標籤基礎開發步驟

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.