2

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.

1 Answer 1

2

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

2 Comments

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.
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.

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.