title: Import List<String> into jQuery DataTables with Java and Spring MVC
body: <p>I am making Spring MVC Web Service, with jQuery DataTables and I have a few List which I have to import into the jQuery DataTables table. </p>
  
  <pre><code>public class Elements {
  
  private List&lt;String&gt; dep1;
  private List&lt;String&gt; dep2;
  private List&lt;String&gt; dep3;
  private List&lt;String&gt; dep4;
  private List&lt;String&gt; dep5;
  
  private List&lt;String&gt; indep1;
  private List&lt;String&gt; indep2;
  private List&lt;String&gt; indep3;
  private List&lt;String&gt; indep4;
  private List&lt;String&gt; indep5;
  
   public List&lt;String&gt; getDep1() {
          return dep1;
      }
  
      public void setDep1(List&lt;String&gt; first) {
          this.dep1 = first;
      }
  
      public List&lt;String&gt; getDep2(){
          return dep2;
      }
  
      public void setDep2(List&lt;String&gt; dep2){
          this.dep2 = dep2;
      }
  
      public List&lt;String&gt; getDep3(){
          return dep3;
      }
  
      public void setDep3(List&lt;String&gt; dep3){
          this.dep3 = dep3;
      }
  
      public List&lt;String&gt; getDep4(){
          return dep4;
      }
  
      public void setDep4(List&lt;String&gt; dep4){
          this.dep4 = dep4;
      }
  
      public List&lt;String&gt; getDep5(){
          return dep5;
      }
  
      public void setDep5(List&lt;String&gt; dep5){
          this.dep5 = dep5;
      }
  
      public List&lt;String&gt; getIndep1(){
          return indep1;
      }
  
      public void setIndep1(List&lt;String&gt; indep1){
          this.indep1 = indep1;
      }
  
      public List&lt;String&gt; getIndep2(){
          return indep2;
      }
  
      public void setIndep2(List&lt;String&gt; indep2){
          this.indep2 = indep2;
      }
  
      public List&lt;String&gt; getIndep3(){
          return indep3;
      }
  
      public void setIndep3(List&lt;String&gt; indep3){
          this.indep3 = indep3;
      }
  
      public List&lt;String&gt; getIndep4(){
          return indep4;
      }
  
      public void setIndep4(List&lt;String&gt; indep4){
          this.indep4 = indep4;
      }
  
      public List&lt;String&gt; getIndep5(){
          return indep5;
      }
  
      public void setIndep5(List&lt;String&gt; indep5){
          this.indep5 = indep5;
      }
  }
  </code></pre>
  
  <p>This is my JSONClassObject:</p>
  
  <pre><code>import java.util.List;
  
  public class ElementsJsonObject {
  
  
  int iTotalRecords;
  
  int iTotalDisplayRecords;
  
  String sEcho;
  
  String sColumns;
  
  List&lt;Elements&gt; aaData;
  
  public int getiTotalRecords() {
  return iTotalRecords;
  }
  
  public void setiTotalRecords(int iTotalRecords) {
  this.iTotalRecords = iTotalRecords;
  }
  
  public int getiTotalDisplayRecords() {
  return iTotalDisplayRecords;
  }
  
  public void setiTotalDisplayRecords(int iTotalDisplayRecords) {
  this.iTotalDisplayRecords = iTotalDisplayRecords;
  }
  
  public String getsEcho() {
  return sEcho;
  }
  
  public void setsEcho(String sEcho) {
  this.sEcho = sEcho;
  }
  
  public String getsColumns() {
  return sColumns;
  }
  
  public void setsColumns(String sColumns) {
  this.sColumns = sColumns;
  }
  
  public List&lt;Elements&gt; getAaData() {
      return aaData;
  }
  
  public void setAaData(List&lt;Elements&gt; aaData) {
      this.aaData = aaData;
  }
  
  
  }
  </code></pre>
  
  <p>This is my Controller class: </p>
  
  <pre><code>@Controller
  
  public class ElementsController {
  
  
  @RequestMapping(value = "/elements.web", method = 
      RequestMethod.GET)
  public String printWelcome(@ModelAttribute("elements") Elements elements, BindingResult result,ModelMap model, HttpServletRequest 
      request, HttpServletResponse response) {
  
      return "ListOfElements";
  
  }
  
  @RequestMapping(value = "/elementsPaginationDataTables.web", method = RequestMethod.GET, produces = "application/json")
  public @ResponseBody String elementsPaginationDataTables(HttpServletRequest  request) throws IOException {
  
      //Fetch the page number from client
      Integer pageNumber = 0;
      if (null != request.getParameter("iDisplayStart"))
          pageNumber = (Integer.valueOf(request.getParameter("iDisplayStart"))/10)+1;     
  
      //Fetch search parameter
      String searchParameter = request.getParameter("sSearch");
  
      //Fetch Page display length
      Integer pageDisplayLength = Integer.valueOf(request.getParameter("iDisplayLength"));
  
      //Create page list data
      List&lt;Elements&gt; elementsList = createPaginationData(pageDisplayLength);
  
      //Here is server side pagination logic. Based on the page number you could make call 
      //to the data base create new list and send back to the client. For demo I am shuffling 
      //the same list to show data randomly
      if (pageNumber == 1) {
          Collections.shuffle(elementsList);
      }else if (pageNumber == 2) {
          Collections.shuffle(elementsList);
      }else {
          Collections.shuffle(elementsList);
      }
  
      //Search functionality: Returns filtered list based on search parameter
      elementsList = getListBasedOnSearchParameter(searchParameter,elementsList);
  
  
      ElementsJsonObject elementsJsonObject = new ElementsJsonObject();
      //Set Total display record
      elementsJsonObject.setiTotalDisplayRecords(500);
      //Set Total record
      elementsJsonObject.setiTotalRecords(500);
      elementsJsonObject.setAaData(elementsList);
  
      Gson gson = new GsonBuilder().setPrettyPrinting().create();
      String json2 = gson.toJson(elementsJsonObject);
  
      return json2;
  }
  
  private List&lt;Elements&gt; getListBasedOnSearchParameter(String searchParameter,List&lt;Elements&gt; elementsList) {
  
      if (null != searchParameter &amp;&amp; !searchParameter.equals("")) {
          List&lt;Elements&gt; elementsListForSearch = new ArrayList&lt;Elements&gt;();
          searchParameter = searchParameter.toUpperCase();
          for (Elements elements : elementsList) {
              if (elements.getDep1().indexOf(searchParameter)!= -1 || elements.getDep2().indexOf(searchParameter)!= -1
                      || elements.getDep3().indexOf(searchParameter)!= -1 || elements.getDep4().indexOf(searchParameter)!= -1
                      || elements.getDep5().indexOf(searchParameter)!= -1 || elements.getIndep1().indexOf(searchParameter)!= -1
                      || elements.getIndep2().indexOf(searchParameter)!= -1 || elements.getIndep3().indexOf(searchParameter)!= -1
                      || elements.getIndep4().indexOf(searchParameter)!= -1 || elements.getIndep5().indexOf(searchParameter)!= -1)
                      {
                  elementsListForSearch.add(elements);                    
              }
  
          }
  
          elementsList = elementsListForSearch;
          elementsListForSearch = null;
      }
  
      return elementsList;
  }
  
  private List&lt;Elements&gt; createPaginationData(Integer pageDisplayLength) {
      List&lt;Elements&gt; elementsList = new ArrayList&lt;Elements&gt;();
  
      try{
  
          File file = new File("C:/Program Files/Apache Software Foundation/Tomcat 8.0/tmpFiles/Canon.xml");
          JAXBContext jaxbContext = JAXBContext.newInstance(Canon.class);      
          Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
          Canon canon = (Canon) jaxbUnmarshaller.unmarshal(file);
  
          List&lt;Table&gt; table = canon.getTable();
  
      for(Table tables: table){
  
              List&lt;String&gt; row = tables.getRow();
  
              List&lt;String&gt; first = new ArrayList&lt;String&gt;();
              List&lt;String&gt; second = new ArrayList&lt;String&gt;();
              List&lt;String&gt; third = new ArrayList&lt;String&gt;();
              List&lt;String&gt; fourth = new ArrayList&lt;String&gt;();
              List&lt;String&gt; fifth = new ArrayList&lt;String&gt;();
              List&lt;String&gt; sixth = new ArrayList&lt;String&gt;();
              List&lt;String&gt; seventh = new ArrayList&lt;String&gt;();
              List&lt;String&gt; eight = new ArrayList&lt;String&gt;();
              List&lt;String&gt; ninth = new ArrayList&lt;String&gt;();
              List&lt;String&gt; tenth = new ArrayList&lt;String&gt;();
  
              String values = null;
              String values2 = null;
  
              String value1 = null;
              String value2 = null;
              String value3 = null;
              String value4 = null;
              String value5 = null;
              String value6 = null;
              String value7 = null;
              String value8 = null;
              String value9 = null;
              String value10 = null;
  
              String value11 = null;
              String value22 = null;
              String value33 = null;
              String value44 = null;
              String value55 = null;
              String value66 = null;
              String value77 = null;
              String value88 = null;
              String value99 = null;
              String value1010 = null;
  
              List&lt;String&gt; rowss = row.subList(0, 12600);
                  for(int i=0; i&lt;rowss.size(); i++){
                  values = row.get(i);
  
                  String[] parts = values.split(" +");
  
                  String part0 = parts[1];     
                  String part1 = parts[2];
                  String part2 = parts[3];
                  String part3 = parts[4];
                  String part4 = parts[5];
                  String part5 = parts[6];
                  String part6 = parts[7];
                  String part8 = parts[8];
                  String part9 = parts[9];
                  String part10 = parts[10];
  
                  value1 = parts[1];
                  value2 = parts[2];
                  value3 = parts[3];
                  value4 = parts[4];
                  value5 = parts[5];
                  value6 = parts[6];
                  value7 = parts[7];
                  value8 = parts[8];
                  value9 = parts[9];
                  value10 = parts[10];
  
                  first.add(value1);
                  second.add(value2);
                  third.add(value3);
                  fourth.add(value4);
                  fifth.add(value5);
                  sixth.add(value6);
                  seventh.add(value7);
                  eight.add(value8);
                  ninth.add(value9);
                  tenth.add(value10);
                  }   
  
                  List&lt;String&gt; row1 = tables.getRow();
                 List&lt;String&gt; rows = row1.subList(12600, row1.size());
                  for(int j=0; j&lt;rows.size(); j++){
  
                  values2 = rows.get(j);
  
                  String[] parts2 = values2.split(" +");
  
                  String part11 = parts2[0];   
                  String part22 = parts2[1];
                  String part33 = parts2[2];
                  String part44 = parts2[3];
                  String part55 = parts2[4];
                  String part66 = parts2[5];
                  String part77 = parts2[6];
                  String part88 = parts2[7];
                  String part99 = parts2[8];
                  String part1010 = parts2[9];
  
                  value11 = parts2[0];
                  value22 = parts2[1];
                  value33 = parts2[2];
                  value44 = parts2[3];
                  value55 = parts2[4];
                  value66 = parts2[5];
                  value77 = parts2[6];
                  value88 = parts2[7];
                  value99 = parts2[8];
                  value1010 = parts2[9];
  
  
                  first.add(value11);
                  second.add(value22);
                  third.add(value33);
                  fourth.add(value44);
                  fifth.add(value55);
                  sixth.add(value66);
                  seventh.add(value77);
                  eight.add(value88);
                  ninth.add(value99);
                  tenth.add(value1010);
  
  
                  }
  
  
      for (int i = 0; i &lt; 1; i++) {
          Elements element = new Elements();
  
              element.setDep1(first);
              element.setDep2(second);
              element.setDep3(third);
              element.setDep4(fourth);
              element.setDep5(fifth);
              element.setIndep1(sixth);
              element.setIndep2(seventh);
              element.setIndep3(eight);
              element.setIndep4(ninth);
              element.setIndep5(tenth);
  
              elementsList.add(element);              
  
      }       
  
      }
      }
  
                  catch (JAXBException e) {
                      e.printStackTrace();
                  }
      return elementsList;
  }
  }
  </code></pre>
  
  <p>And this is my jsp page:</p>
  
  <pre><code>&lt;%@ page language="java" contentType="text/html; charset=ISO-8859-1"
  pageEncoding="ISO-8859-1"%&gt;
  &lt;!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd"&gt;
  
  &lt;%@ taglib prefix="form" uri="http://www.springframework.org/tags/form" %&gt;
  &lt;html&gt;
  
  &lt;head&gt;
  
  &lt;meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1"&gt;
  
  &lt;title&gt;Spring pagination using data tables&lt;/title&gt;
  
  &lt;link rel="stylesheet" type="text/css" href="//cdn.datatables.net/1.10.0/css/jquery.dataTables.css"&gt;
  `&lt;script type="text/javascript" src="//code.jquery.com/jquery-1.10.2.min.js"&gt;`
  
  &lt;/script&gt;
  
  &lt;script type="text/javascript" src="//cdn.datatables.net/1.10.0/js/jquery.dataTables.js"&gt;&lt;/script&gt;
  
  &lt;script type="text/javascript"&gt;
  
  
  //Plug-in to fetch page data 
  
  
  jQuery.fn.dataTableExt.oApi.fnPagingInfo = function ( oSettings )
  {
  
  
  return {
          "iStart":         oSettings._iDisplayStart,
          "iEnd":           oSettings.fnDisplayEnd(),
          "iLength":        oSettings._iDisplayLength,
          "iTotal":         oSettings.fnRecordsTotal(),
          "iFilteredTotal": oSettings.fnRecordsDisplay(),
          "iPage":          oSettings._iDisplayLength === -1 ?
              0 : Math.ceil( oSettings._iDisplayStart / oSettings._iDisplayLength ),
          "iTotalPages":    oSettings._iDisplayLength === -1 ?
              0 : Math.ceil( oSettings.fnRecordsDisplay() / oSettings._iDisplayLength )
      };
  };
  
  
  $(document).ready(function() {
  
  
  $("#example").dataTable( {
      "bProcessing": true,
      "bServerSide": true,
      "sort": "position",
      //bStateSave variable you can use to save state on client cookies: set value "true" 
      "bStateSave": false,
      //Default: Page display length
      "iDisplayLength": 10,
      //We will use below variable to track page number on server side(For more information visit: http://legacy.datatables.net/usage/options#iDisplayStart)
      "iDisplayStart": 0,
      "fnDrawCallback": function () {
          //Get page numer on client. Please note: number start from 0 So
          //for the first page you will see 0 second page 1 third page 2...
          //Un-comment below alert to see page number
          //alert("Current page number: "+this.fnPagingInfo().iPage);    
      },         
      "sAjaxSource": "elementsPaginationDataTables.web",
      "aoColumns": [
          { "mData": "dep1" },
          { "mData": "dep2" },
          { "mData": "dep3" },
          { "mData": "dep4" },
          { "mData": "dep5" },
          { "mData": "indep1" },
          { "mData": "indep2" },
          { "mData": "indep3" },
          { "mData": "indep4" },
          { "mData": "indep5" },
  
      ]
  } );
  
  
  } );
  
  
  
  &lt;/script&gt;
  
  &lt;/head&gt;
  
  &lt;body&gt;
  
  &lt;form:form action="" method="GET"&gt;
  
  &lt;h2 &gt;Spring MVC pagination using data tables&lt;br&gt;&lt;br&gt;&lt;/h2&gt;
  
  &lt;table width="70%" style="border: 3px;background: rgb(243, 244, 248);"&gt;&lt;tr&gt;&lt;td&gt;
  
  
  &lt;table id="example" class="display" cellspacing="0" width="100%"&gt;
  
  
  &lt;thead&gt;
  
  
  &lt;tr&gt;
  
  
  &lt;th&gt;Dep1&lt;/th&gt;
              &lt;th&gt;Dep2&lt;/th&gt;
              &lt;th&gt;Dep3&lt;/th&gt;
              &lt;th&gt;Dep4&lt;/th&gt;
              &lt;th&gt;Dep5&lt;/th&gt;
              &lt;th&gt;Indep1&lt;/th&gt;
              &lt;th&gt;Indep2&lt;/th&gt;
              &lt;th&gt;Indep3&lt;/th&gt;
              &lt;th&gt;Indep4&lt;/th&gt;
              &lt;th&gt;Indep5&lt;/th&gt;
          &lt;/tr&gt;
      &lt;/thead&gt;       
  &lt;/table&gt;
  &lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;
  &lt;/form:form&gt;
  &lt;/body&gt;
  
  &lt;/html&gt;
  </code></pre>
  
  <p>The List are between try - catch and it is data from XML file. </p>
  
  <p>My question is how to implement those list into a different cells for each column in the table? When I do <code>setDep1(first)</code> I'm getting whole list in one cell in the first column. </p>
  
  <p>How to put each value in a different cell? Maybe i have to mention that <code>first</code>, <code>second</code>, <code>third</code> and so on... to <code>tenth</code> has about 45000 values each. </p>