解析具有多个子块的JTPL块

时间:2014-11-26 00:53:03

标签: java

我正在使用JTPL(http://jtpl.sourceforge.net/)来解析HTML。当我有一个将被重复解析的块并且在该块内部具有子块时,输出似乎是先前迭代的刷新数据,除了最后一个子块。我浏览了库的源代码,发现模板解析的方式存在错误,或者可能是我正在尝试的功能不受支持。是否有任何面临类似的问题并用补丁修复它?

以下是我的java程序

import net.sf.jtpl.Template;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.*;
import javax.servlet.*;

@WebServlet("/sample1")
public class sample1 extends HttpServlet implements SingleThreadModel {
        Template tpl;

        protected void doGet(HttpServletRequest request,
                        HttpServletResponse response) throws ServletException, IOException {
                PrintWriter out = response.getWriter();
                try {
                        out.print(this.generatePage());
                } catch (Exception e) {
                        e.printStackTrace(out);
                }
        }

        private String generatePage() throws Exception {
                tpl = new Template(new File("C:\\Users\\stadikon\\Desktop\\sample1.jtpl"));
                tpl.assign("TITLE", "Sample 2");
                int line = 10, col = 4, altern = 2, count = 0;
                for (int i = 1; i <= line; i++) {
                        for (int j = 1; j <= col; j++) {
                                if (count % altern == 0) {
                                        tpl.assign("VARIABLE", "section1");
                                        System.out.println("sample1 - before parsing section1");
                                        tpl.parse("main.line.section1");
                                } else {
                                        tpl.assign("VARIABLE", "section2");
                                        System.out.println("sample1 - before parsing section2");
                                        tpl.parse("main.line.section2");
                                }
                                count++;
                        }
                        System.out.println("sample1 - before parsing line");
                        tpl.parse("main.line");

                }
                tpl.parse("main");
                return (tpl.out());
        }
}

关注是.jtpl模板

<!-- BEGIN: main -->
<html>
<head>
<title>{TITLE}</title>
</head>
<body>
    <!-- BEGIN: line -->
    <table>
        <!-- BEGIN: section1 -->
        <tr>
            <td>{VARIABLE}</td>
            <td>Section1</td>
        </tr>
        <!-- END: section1 -->
        <tr>
            <td colspan="2">Dividing Line</td>
        </tr>
        <!-- BEGIN: section2 -->>
        <tr>
            <td>{VARIABLE}</td>
            <td>Section2</td>
        </tr>
        <!--  END: section2 -->
    </table>
    <!-- END: line -->
</body>
</html>
<!-- END: main -->

实际输出:

> >
section1    Section1
section1    Section1
Dividing Line
section2    Section2
section2    Section2
> >
section1    Section1
section1    Section1
section1    Section1
section1    Section1
Dividing Line
section2    Section2
section2    Section2
> >
section1    Section1
section1    Section1
section1    Section1
section1    Section1
section1    Section1
section1    Section1
Dividing Line
section2    Section2
section2    Section2

预期产出

> >
section1    Section1
section1    Section1
Dividing Line
section2    Section2
section2    Section2
> >
section1    Section1
section1    Section1
Dividing Line
section2    Section2
section2    Section2
> >
section1    Section1
section1    Section1
Dividing Line
section2    Section2
section2    Section2

任何指针都表示赞赏。

1 个答案:

答案 0 :(得分:0)

我通过创建一个单独的模板解析了它,解析它,将其输出以字符串格式写入原始模板。