简体   繁体   中英

How to read data from CSV and write it in HTML template using java

I want to read data from CSV file and write the data in html template using java. right now I am reading csv file but while writing the data into html template, looping of data is not happening. (ie if there are 10 rows in csv file then it should create 10 rows dynamically in html table but that is not happening )

I googled a lot but couldn't find any relevant examples. It would help me a lot if you share any POC or example. Thanks in advance.

Java code

import java.io.File;
import java.io.FileReader;
import java.util.Arrays;
import java.util.List;

import org.apache.commons.io.FileUtils;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import com.opencsv.CSVReader;

@Controller
public class HelloController {

String totalScenarios ;
String passScenarios ;

@RequestMapping("/")
String home(ModelMap modal) throws Exception{
    //reading from csv file
    CSVReader reader = new CSVReader(new FileReader("employees.csv"));
       List<String[]> myEnteries = reader.readAll();
       reader.close();
       for(String[] entry:myEnteries)
       {
           System.out.println(Arrays.toString(entry));
           totalScenarios =  Arrays.toString(entry);
           passScenarios = entry.toString();
          // writing in html file
           modal.addAttribute("title", totalScenarios);
           modal.addAttribute("message", passScenarios);
       }      
       return "hello";
}
}

CSV data

[1, java, java, 1]
[2, angular, angular, 1]
[3, java, java, 1]

HTML template

<!DOCTYPE html>

<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="css/bootstrap.css" />
</head>
<body>
<div class="container">
    <div class="jumbotron">
        <h2>${title}</h2>
     <table>
        <thead>
          <tr>
            <th>title</th>                
          </tr>
          </thead>
          <tbody>
          <tr>
            <td>${title}</td>              
          </tr>
          <tr>
            <td>${title}</td>               
          </tr>
          </tbody>
        </table>

    </div>
</div>

Your modelMap will only have the last row from the csv file in it. It's a map, not a list, so model.addAttribute() will replace the previous value with the new value. Not sure what you need for your templating, but if a list of strings works you'd need to do something like...

List<String> totalScenarios = new ArrayList<>();
List<String> passScenarios = new ArrayList<>();
for(String[] entry:myEnteries)
{
   totalScenarios.add(Arrays.toString(entry));
   passScenarios.add(entry.toString());
}
// writing in html file
modal.addAttribute("title", totalScenarios);
modal.addAttribute("message", passScenarios);     

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM