JAVA-将用户输入打印成HTML

时间:2018-08-23 22:17:40

标签: java html printwriter

我想使用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>"
                );
            }

1 个答案:

答案 0 :(得分:0)

只需将要打印的内容一分为三即可:

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>"
);