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. (i.e 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>