0

Assume I implement MVC model, there is List object which contains data from database where I get by using JPA in session bean and pass to servlet. The List object then pass to jsp from servlet. Since I need to display it in table in JSP by using JSTL, there is a lot of 40++ columns in database.

How should I display it by calling the columns index instead of columns name so that I can loop it rather than typing the column name out? My basic idea is:

<table>
<tr>
    <c:forEach var="i" begin="1" end="49" items="${listObject}">
       <td>${//here to loop listObject column name by index}</td>
    </c:forEach>
</tr>
<c:/forEach item="${listObject}"  var="a">
 <tr>
    <c:forEach var="i" begin="1" end="49">
    <td>${//here to loop listObject data by column index}</td>
    </c:forEach>
</tr>
</c:forEach>
</table>

So that the output would be like:

stu ID   | First Name | Last Name | Attendance Week 1| ...... | Attendance Week 46
-----------------------------------------------------------------------------------
         |            |           |                  |        |
         |            |           |                  |        |
         |            |           |                  |        |
         |            |           |                  |        |

It is impossible for me to type from column 1 to column 40++, so I would like to use looping, anyone can help?

helloworld1234
  • 287
  • 1
  • 5
  • 16
  • for the element, just take look at you first question http://stackoverflow.com/questions/39866370/jstl-loop-by-column-index. – AxelH Oct 06 '16 at 06:51
  • For the column name, you could create an array with the names, or in the same collection in the first row. – AxelH Oct 06 '16 at 06:52
  • Thanks, yes, for the column name, you are right, I can insert at the first row in database. However, come to the element, the way you suggest is to get the element at specific position in the array. Use c[0], means to get the first element in the c array instead of the calling the element at first column. – helloworld1234 Oct 06 '16 at 07:04
  • Well,usually, I create the array in the same order than the headers so the value in index 2 is the one for the headers in the same index. – AxelH Oct 06 '16 at 07:06
  • in JSTL, normally call the element like c.columnA, c.columnB....but this time since I have 40++ column, I would like to call the element like c.[0], c.[1]...so that I can loop the number incrementally to call the element instead of type it for every single column.. – helloworld1234 Oct 06 '16 at 07:06
  • Same comment, why do you think that the values in the column X will not be the one for the header X that you want to put in the column X of the table. If you can't change the order of the array, you will need to first create a map to say column X name "foo" should be in Y. Then loop on the map to create the table with this new mapping. – AxelH Oct 06 '16 at 07:10

1 Answers1

1

You would need "reflection" to achieve this.
The basic idea is that:

  • loop through the fields/properties of your object.
  • create a list of string called fieldValueList that has all the values of the properties. Each element of the op's listObject will need to have a corresponding fieldValueList.
  • In JSTL, simply loop the listObject and then loop the fieldValueList.

Here is another thread that is pretty relevant.
How to loop over a Class attributes in Java?

Not related to your question, you might want to consider refactor/normalize table so that your table does not contain hard coded "Attendance Week" from 1 to 46 so you do not need to deal with reflection on this seemingly simple problem.

Community
  • 1
  • 1
Minjun Yu
  • 3,218
  • 4
  • 21
  • 36
  • How would you use Reflection in JSTL ? Or are you suggesting to add Java in JSP ? – AxelH Oct 06 '16 at 07:27
  • @AxelH create a list of string called `fieldValueList` that has all the values of the properties. Each element of the op's `listObject` will need to have a corresponding `fieldValueList`. In JSTL, simply loop the `listObject` and then loop the `fieldValueList`. The fact that op's database table have something like Attendence week 1 through 49 makes me think that it needs normalization but it is a different problem.. – Minjun Yu Oct 06 '16 at 15:50
  • Yes, I did consider the normalization, since the some of the design issue, it is better for me to hard coded the attendance list. By the way thanks for helping. Is it the link provided is the same idea you want to address? [http://stackoverflow.com/a/160976/5614753] – helloworld1234 Oct 07 '16 at 03:38
  • @Minjun.Y, ok """"reflexion"""" ;) Yeah, this is the easiest – AxelH Oct 07 '16 at 07:50