• 定制标签-动态数据的标记:迭代执行体


    自定义一个简单的标记开发。需要一个标记处理器,一个tld文件,一个jsp文件。

    simpletag.jsp

    <%@ taglib prefix="my" uri="simple" %>
    <html>
    <body>
    <em><p>Movie List</p></em><br>
    <my:do>
    Name:${movie}      author:${author}<br>
    </my:do>
    </body>
    </html>

    simpleTag.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">
    <tlib-version>1.2</tlib-version>
    <uri>simple</uri>
    
    <tag>
        <description>Simple Tag</description>
        <name>do</name>
        <tag-class>foo.SimpleTagTest</tag-class>
        <body-content>scriptless</body-content>
    </tag>
    </taglib>

    SimpleTagTest.java

    package foo;
    
    import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.tagext.SimpleTagSupport;
    import java.io.IOException;
    import java.util.Map;
    import java.util.HashMap;
    import java.util.Map.Entry;
    
    public class SimpleTagTest extends SimpleTagSupport {
        //String[] movie = {"Big War","Love","A Happy Day"};
        Map<String,String> movieList = new HashMap<String,String>();
        public void doTag() throws JspException,IOException {
            movieList.put("Big War","jack");
            movieList.put("Love","jenny");
            movieList.put("A Happy Day","hany");
            for(Map.Entry movie : movieList.entrySet()){
                getJspContext().setAttribute("movie",movie.getKey());
                getJspContext().setAttribute("author",movie.getValue());
                getJspBody().invoke(null);
            }
        }
    }

    标记处理器是个java代码文件,继承SimpleTagSuport类,并重写doTag方法。invoke方法的参数null的意思是指输出到响应。

    上面的标记体并没有标记体属性,下面的例子展示具有属性的简单标记。

    simpletag2.jsp

    <%@ taglib prefix="mytag2" uri="simple2" %>
    <html>
    <body>
    <% pageContext.setAttribute("MOVIE","Happy Day"); %>
    <mytag2:do movieName="${MOVIE}" >
    name:${MovieName}
    </mytag2:do>
    </body>
    </html>

    SimpleTagTest2.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">
    <tlib-version>1.2</tlib-version>
    <uri>simple2</uri>
    
    <tag>
        <description>Simple2 Tag</description>
        <name>do</name>
        <tag-class>foo.SimpleTagTest2</tag-class>
        <body-content>scriptless</body-content>
        <attribute>
            <name>movieName</name>
            <required>true</required>
            <rtexprvalue>true</rtexprvalue>
        </attribute>
    </tag>
    </taglib>

    SimpleTagTest2.java

    package foo;
    
    import javax.servlet.jsp.JspException;
    import javax.servlet.jsp.tagext.SimpleTagSupport;
    import java.io.IOException;
    import java.util.Map;
    import java.util.HashMap;
    import java.util.Map.Entry;
    
    public class SimpleTagTest2 extends SimpleTagSupport {
        private String movieName;
        public void setMovieName(String movieName) {
            this.movieName = movieName;
        }
        public void doTag() throws JspException,IOException {
            getJspContext().setAttribute("MovieName",movieName);
            getJspBody().invoke(null);    
        }
    }

    以上代码很简单就不在赘言了。

  • 相关阅读:
    解析form表单数据
    linux下开启https
    Jquery UI的日历控件datepicker限制日期(转)
    [MicroPython]TPYBoardv102超全DIY案例一览
    [MicroPython]TurnipBit开发板DIY自动浇水系统
    [MicroPython]TurniBit开发板旋转按钮控制脱机摆动
    [MicroPython]TurniBit开发板DIY自动窗帘模拟系统
    [Micropython]TPYBoard v102 DIY照相机
    python的各种推导式(列表推导式、字典推导式、集合推导式)
    管理 python logging 日志使用
  • 原文地址:https://www.cnblogs.com/justenjoy/p/6771322.html
Copyright © 2020-2023  润新知