如何从列表中生成java类中的动态表

时间:2015-09-04 09:51:42

标签: java html

我正在一个应用程序中工作,我必须在java类中创建一个html表,并且必须将其保存在database.I我在java中创建该表但是如何在该表中生成动态行。使用3个列表我正在做我已经完成的事情,

                              +"Interview LineUp"
                              +" <table border ='1'>"
                              +"<tr>"                           
                              +"<td>Interviewe</td>"
                              +"<td>Timing1</td>"       
                              +"<td>Timing2</td> "                              
                              +"</tr> "
                              +"<tr>"                             
                              +"<td>name</td>"
                              +"<td>timing1</td> "
                              +"<td>timing2</td> "
                              +"</tr> "
                              +"</table>"

所以这是我在java类中使用的表,我有3个列表,其中包含3组信息,如name,timing1,timing2.Now我想要的是,如果所有列表中有3个值,那么3行将会产生。

列表是

List<String> interviewTimingToFrom1 = Arrays.asList(interviewTime1.split(","));
List<String> interviewTimingToFrom2 = Arrays.asList(interviewTime2.split(","));
List<String> listOfinterviewerName = Arrays.asList(intervierwName.split(","));

就像我在做这个

                          +"<tr>";  
                              for(int k=0;k<listOfinterviewerName .size();k++){
                              +"<td>listOfinterviewerName .get(k)</td>"
                              +}

如何做到这一点,在那个java类中?有人请帮忙。谢谢提前

2 个答案:

答案 0 :(得分:2)

+"test"不是有效的Java语句。你在什么地方添加文字?

以增量方式构建字符串时,总是使用StringBuilder

List<String> interviewTimingToFrom1 = Arrays.asList(interviewTime1.split(","));
List<String> interviewTimingToFrom2 = Arrays.asList(interviewTime2.split(","));
List<String> listOfinterviewerName = Arrays.asList(intervierwName.split(","));

StringBuilder buf = new StringBuilder();
buf.append("<html>" +
           "<body>" +
           "<table>" +
           "<tr>" +
           "<th>Interviewe</th>" +
           "<th>Timing1</th>" +
           "<th>Timing2</th>" +
           "</tr>");
for (int i = 0; i < listOfinterviewerName.size(); i++) {
    buf.append("<tr><td>")
       .append(listOfinterviewerName.get(i))
       .append("</td><td>")
       .append(interviewTimingToFrom1.get(i))
       .append("</td><td>")
       .append(interviewTimingToFrom2.get(i))
       .append("</td></tr>");
}
buf.append("</table>" +
           "</body>" +
           "</html>");
String html = buf.toString();

当然,为了防范Cross-site scripting (XSS)攻击,你应该逃避价值观。

答案 1 :(得分:0)

我对你的问题不是很清楚,但根据我的理解,下面的代码对你有用。

public static void main(String[] args) {

   String s =  ""+"Interview LineUp"
               +" <table border ='1'>"
               +"<tr>"                           
               +"<td>Interviewe</td>"
               +"<td>Timing1</td>"       
               +"<td>Timing2</td> "                              
               +"</tr> "
            ;

    String interviewTime1="11:30,12:30";
    String interviewTime2="13:30,15:00";
    String intervierwName="Adam,Smith";
    List<String> interviewTimingToFrom1 = Arrays.asList(interviewTime1.split(","));
    List<String> interviewTimingToFrom2 = Arrays.asList(interviewTime2.split(","));
    List<String> listOfinterviewerName = Arrays.asList(intervierwName.split(","));
    for(int i=0;i<interviewTimingToFrom1.size();i++)
    {
        s = s.concat( "<tr>"
                +"<td>"+listOfinterviewerName.get(i)+"</td>"
                +"<td>"+interviewTimingToFrom1.get(i)+"</td> "
               +"<td>"+interviewTimingToFrom2.get(i)+"</td> "
               +"</tr> ");
    }
    s=s.concat( "</table>");
    System.out.println(s);
}