zoukankan      html  css  js  c++  java
  • 定制标签-动态数据的标记:迭代执行体

    自定义一个简单的标记开发。需要一个标记处理器,一个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);    
        }
    }

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

  • 相关阅读:
    [LeetCode 1029] Two City Scheduling
    POJ 2342 Anniversary party (树形DP入门)
    Nowcoder 106 C.Professional Manager(统计并查集的个数)
    2018 GDCPC 省赛总结
    CF 977 F. Consecutive Subsequence
    Uva 12325 Zombie's Treasure Chest (贪心,分类讨论)
    Poj 2337 Catenyms(有向图DFS求欧拉通路)
    POJ 1236 Network of Schools (强连通分量缩点求度数)
    POJ 1144 Network (求割点)
    POJ 3310 Caterpillar(图的度的判定)
  • 原文地址:https://www.cnblogs.com/justenjoy/p/6771322.html
Copyright © 2011-2022 走看看