1

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>

2
  • where is your code to edit? we want the code to make changes in that. Commented Nov 30, 2018 at 7:03
  • I added code to edit @ArshiyaKhanam Commented Nov 30, 2018 at 7:21

1 Answer 1

1

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);     
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.