How to display TABLE dynamically in JSP using SPRING MVC

15,825

Why are you looking over the list of headers twice? You dont need that scriptlet code.

Since tableView.tableHeaders returns a list of strings, you just need :

<table>
        <tr>
            <c:forEach var = "listValue" items = "${tableView.tableHeaders}">
            <td>
                <c:out value="${listValue}"/>
            </td>
            </c:forEach>
        </tr>
</table>
Share:
15,825
user1782009
Author by

user1782009

Updated on June 13, 2022

Comments

  • user1782009
    user1782009 almost 2 years

    I want to create a table dynamically in such a way that no of headers and data i can pass from JAVA class and on no. basis, jsp should create a table accordingly.

    I have controller class in which i am returning model object which is my bean class. In that i have a list attribute in which i have hardcoded some values.These values should be by header names in the JSP table. When i am rendering this model object to jsp then i am not able to retrieve the headers info. Plz suggest. My contrller class loooks like this:

    @RequestMapping
    public ModelAndView getHeaders(PortletRequest portlestRequest, PortletResponse portletResponse){
    
        ModelAndView mv = new ModelAndView();
    
        TableDAO dao = new TableDAO();
        List<String> headersList = dao.getHeaders();
    
        TableView tableView  = new TableView();
    
        tableView.setTableHeaders(headersList);
        mv.addObject("tableView",tableView);
        mv.setView("tableView");
        return mv;
    }
    

    My jsp:

    <table>
    <c:forEach var = "listValue" items = "${tableView.tableHeaders}">
        <tr>
          <%for(int i = 0;i<5;i++){ %>
                    <td>
                        <%=${listValue.get(i)} %>
                    </td>
                <%} %>
        </tr>
      </c:forEach>
    </table> 
    

    Someone plz help me on this.

  • user1782009
    user1782009 almost 10 years
    it worked.i ned 2 display data blow dese headers.I hav a clas People n in dat i have 2 att. name, age.I created n aray list of PEOPLE. Im doig lik this: People people1 = new People();people1.setName("abc");people1.setDOB("3-apr-1987")‌​; Then:People people2 = new People();people1.setName("xyz"); people1.setDOB("3-dec-1987");then i am setting these two objects in a list n pasing dis lst to d main clas which wilreturned as Model 2 d jsp bt i am geting wrong output. Could u plz sugest how 2 do this.
  • Louise Miller
    Louise Miller almost 10 years
    comments arent the place for adding another question. Since this question is answered, its best to post a new question if you have a new issue.