【问题标题】:JAVA - Printing user input into HTMLJAVA - 将用户输入打印到 HTML
【发布时间】:2018-08-23 22:17:40
【问题描述】:

我想使用 Java 获取用户输入并将其打印为 index.html 表单。用户输入位于数组列表中。通过代码的外观,您可以知道它将打印来自#nbOfPieces 次的 HTML。如何打印 arraylist 中的单个项目并且只打印一次 html 代码?

for (int i = 0;i < nbOfPieces ;i++ ) {
                pw.println(
                    "<html>\n" +
                            "<head>\n" +
                            "    <title>Maria Shop</title>\n" +
                            "    <link rel=\"stylesheet\" href=\"style.css\">\n" +
                            "</head>\n" +
                            "<body>\n" +
                            "<center>\n" +
                            "<section id=\"portfolio\">\n" +
                            "        <h1>MARIA TEST</h1>\n" +
                            "            <div class=\"img-box\">\n" +
                            "            <a href=" + links.get(i) + "><img src=\"./img/"+subImg.get(i)+"\" alt=\"CPU image\"></a>\n" +
                            "            </div>\n" +
                            "            <div class=\"img-box\">\n" +
                            "                <a href=" + links.get(i) + "><img src=\"./img/"+subImg.get(i)+"\" alt=\"CPU image\"></a>\n" +
                            "            </div>\n" +
                            "            <div class=\"img-box\">\n" +
                            "                <a href=" + links.get(i) + "><img src=\"./img/"+subImg.get(i)+"\" alt=\"CPU image\"></a>\n" +
                            "            </div>\n" +
                            "</section>\n" +
                            "</center>\n" +
                            "</body>\n" +
                            "</html>"
                );
            }

【问题讨论】:

  • 通过在循环之前打印开始节(htmlcenter),然后在循环之后打印结束节(centerhtml)。

标签: java html printwriter


【解决方案1】:

只需将要打印的内容分成三份,如下所示:

pw.println(
   "<html>\n" +
    "<head>\n" +
    "    <title>Maria Shop</title>\n" +
    "    <link rel=\"stylesheet\" href=\"style.css\">\n" +
    "</head>\n" +
    "<body>\n" +
    "<center>\n" +
    "<section id=\"portfolio\">\n" +
    "        <h1>MARIA TEST</h1>"
);
//Repeated sectection
for (int i = 0;i < nbOfPieces ;i++ ) {
    pw.println(
        "            <div class=\"img-box\">\n" +
        "            <a href=" + links.get(i) + "><img src=\"./img/"+subImg.get(i)+"\" alt=\"CPU image\"></a>\n" +
        "            </div>\n" +
        "            <div class=\"img-box\">\n" +
        "                <a href=" + links.get(i) + "><img src=\"./img/"+subImg.get(i)+"\" alt=\"CPU image\"></a>\n" +
        "            </div>\n" +
        "            <div class=\"img-box\">\n" +
        "                <a href=" + links.get(i) + "><img src=\"./img/"+subImg.get(i)+"\" alt=\"CPU image\"></a>\n" +
        "            </div>"
    );
}
pw.println(
    "</section>\n" +
    "</center>\n" +
    "</body>\n" +
    "</html>"
);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-03-31
    • 2015-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多